Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ARROW-12704: [JS] Support and use optional chaining #10278

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions js/bin/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const exists = async (p) => {
}
})()
.then((x) => +x || 0, (e) => {
e && process.stderr.write(`${e && e.stack || e}\n`);
e && process.stderr.write(`${e?.stack || e}\n`);
return process.exitCode || 1;
}).then((code) => process.exit(code));

Expand Down Expand Up @@ -141,7 +141,7 @@ function validateReaderIntegration(jsonData, arrowBuffer) {
for (const [jsonRecordBatch, binaryRecordBatch] of zip(jsonReader, binaryReader)) {
compareTableIsh(jsonRecordBatch, binaryRecordBatch);
}
} catch (e) { throw new Error(`${msg}: fail \n ${e && e.stack || e}`); }
} catch (e) { throw new Error(`${msg}: fail \n ${e?.stack || e}`); }
process.stdout.write(`${msg}: pass\n`);
}

Expand All @@ -151,7 +151,7 @@ function validateTableFromBuffersIntegration(jsonData, arrowBuffer) {
const jsonTable = Table.from(jsonData);
const binaryTable = Table.from(arrowBuffer);
compareTableIsh(jsonTable, binaryTable);
} catch (e) { throw new Error(`${msg}: fail \n ${e && e.stack || e}`); }
} catch (e) { throw new Error(`${msg}: fail \n ${e?.stack || e}`); }
process.stdout.write(`${msg}: pass\n`);
}

