Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 2 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"tstl": "./dist/tstl.js"
},
"engines": {
"node": ">=8.5.0"
"node": ">=12.13.0"
},
"dependencies": {
"resolve": "^1.13.1",
Expand Down
5 changes: 2 additions & 3 deletions src/transformation/context/context.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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[]);
}
Expand Down
3 changes: 1 addition & 2 deletions src/transformation/utils/annotations.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as ts from "typescript";
import { flatMap } from "../../utils";
import { TransformationContext } from "../context";
import { findFirstNodeAbove, inferAssignedType } from "./typescript";

Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions src/transformation/utils/function-context.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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)) &&
Expand Down
3 changes: 1 addition & 2 deletions src/transformation/utils/typescript/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as ts from "typescript";
import { flatMap } from "../../../utils";
import { TransformationContext } from "../../context";
import { isDeclaration } from "./nodes";

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions src/transformation/visitors/loops/for.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -11,7 +10,7 @@ export const transformForStatement: FunctionVisitor<ts.ForStatement> = (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)));
}
Expand Down
4 changes: 2 additions & 2 deletions src/transformation/visitors/variable-declaration.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -223,4 +223,4 @@ export function transformVariableDeclaration(
}

export const transformVariableStatement: FunctionVisitor<ts.VariableStatement> = (node, context) =>
flatMap(node.declarationList.declarations, declaration => transformVariableDeclaration(context, declaration));
node.declarationList.declarations.flatMap(declaration => transformVariableDeclaration(context, declaration));
15 changes: 0 additions & 15 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,6 @@ export function formatPathToLuaPath(filePath: string): string {
return filePath.replace(/\.\//g, "").replace(/\//g, ".");
}

export function flatMap<T, U>(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][T extends any ? 0 : never];

export function getOrUpdate<K, V>(
Expand Down
4 changes: 2 additions & 2 deletions test/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"target": "es2017",
"lib": ["es2017"],
"target": "es2019",
"lib": ["es2019"],
"types": ["node", "jest"],
"experimentalDecorators": true,

Expand Down
36 changes: 17 additions & 19 deletions test/unit/builtins/array.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: <T>(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: <T>(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: <T>(v: T) => v },
{ array: [1, 2, 3], map: (v: number) => ({ a: v * 2 }) },
{ array: [1, [2, 3], [4]], map: <T>(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 => {
Expand Down
3 changes: 1 addition & 2 deletions test/unit/builtins/numbers.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { flatMap } from "../../../src/utils";
import * as util from "../../util";

test.each([
Expand Down Expand Up @@ -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();
Expand Down
16 changes: 6 additions & 10 deletions test/unit/builtins/object.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
);
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"outDir": "./dist",
"declaration": true,
"sourceMap": true,
"target": "es2017",
"lib": ["es2017"],
"target": "es2019",
"lib": ["es2019"],
"module": "commonjs",
"stripInternal": true
},
Expand Down