From 939b79f5f38e48d9536fe5ed75dd96b969e6a0ed Mon Sep 17 00:00:00 2001 From: ark120202 Date: Mon, 9 Dec 2019 12:08:24 +0000 Subject: [PATCH] Use Node.js 12 features --- .github/workflows/ci.yml | 6 ++-- .github/workflows/release.yml | 1 - package.json | 2 +- src/transformation/context/context.ts | 5 ++- src/transformation/utils/annotations.ts | 3 +- src/transformation/utils/function-context.ts | 3 +- src/transformation/utils/typescript/index.ts | 3 +- .../destructuring-assignments.ts | 3 +- src/transformation/visitors/loops/for.ts | 3 +- .../visitors/variable-declaration.ts | 4 +-- src/utils.ts | 15 -------- test/tsconfig.json | 4 +-- test/unit/builtins/array.spec.ts | 36 +++++++++---------- test/unit/builtins/numbers.spec.ts | 3 +- test/unit/builtins/object.spec.ts | 16 ++++----- tsconfig.json | 4 +-- 16 files changed, 40 insertions(+), 71 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 95cb25965..35c7ec129 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -13,7 +13,6 @@ jobs: steps: - uses: actions/checkout@v1 - uses: actions/setup-node@v1 - - run: npm install --global npm@6 - run: npm ci - run: npm run lint env: @@ -28,11 +27,10 @@ jobs: steps: - uses: actions/checkout@v1 - - name: Use Node.js 8.5.0 + - name: Use Node.js 12.13.1 uses: actions/setup-node@v1 with: - node-version: 8.5.0 - - run: npm install --global npm@6 + node-version: 12.13.1 - run: npm ci - run: npm run build - run: npx jest --maxWorkers 2 --coverage diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 55f5b96f5..e3edaabbe 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -14,7 +14,6 @@ jobs: - uses: actions/setup-node@v1 with: registry-url: "https://registry.npmjs.org" - - run: npm install --global npm@6 - run: npm ci - run: npm run build - run: npm publish diff --git a/package.json b/package.json index 1f58ae98a..a7f6ba498 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,7 @@ "tstl": "./dist/tstl.js" }, "engines": { - "node": ">=8.5.0" + "node": ">=12.13.0" }, "dependencies": { "resolve": "^1.13.1", diff --git a/src/transformation/context/context.ts b/src/transformation/context/context.ts index 5d9a7ef22..07d16f143 100644 --- a/src/transformation/context/context.ts +++ b/src/transformation/context/context.ts @@ -1,7 +1,6 @@ import * as ts from "typescript"; import { CompilerOptions, LuaTarget } from "../../CompilerOptions"; import * as lua from "../../LuaAST"; -import { flatMap } from "../../utils"; import { unwrapVisitorResult } from "../utils/lua-ast"; import { isFileModule } from "../utils/typescript"; import { ExpressionLikeNode, ObjectVisitor, StatementLikeNode, VisitorMap } from "./visitors"; @@ -81,14 +80,14 @@ export class TransformationContext { public transformStatements(node: StatementLikeNode | readonly StatementLikeNode[]): lua.Statement[] { return Array.isArray(node) - ? flatMap(node, n => this.transformStatements(n)) + ? node.flatMap(n => this.transformStatements(n)) : // TODO: https://github.com/microsoft/TypeScript/pull/28916 (this.transformNode(node as StatementLikeNode) as lua.Statement[]); } public superTransformStatements(node: StatementLikeNode | readonly StatementLikeNode[]): lua.Statement[] { return Array.isArray(node) - ? flatMap(node, n => this.superTransformStatements(n)) + ? node.flatMap(n => this.superTransformStatements(n)) : // TODO: https://github.com/microsoft/TypeScript/pull/28916 (this.superTransformNode(node as StatementLikeNode) as lua.Statement[]); } diff --git a/src/transformation/utils/annotations.ts b/src/transformation/utils/annotations.ts index 985a4ec26..8c7f4ea12 100644 --- a/src/transformation/utils/annotations.ts +++ b/src/transformation/utils/annotations.ts @@ -1,5 +1,4 @@ import * as ts from "typescript"; -import { flatMap } from "../../utils"; import { TransformationContext } from "../context"; import { findFirstNodeAbove, inferAssignedType } from "./typescript"; @@ -102,7 +101,7 @@ export function getFileAnnotations(sourceFile: ts.SourceFile): AnnotationsMap { // Manually collect jsDoc because `getJSDocTags` includes tags only from closest comment const jsDoc = sourceFile.statements[0].jsDoc; if (jsDoc) { - for (const tag of flatMap(jsDoc, x => x.tags ?? [])) { + for (const tag of jsDoc.flatMap(x => x.tags ?? [])) { const tagName = tag.tagName.text; const annotation = createAnnotation(tagName, tag.comment ? tag.comment.split(" ") : []); if (annotation) { diff --git a/src/transformation/utils/function-context.ts b/src/transformation/utils/function-context.ts index 84f19d17a..6a2113d6a 100644 --- a/src/transformation/utils/function-context.ts +++ b/src/transformation/utils/function-context.ts @@ -1,6 +1,5 @@ import * as ts from "typescript"; import { CompilerOptions } from "../../CompilerOptions"; -import { flatMap } from "../../utils"; import { TransformationContext } from "../context"; import { AnnotationKind, getFileAnnotations, getNodeAnnotations } from "./annotations"; import { findFirstNodeAbove, getAllCallSignatures, inferAssignedType } from "./typescript"; @@ -107,7 +106,7 @@ function getSignatureDeclarations( context: TransformationContext, signatures: readonly ts.Signature[] ): ts.SignatureDeclaration[] { - return flatMap(signatures, signature => { + return signatures.flatMap(signature => { const signatureDeclaration = signature.getDeclaration(); if ( (ts.isFunctionExpression(signatureDeclaration) || ts.isArrowFunction(signatureDeclaration)) && diff --git a/src/transformation/utils/typescript/index.ts b/src/transformation/utils/typescript/index.ts index b6b5e6a1a..3869a4635 100644 --- a/src/transformation/utils/typescript/index.ts +++ b/src/transformation/utils/typescript/index.ts @@ -1,5 +1,4 @@ import * as ts from "typescript"; -import { flatMap } from "../../../utils"; import { TransformationContext } from "../../context"; import { isDeclaration } from "./nodes"; @@ -90,7 +89,7 @@ export function inferAssignedType(context: TransformationContext, expression: ts } export function getAllCallSignatures(type: ts.Type): readonly ts.Signature[] { - return type.isUnion() ? flatMap(type.types, getAllCallSignatures) : type.getCallSignatures(); + return type.isUnion() ? type.types.flatMap(getAllCallSignatures) : type.getCallSignatures(); } // Returns true for expressions that may have effects when evaluated diff --git a/src/transformation/visitors/binary-expression/destructuring-assignments.ts b/src/transformation/visitors/binary-expression/destructuring-assignments.ts index cb89a3673..7ed1d92d7 100644 --- a/src/transformation/visitors/binary-expression/destructuring-assignments.ts +++ b/src/transformation/visitors/binary-expression/destructuring-assignments.ts @@ -1,6 +1,5 @@ import * as ts from "typescript"; import * as lua from "../../../LuaAST"; -import { flatMap } from "../../../utils"; import { TransformationContext } from "../../context"; import { UnsupportedKind } from "../../utils/errors"; import { LuaLibFeature, transformLuaLibFunction } from "../../utils/lualib"; @@ -57,7 +56,7 @@ function transformArrayLiteralAssignmentPattern( node: ts.ArrayLiteralExpression, root: lua.Expression ): lua.Statement[] { - return flatMap(node.elements, (element, index) => { + return node.elements.flatMap((element, index) => { const indexedRoot = lua.createTableIndexExpression(root, lua.createNumericLiteral(index + 1), element); switch (element.kind) { diff --git a/src/transformation/visitors/loops/for.ts b/src/transformation/visitors/loops/for.ts index 6d167ce03..517852fdf 100644 --- a/src/transformation/visitors/loops/for.ts +++ b/src/transformation/visitors/loops/for.ts @@ -1,6 +1,5 @@ import * as ts from "typescript"; import * as lua from "../../../LuaAST"; -import { flatMap } from "../../../utils"; import { FunctionVisitor } from "../../context"; import { transformVariableDeclaration } from "../variable-declaration"; import { transformLoopBody } from "./body"; @@ -11,7 +10,7 @@ export const transformForStatement: FunctionVisitor = (statemen if (statement.initializer) { if (ts.isVariableDeclarationList(statement.initializer)) { // local initializer = value - result.push(...flatMap(statement.initializer.declarations, d => transformVariableDeclaration(context, d))); + result.push(...statement.initializer.declarations.flatMap(d => transformVariableDeclaration(context, d))); } else { result.push(...context.transformStatements(ts.createExpressionStatement(statement.initializer))); } diff --git a/src/transformation/visitors/variable-declaration.ts b/src/transformation/visitors/variable-declaration.ts index 074198af1..9b932eef3 100644 --- a/src/transformation/visitors/variable-declaration.ts +++ b/src/transformation/visitors/variable-declaration.ts @@ -1,6 +1,6 @@ import * as ts from "typescript"; import * as lua from "../../LuaAST"; -import { assertNever, flatMap } from "../../utils"; +import { assertNever } from "../../utils"; import { FunctionVisitor, TransformationContext } from "../context"; import { isTupleReturnCall } from "../utils/annotations"; import { validateAssignment } from "../utils/assignment-validation"; @@ -223,4 +223,4 @@ export function transformVariableDeclaration( } export const transformVariableStatement: FunctionVisitor = (node, context) => - flatMap(node.declarationList.declarations, declaration => transformVariableDeclaration(context, declaration)); + node.declarationList.declarations.flatMap(declaration => transformVariableDeclaration(context, declaration)); diff --git a/src/utils.ts b/src/utils.ts index 7b29b553a..6baad413a 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -12,21 +12,6 @@ export function formatPathToLuaPath(filePath: string): string { return filePath.replace(/\.\//g, "").replace(/\//g, "."); } -export function flatMap(array: readonly T[], callback: (value: T, index: number) => U | readonly U[]): U[] { - const result: U[] = []; - - for (const [index, value] of array.entries()) { - const mappedValue = callback(value, index); - if (Array.isArray(mappedValue)) { - result.push(...mappedValue); - } else { - result[result.length] = mappedValue as U; - } - } - - return result; -} - type NoInfer = [T][T extends any ? 0 : never]; export function getOrUpdate( diff --git a/test/tsconfig.json b/test/tsconfig.json index 63542f392..ecdd3263e 100644 --- a/test/tsconfig.json +++ b/test/tsconfig.json @@ -5,8 +5,8 @@ "strict": true, "noUnusedLocals": true, "noUnusedParameters": true, - "target": "es2017", - "lib": ["es2017"], + "target": "es2019", + "lib": ["es2019"], "types": ["node", "jest"], "experimentalDecorators": true, diff --git a/test/unit/builtins/array.spec.ts b/test/unit/builtins/array.spec.ts index 6c439a920..463d0d1dc 100644 --- a/test/unit/builtins/array.spec.ts +++ b/test/unit/builtins/array.spec.ts @@ -488,28 +488,26 @@ test.each([ }); test.each([ - { array: [[]], expected: [] }, - { array: [{ a: 1 }, { a: 2 }, { a: 3 }], expected: [{ a: 1 }, { a: 2 }, { a: 3 }] }, - { array: [1, [2, 3], 4], expected: [1, 2, 3, 4] }, - { array: [1, [2, 3], 4], depth: 0, expected: [1, [2, 3], 4] }, - { array: [1, [[2], [3]], 4], expected: [1, [2], [3], 4] }, - { array: [1, [[[2], [3]]], 4], depth: Infinity, expected: [1, 2, 3, 4] }, -])("array.flat (%p)", ({ array, depth, expected }) => { - // TODO: Node 12 - util.testExpressionTemplate`${array}.flat(${depth})`.expectToEqual(expected); + { array: [[]] }, + { array: [{ a: 1 }, { a: 2 }, { a: 3 }] }, + { array: [1, [2, 3], 4] }, + { array: [1, [2, 3], 4], depth: 0 }, + { array: [1, [[2], [3]], 4] }, + { array: [1, [[[2], [3]]], 4], depth: Infinity }, +])("array.flat (%p)", ({ array, depth }) => { + util.testExpressionTemplate`${array}.flat(${depth})`.expectToMatchJsResult(); }); test.each([ - { array: [[]], map: (v: T) => v, expected: [] }, - { array: [1, 2, 3], map: (v: number) => ({ a: v * 2 }), expected: [{ a: 2 }, { a: 4 }, { a: 6 }] }, - { array: [1, [2, 3], [4]], map: (value: T) => value, expected: [1, 2, 3, 4] }, - { array: [1, 2, 3], map: (v: number) => v * 2, expected: [2, 4, 6] }, - { array: [1, 2, 3], map: (v: number) => [v, v * 2], expected: [1, 2, 2, 4, 3, 6] }, - { array: [1, 2, 3], map: (v: number) => [v, [v]], expected: [1, [1], 2, [2], 3, [3]] }, - { array: [1, 2, 3], map: (v: number, i: number) => [v * 2 * i], expected: [0, 4, 12] }, -])("array.flatMap (%p)", ({ array, map, expected }) => { - // TODO: Node 12 - util.testExpressionTemplate`${array}.flatMap(${map})`.expectToEqual(expected); + { array: [[]], map: (v: T) => v }, + { array: [1, 2, 3], map: (v: number) => ({ a: v * 2 }) }, + { array: [1, [2, 3], [4]], map: (value: T) => value }, + { array: [1, 2, 3], map: (v: number) => v * 2 }, + { array: [1, 2, 3], map: (v: number) => [v, v * 2] }, + { array: [1, 2, 3], map: (v: number) => [v, [v]] }, + { array: [1, 2, 3], map: (v: number, i: number) => [v * 2 * i] }, +])("array.flatMap (%p)", ({ array, map }) => { + util.testExpressionTemplate`${array}.flatMap(${map})`.expectToMatchJsResult(); }); describe.each(["reduce", "reduceRight"])("array.%s", reduce => { diff --git a/test/unit/builtins/numbers.spec.ts b/test/unit/builtins/numbers.spec.ts index 7a5d5c731..94334376c 100644 --- a/test/unit/builtins/numbers.spec.ts +++ b/test/unit/builtins/numbers.spec.ts @@ -1,4 +1,3 @@ -import { flatMap } from "../../../src/utils"; import * as util from "../../util"; test.each([ @@ -51,7 +50,7 @@ describe("Number", () => { const toStringRadixes = [undefined, 10, 2, 8, 9, 16, 17, 36, 36.9]; const toStringValues = [-1, 0, 1, 1.5, 1024, 1.2]; -const toStringPairs = flatMap(toStringValues, value => toStringRadixes.map(radix => [value, radix] as const)); +const toStringPairs = toStringValues.flatMap(value => toStringRadixes.map(radix => [value, radix] as const)); test.each(toStringPairs)("(%p).toString(%p)", (value, radix) => { util.testExpressionTemplate`(${value}).toString(${radix})`.expectToMatchJsResult(); diff --git a/test/unit/builtins/object.spec.ts b/test/unit/builtins/object.spec.ts index 5e0a973a4..067da81f6 100644 --- a/test/unit/builtins/object.spec.ts +++ b/test/unit/builtins/object.spec.ts @@ -22,13 +22,9 @@ test.each([{}, { abc: "def" }, { abc: 3, def: "xyz" }])("Object.values (%p)", ob util.testExpressionTemplate`Object.values(${obj})`.expectToMatchJsResult(); }); -// TODO: Jest 25: as const -test.each<[string, object]>([ - ["[]", []], - ['[["a", 1], ["b", 2]]', { a: 1, b: 2 }], - ['[["a", 1], ["a", 2]]', { a: 2 }], - ['new Map([["foo", "bar"]])', { foo: "bar" }], -])("Object.fromEntries(%s)", (entries, expected) => { - // TODO: Node 12 - util.testExpression`Object.fromEntries(${entries})`.expectToEqual(expected); -}); +test.each(["[]", '[["a", 1], ["b", 2]]', '[["a", 1], ["a", 2]]', 'new Map([["foo", "bar"]])'])( + "Object.fromEntries(%s)", + entries => { + util.testExpression`Object.fromEntries(${entries})`.expectToMatchJsResult(); + } +); diff --git a/tsconfig.json b/tsconfig.json index 8f1275d19..d16784b37 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -7,8 +7,8 @@ "outDir": "./dist", "declaration": true, "sourceMap": true, - "target": "es2017", - "lib": ["es2017"], + "target": "es2019", + "lib": ["es2019"], "module": "commonjs", "stripInternal": true },