Expand All @@ -164,7 +164,7 @@ function validateTableToBuffersIntegration(srcFormat, arrowFormat) {
const srcTable = Table.from(srcFormat === `json` ? jsonData : arrowBuffer);
const dstTable = Table.from(srcTable.serialize(`binary`, arrowFormat === `stream`));
compareTableIsh(dstTable, refTable);
} catch (e) { throw new Error(`${msg}: fail \n ${e && e.stack || e}`); }
} catch (e) { throw new Error(`${msg}: fail \n ${e?.stack || e}`); }
process.stdout.write(`${msg}: pass\n`);
};
}
Expand Down
2 changes: 1 addition & 1 deletion js/gulp/closure-task.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const closureTask = ((cache) => memoizeTask(cache, async function closure(target
const exportedImports = publicModulePaths(srcAbsolute).reduce((entries, publicModulePath) => [
...entries, {
publicModulePath,
exports_: getPublicExportedNames(esmRequire(publicModulePath, { warnings: false }))
exports_: getPublicExportedNames(esmRequire(publicModulePath))
}
], []);

Expand Down
13 changes: 5 additions & 8 deletions js/gulp/minify-task.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ const {
targetDir,
mainExport,
UMDSourceTargets,
terserLanguageNames,
shouldRunInChildProcess,
spawnGulpCommandInChildProcess,
} = require('./util');
Expand All @@ -46,6 +45,7 @@ const minifyTask = ((cache, commonConfig) => memoizeTask(cache, function minifyJ

const webpackConfigs = [mainExport].map((entry) => ({
...targetConfig,
mode: 'production',
name: entry,
entry: { [entry]: path.resolve(`${src}/${entry}.dom.js`) },
plugins: [
Expand All @@ -62,13 +62,10 @@ const minifyTask = ((cache, commonConfig) => memoizeTask(cache, function minifyJ
minimize: true,
minimizer: [
new TerserPlugin({
sourceMap: true,
terserOptions: {
ecma: terserLanguageNames[target],
output: { comments: false, beautify: false },
compress: { unsafe: true },
mangle: true,
safari10: true // <-- works around safari10 bugs, see the "safari10" option here: https://github.com/terser-js/terser#minify-options
ecma: target,
output: { comments: false },
compress: { unsafe: true }
},
})
]
Expand All @@ -83,7 +80,7 @@ const minifyTask = ((cache, commonConfig) => memoizeTask(cache, function minifyJ
}))({}, {
resolve: { mainFields: [`module`, `main`] },
module: { rules: [{ test: /\.js$/, enforce: `pre`, use: [`source-map-loader`] }] },
output: { filename: '[name].js', library: mainExport, libraryTarget: `umd`, umdNamedDefine: true },
output: { filename: '[name].js', library: mainExport, libraryTarget: `umd`, umdNamedDefine: true, globalObject: 'this' },
});

module.exports = minifyTask;
Expand Down
1 change: 0 additions & 1 deletion js/gulp/test-task.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ const ARROW_JAVA_DIR = process.env.ARROW_JAVA_DIR || path.join(ARROW_HOME, 'java
const CPP_EXE_PATH = process.env.ARROW_CPP_EXE_PATH || path.join(ARROW_HOME, 'cpp/build/debug');
const ARROW_INTEGRATION_DIR = process.env.ARROW_INTEGRATION_DIR || path.join(ARROW_HOME, 'integration');
const CPP_JSON_TO_ARROW = path.join(CPP_EXE_PATH, 'arrow-json-integration-test');
const CPP_STREAM_TO_FILE = path.join(CPP_EXE_PATH, 'arrow-stream-to-file');
const CPP_FILE_TO_STREAM = path.join(CPP_EXE_PATH, 'arrow-file-to-stream');

const testFilesDir = path.join(ARROW_HOME, 'js/test/data');
Expand Down
10 changes: 3 additions & 7 deletions js/gulp/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ const gCCLanguageNames = {
es2015: `ECMASCRIPT_2015`,
es2016: `ECMASCRIPT_2016`,
es2017: `ECMASCRIPT_2017`,
es2018: `ECMASCRIPT_2018`,
es2019: `ECMASCRIPT_2019`,
esnext: `ECMASCRIPT_NEXT`
};

Expand All @@ -72,12 +74,6 @@ const UMDSourceTargets = {
esnext: `esnext`
};

const terserLanguageNames = {
es5: 5, es2015: 6,
es2016: 7, es2017: 8,
esnext: 8 // <--- ?
};

// ES7+ keywords Terser shouldn't mangle
// Hardcoded here since some are from ES7+, others are
// only defined in interfaces, so difficult to get by reflection.
Expand Down Expand Up @@ -211,7 +207,7 @@ module.exports = {
mainExport, npmPkgName, npmOrgName, metadataFiles, packageJSONFields,

knownTargets, knownModules, tasksToSkipPerTargetOrFormat,
gCCLanguageNames, UMDSourceTargets, terserLanguageNames,
gCCLanguageNames, UMDSourceTargets,

taskName, packageName, tsconfigName, targetDir, combinations, observableFromStreams,
ESKeywords, publicModulePaths, esmRequire, shouldRunInChildProcess, spawnGulpCommandInChildProcess
Expand Down
25 changes: 12 additions & 13 deletions js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
],
"dependencies": {
"@types/flatbuffers": "^1.10.0",
"@types/node": "^14.14.37",
"@types/node": "^15.0.2",
"@types/text-encoding-utf-8": "^1.0.1",
"command-line-args": "5.1.1",
"command-line-usage": "6.1.1",
Expand All @@ -65,24 +65,24 @@
"tslib": "^2.2.0"
},
"devDependencies": {
"@types/glob": "7.1.1",
"@types/jest": "26.0.22",
"@typescript-eslint/eslint-plugin": "^4.22.0",
"@typescript-eslint/parser": "^4.22.0",
"@types/glob": "7.1.3",
"@types/jest": "26.0.23",
"@typescript-eslint/eslint-plugin": "^4.22.1",
"@typescript-eslint/parser": "^4.22.1",
"async-done": "1.3.1",
"benchmark": "2.1.4",
"cpy": "^8.1.2",
"cross-env": "^7.0.3",
"del-cli": "3.0.1",
"eslint": "^7.24.0",
"eslint-plugin-jest": "^24.3.5",
"esm": "3.2.25",
"esm": "https://github.com/jsg2021/esm/releases/download/v3.x.x-pr883/esm-3.x.x-pr883.tgz",
"glob": "7.1.4",
"google-closure-compiler": "20210406.0.0",
"google-closure-compiler": "20210505.0.0",
"gulp": "4.0.2",
"gulp-json-transform": "0.4.6",
"gulp-rename": "1.4.0",
"gulp-sourcemaps": "2.6.5",
"gulp-json-transform": "0.4.7",
"gulp-rename": "2.0.0",
"gulp-sourcemaps": "3.0.0",
"gulp-typescript": "5.0.1",
"ix": "2.5.3",
"jest": "26.6.3",
Expand All @@ -94,15 +94,14 @@
"npm-run-all": "4.1.5",
"randomatic": "3.1.1",
"rxjs": "5.5.11",
"source-map-loader": "0.2.4",
"terser-webpack-plugin": "4.2.2",
"source-map-loader": "2.0.1",
"ts-jest": "26.5.4",
"ts-node": "9.1.1",
"typedoc": "0.20.35",
"typescript": "4.0.2",
"web-stream-tools": "0.0.1",
"web-streams-polyfill": "3.0.3",
"webpack": "4.29.0",
"webpack": "5.36.2",
"xml2js": "0.4.19"
},
"engines": {
Expand Down
2 changes: 1 addition & 1 deletion js/src/bin/arrow2csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ type ToStringState = {
})()
.then((x) => +x || 0, (err) => {
if (err) {
console.error(`${err && err.stack || err}`);
console.error(`${err?.stack || err}`);
}
return process.exitCode || 1;
}).then((code) => process.exit(code));
Expand Down
4 changes: 2 additions & 2 deletions js/src/io/node/iterable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class IterableReadable<T extends Uint8Array | any> extends Readable {
const it = this._iterator;
let fn: any;
it && (fn = e != null && it.throw || it.return);
fn && fn.call(it, e);
fn?.call(it, e);
cb && cb(null);
}
private _pull(size: number, it: SourceIterator<T>) {
Expand Down Expand Up @@ -94,7 +94,7 @@ class AsyncIterableReadable<T extends Uint8Array | any> extends Readable {
const it = this._iterator;
let fn: any;
it && (fn = e != null && it.throw || it.return);
fn && fn.call(it, e).then(() => cb && cb(null)) || (cb && cb(null));
fn?.call(it, e).then(() => cb && cb(null)) || (cb && cb(null));
}
private async _pull(size: number, it: AsyncSourceIterator<T>) {
const bm = this._bytesMode;
Expand Down
6 changes: 3 additions & 3 deletions js/src/io/node/reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ class RecordBatchReaderDuplex<T extends { [key: string]: DataType } = any> exten
}
_final(cb?: CB) {
const aq = this._asyncQueue;
aq && aq.close();
aq?.close();
cb && cb();
}
_write(x: any, _: string, cb: CB) {
const aq = this._asyncQueue;
aq && aq.write(x);
aq?.write(x);
cb && cb();
return true;
}
Expand Down Expand Up @@ -77,7 +77,7 @@ class RecordBatchReaderDuplex<T extends { [key: string]: DataType } = any> exten
while (this.readable && !(r = await reader.next()).done) {
if (!this.push(r.value) || (size != null && --size <= 0)) { break; }
}
if (!this.readable || (r && r.done && (reader.autoDestroy || (await reader.reset().open()).closed))) {
if (!this.readable || (r?.done && (reader.autoDestroy || (await reader.reset().open()).closed))) {
this.push(null);
await reader.cancel();
}
Expand Down
6 changes: 3 additions & 3 deletions js/src/io/node/writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ class RecordBatchWriterDuplex<T extends { [key: string]: DataType } = any> exten
}
_final(cb?: CB) {
const writer = this._writer;
writer && writer.close();
writer?.close();
cb && cb();
}
_write(x: any, _: string, cb: CB) {
const writer = this._writer;
writer && writer.write(x);
writer?.write(x);
cb && cb();
return true;
}
Expand All @@ -68,7 +68,7 @@ class RecordBatchWriterDuplex<T extends { [key: string]: DataType } = any> exten
}
if (!this.push(r.value) || size <= 0) { break; }
}
if ((r && r.done || !this.readable)) {
if ((r?.done || !this.readable)) {
this.push(null);
await reader.cancel();
}
Expand Down
12 changes: 6 additions & 6 deletions js/src/io/whatwg/iterable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ export function toDOMStream<T>(source: Iterable<T> | AsyncIterable<T>, options?:
function iterableAsReadableDOMStream<T>(source: Iterable<T>, options?: ReadableDOMStreamOptions) {

let it: SourceIterator<T> | null = null;
const bm = (options && options.type === 'bytes') || false;
const hwm = options && options.highWaterMark || (2 ** 24);
const bm = (options?.type === 'bytes') || false;
const hwm = options?.highWaterMark || (2 ** 24);

return new ReadableStream<T>({
...options as any,
start(controller) { next(controller, it || (it = source[Symbol.iterator]() as SourceIterator<T>)); },
pull(controller) { it ? (next(controller, it)) : controller.close(); },
cancel() { (it && (it.return && it.return()) || true) && (it = null); }
cancel() { (it?.return && it.return() || true) && (it = null); }
}, { highWaterMark: bm ? hwm : undefined, ...options });

function next(controller: ReadableStreamDefaultController<T>, it: SourceIterator<T>) {
Expand All @@ -66,14 +66,14 @@ function iterableAsReadableDOMStream<T>(source: Iterable<T>, options?: ReadableD
function asyncIterableAsReadableDOMStream<T>(source: AsyncIterable<T>, options?: ReadableDOMStreamOptions) {

let it: AsyncSourceIterator<T> | null = null;
const bm = (options && options.type === 'bytes') || false;
const hwm = options && options.highWaterMark || (2 ** 24);
const bm = (options?.type === 'bytes') || false;
const hwm = options?.highWaterMark || (2 ** 24);

return new ReadableStream<T>({
...options as any,
async start(controller) { await next(controller, it || (it = source[Symbol.asyncIterator]() as AsyncSourceIterator<T>)); },
async pull(controller) { it ? (await next(controller, it)) : controller.close(); },
async cancel() { (it && (it.return && await it.return()) || true) && (it = null); },
async cancel() { (it?.return && await it.return() || true) && (it = null); },
}, { highWaterMark: bm ? hwm : undefined, ...options });

async function next(controller: ReadableStreamDefaultController<T>, it: AsyncSourceIterator<T>) {
Expand Down
10 changes: 5 additions & 5 deletions js/src/ipc/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export class MessageReader implements IterableIterator<Message> {
public readSchema(throwIfNull = false) {
const type = MessageHeader.Schema;
const message = this.readMessage(type);
const schema = message && message.header();
const schema = message?.header();
if (throwIfNull && !schema) {
throw new Error(nullMessage(type));
}
Expand All @@ -81,7 +81,7 @@ export class MessageReader implements IterableIterator<Message> {
protected readMetadataLength(): IteratorResult<number> {
const buf = this.source.read(PADDING);
const bb = buf && new ByteBuffer(buf);
const len = bb && bb.readInt32(0) || 0;
const len = bb?.readInt32(0) || 0;
return { done: len === 0, value: len };
}
protected readMetadata(metadataLength: number): IteratorResult<Message> {
Expand Down Expand Up @@ -141,7 +141,7 @@ export class AsyncMessageReader implements AsyncIterableIterator<Message> {
public async readSchema(throwIfNull = false) {
const type = MessageHeader.Schema;
const message = await this.readMessage(type);
const schema = message && message.header();
const schema = message?.header();
if (throwIfNull && !schema) {
throw new Error(nullMessage(type));
}
Expand All @@ -150,7 +150,7 @@ export class AsyncMessageReader implements AsyncIterableIterator<Message> {
protected async readMetadataLength(): Promise<IteratorResult<number>> {
const buf = await this.source.read(PADDING);
const bb = buf && new ByteBuffer(buf);
const len = bb && bb.readInt32(0) || 0;
const len = bb?.readInt32(0) || 0;
return { done: len === 0, value: len };
}
protected async readMetadata(metadataLength: number): Promise<IteratorResult<Message>> {
Expand Down Expand Up @@ -220,7 +220,7 @@ export class JSONMessageReader extends MessageReader {
public readSchema() {
const type = MessageHeader.Schema;
const message = this.readMessage(type);
const schema = message && message.header();
const schema = message?.header();
if (!message || !schema) {
throw new Error(nullMessage(type));
}
Expand Down
8 changes: 4 additions & 4 deletions js/src/ipc/reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ class RecordBatchFileReaderImpl<T extends { [key: string]: DataType } = any> ext
const block = this._footer && this._footer.getRecordBatch(index);
if (block && this._handle.seek(block.offset)) {
const message = this._reader.readMessage(MessageHeader.RecordBatch);
if (message && message.isRecordBatch()) {
if (message?.isRecordBatch()) {
const header = message.header();
const buffer = this._reader.readMessageBody(message.bodyLength);
const recordBatch = this._loadRecordBatch(header, buffer);
Expand All @@ -560,7 +560,7 @@ class RecordBatchFileReaderImpl<T extends { [key: string]: DataType } = any> ext
const block = this._footer && this._footer.getDictionaryBatch(index);
if (block && this._handle.seek(block.offset)) {
const message = this._reader.readMessage(MessageHeader.DictionaryBatch);
if (message && message.isDictionaryBatch()) {
if (message?.isDictionaryBatch()) {
const header = message.header();
const buffer = this._reader.readMessageBody(message.bodyLength);
const vector = this._loadDictionaryBatch(header, buffer);
Expand Down Expand Up @@ -621,7 +621,7 @@ class AsyncRecordBatchFileReaderImpl<T extends { [key: string]: DataType } = any
const block = this._footer && this._footer.getRecordBatch(index);
if (block && (await this._handle.seek(block.offset))) {
const message = await this._reader.readMessage(MessageHeader.RecordBatch);
if (message && message.isRecordBatch()) {
if (message?.isRecordBatch()) {
const header = message.header();
const buffer = await this._reader.readMessageBody(message.bodyLength);
const recordBatch = this._loadRecordBatch(header, buffer);
Expand All @@ -634,7 +634,7 @@ class AsyncRecordBatchFileReaderImpl<T extends { [key: string]: DataType } = any
const block = this._footer && this._footer.getDictionaryBatch(index);
if (block && (await this._handle.seek(block.offset))) {
const message = await this._reader.readMessage(MessageHeader.DictionaryBatch);
if (message && message.isDictionaryBatch()) {
if (message?.isDictionaryBatch()) {
const header = message.header();
const buffer = await this._reader.readMessageBody(message.bodyLength);
const vector = this._loadDictionaryBatch(header, buffer);
Expand Down
Loading