From 759090a3aabc744f81883bb0d39929ecc740258c Mon Sep 17 00:00:00 2001 From: Lorenz Junglas Date: Sun, 7 Mar 2021 17:48:31 +0100 Subject: [PATCH 1/5] Bump lua-wasm-bindings version (#1002) Fixed an issue were errors would be printed for 5.1 tests even though the tests passed. --- package-lock.json | 14 +++++++------- package.json | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/package-lock.json b/package-lock.json index e04a792e3..8dbf1d54d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -32,7 +32,7 @@ "jest": "^26.0.1", "jest-circus": "^25.1.0", "lua-types": "^2.8.0", - "lua-wasm-bindings": "^0.2.1", + "lua-wasm-bindings": "^0.2.2", "prettier": "^2.0.5", "ts-jest": "^26.3.0", "ts-node": "^8.6.2" @@ -8795,9 +8795,9 @@ "dev": true }, "node_modules/lua-wasm-bindings": { - "version": "0.2.1", - "resolved": "https://registry.npmjs.org/lua-wasm-bindings/-/lua-wasm-bindings-0.2.1.tgz", - "integrity": "sha512-8oig7XmwI/Tq/hSpuBZbjyBXrw3vngUSvQ5kV3/9mGaHfJrQVd993j1HVy6CN/griuPrKsLtSEbpn8Hu79dYhg==", + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/lua-wasm-bindings/-/lua-wasm-bindings-0.2.2.tgz", + "integrity": "sha512-Z2v3An8jEvYbhUJ/gYxNd65ZBbaRyPMmUaleDoOhzJSIGrMgjURvy1EtHtgRGTzdtpmKtHA+YYKDrSZBE9g/ig==", "dev": true }, "node_modules/make-dir": { @@ -18828,9 +18828,9 @@ "dev": true }, "lua-wasm-bindings": { - "version": "0.2.1", - "resolved": "https://registry.npmjs.org/lua-wasm-bindings/-/lua-wasm-bindings-0.2.1.tgz", - "integrity": "sha512-8oig7XmwI/Tq/hSpuBZbjyBXrw3vngUSvQ5kV3/9mGaHfJrQVd993j1HVy6CN/griuPrKsLtSEbpn8Hu79dYhg==", + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/lua-wasm-bindings/-/lua-wasm-bindings-0.2.2.tgz", + "integrity": "sha512-Z2v3An8jEvYbhUJ/gYxNd65ZBbaRyPMmUaleDoOhzJSIGrMgjURvy1EtHtgRGTzdtpmKtHA+YYKDrSZBE9g/ig==", "dev": true }, "make-dir": { diff --git a/package.json b/package.json index 02a0c665a..677789323 100644 --- a/package.json +++ b/package.json @@ -61,7 +61,7 @@ "jest": "^26.0.1", "jest-circus": "^25.1.0", "lua-types": "^2.8.0", - "lua-wasm-bindings": "^0.2.1", + "lua-wasm-bindings": "^0.2.2", "prettier": "^2.0.5", "ts-jest": "^26.3.0", "ts-node": "^8.6.2" From 29dba98fe906972f2c29a5d15954b44ed95c255f Mon Sep 17 00:00:00 2001 From: Lorenz Junglas Date: Thu, 25 Mar 2021 19:22:30 +0100 Subject: [PATCH 2/5] Replaced deprecated factory functions (#1008) * Replaced deprecated factory functions Also enabled some eslint rules & fixed violations * Caught to more deprecated function calls * Fixed typo --- .eslintrc.js | 51 +++++++++++-------- src/LuaLib.ts | 2 + src/LuaPrinter.ts | 2 +- .../visitors/binary-expression/compound.ts | 8 +-- .../visitors/binary-expression/index.ts | 6 +-- src/transformation/visitors/call.ts | 8 +-- src/transformation/visitors/class/index.ts | 4 +- src/transformation/visitors/literal.ts | 4 +- src/transformation/visitors/loops/for.ts | 4 +- src/transformation/visitors/loops/utils.ts | 2 +- src/transformation/visitors/modules/export.ts | 11 ++-- .../visitors/unary-expression.ts | 12 ++--- .../visitors/variable-declaration.ts | 2 +- src/transpilation/transformers.ts | 6 +-- test/cli/parse.spec.ts | 1 + test/transpile/transformers/fixtures.ts | 8 +-- .../transformers/transformers.spec.ts | 1 + .../invalidFunctionAssignments.spec.ts | 16 +++--- test/unit/hoisting.spec.ts | 4 +- 19 files changed, 85 insertions(+), 67 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index fb8ec7488..dabc1db41 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -130,6 +130,8 @@ module.exports = { "no-useless-return": ["error"], "import/no-default-export": "error", + // TODO currently only works for direct imports (useless for now) https://github.com/benmosher/eslint-plugin-import/issues/1729 + // "import/no-deprecated": "error", "jest/expect-expect": "off", "jest/consistent-test-it": ["error", { fn: "test", withinDescribe: "test" }], @@ -154,31 +156,33 @@ module.exports = { "@typescript-eslint/no-require-imports": "off", "@typescript-eslint/no-unnecessary-condition": "off", "@typescript-eslint/prefer-for-of": "error", - // TODO: https://github.com/typescript-eslint/typescript-eslint/issues/1265 - // "@typescript-eslint/prefer-nullish-coalescing": "error", + "@typescript-eslint/prefer-nullish-coalescing": "error", "@typescript-eslint/prefer-readonly": "off", "@typescript-eslint/quotes": ["error", "double", { avoidEscape: true, allowTemplateLiterals: false }], "@typescript-eslint/require-array-sort-compare": "off", "@typescript-eslint/camelcase": "off", - // TODO: https://github.com/typescript-eslint/typescript-eslint/issues/1712 - // "@typescript-eslint/naming-convention": [ - // "error", - // { - // selector: "default", - // format: ["camelCase"], - // leadingUnderscore: "allow", - // }, - // { - // selector: "variable", - // format: ["camelCase", "UPPER_CASE"], - // leadingUnderscore: "allow", - // }, - // { - // selector: "typeLike", - // format: ["PascalCase"], - // }, - // ], + "@typescript-eslint/naming-convention": [ + "error", + { + selector: "default", + format: ["camelCase"], + leadingUnderscore: "allow", + }, + { + selector: "variable", + format: ["camelCase", "UPPER_CASE"], + leadingUnderscore: "allow", + }, + { + selector: "typeLike", + format: ["PascalCase"], + }, + { + selector: "enumMember", + format: ["PascalCase"], + }, + ], }, }, { @@ -187,6 +191,13 @@ module.exports = { "no-restricted-syntax": ["error", "LabeledStatement", "SequenceExpression"], "@typescript-eslint/no-throw-literal": "off", "@typescript-eslint/prefer-optional-chain": "off", + "@typescript-eslint/naming-convention": "off", + }, + }, + { + files: "language-extensions/index.d.ts", + rules: { + "@typescript-eslint/naming-convention": "off", }, }, { diff --git a/src/LuaLib.ts b/src/LuaLib.ts index 7168d0067..06742022d 100644 --- a/src/LuaLib.ts +++ b/src/LuaLib.ts @@ -87,6 +87,7 @@ export enum LuaLibFeature { Unpack = "Unpack", } +/* eslint-disable @typescript-eslint/naming-convention */ const luaLibDependencies: Partial> = { ArrayConcat: [LuaLibFeature.ArrayIsArray], ArrayFlat: [LuaLibFeature.ArrayConcat, LuaLibFeature.ArrayIsArray], @@ -108,6 +109,7 @@ const luaLibDependencies: Partial> = { StringSplit: [LuaLibFeature.StringSubstring], SymbolRegistry: [LuaLibFeature.Symbol], }; +/* eslint-enable @typescript-eslint/naming-convention */ export function loadLuaLibFeatures(features: Iterable, emitHost: EmitHost): string { let result = ""; diff --git a/src/LuaPrinter.ts b/src/LuaPrinter.ts index 912ab33f5..b1cfb7f46 100644 --- a/src/LuaPrinter.ts +++ b/src/LuaPrinter.ts @@ -272,7 +272,7 @@ export class LuaPrinter { } } }); - return result || false; + return result ?? false; } protected printStatementArray(statements: lua.Statement[]): SourceChunk[] { diff --git a/src/transformation/visitors/binary-expression/compound.ts b/src/transformation/visitors/binary-expression/compound.ts index 7a11454c8..febd7681e 100644 --- a/src/transformation/visitors/binary-expression/compound.ts +++ b/src/transformation/visitors/binary-expression/compound.ts @@ -22,15 +22,15 @@ export function parseAccessExpressionWithEvaluationEffects( const type = context.checker.getTypeAtLocation(node.expression); if (isArrayType(context, type)) { // Offset arrays by one - const oneLit = ts.createNumericLiteral("1"); - const exp = ts.createParen(node.argumentExpression); - const addExp = ts.createBinary(exp, ts.SyntaxKind.PlusToken, oneLit); + const oneLit = ts.factory.createNumericLiteral("1"); + const exp = ts.factory.createParenthesizedExpression(node.argumentExpression); + const addExp = ts.factory.createBinaryExpression(exp, ts.SyntaxKind.PlusToken, oneLit); return [node.expression, addExp]; } else { return [node.expression, node.argumentExpression]; } } else if (ts.isPropertyAccessExpression(node) && isExpressionWithEvaluationEffect(node.expression)) { - return [node.expression, ts.createStringLiteral(node.name.text)]; + return [node.expression, ts.factory.createStringLiteral(node.name.text)]; } return []; diff --git a/src/transformation/visitors/binary-expression/index.ts b/src/transformation/visitors/binary-expression/index.ts index 26e90ad67..5ed023618 100644 --- a/src/transformation/visitors/binary-expression/index.ts +++ b/src/transformation/visitors/binary-expression/index.ts @@ -125,7 +125,7 @@ export const transformBinaryExpression: FunctionVisitor = ( return transformToImmediatelyInvokedFunctionExpression( context, () => ({ - statements: context.transformStatements(ts.createExpressionStatement(node.left)), + statements: context.transformStatements(ts.factory.createExpressionStatement(node.left)), result: context.transformExpression(node.right), }), node @@ -159,8 +159,8 @@ export function transformBinaryExpressionStatement( return transformAssignmentStatement(context, expression as ts.AssignmentExpression); } else if (operator === ts.SyntaxKind.CommaToken) { const statements = [ - ...context.transformStatements(ts.createExpressionStatement(expression.left)), - ...context.transformStatements(ts.createExpressionStatement(expression.right)), + ...context.transformStatements(ts.factory.createExpressionStatement(expression.left)), + ...context.transformStatements(ts.factory.createExpressionStatement(expression.right)), ]; return lua.createDoStatement(statements, expression); diff --git a/src/transformation/visitors/call.ts b/src/transformation/visitors/call.ts index 75f73db88..306968010 100644 --- a/src/transformation/visitors/call.ts +++ b/src/transformation/visitors/call.ts @@ -183,7 +183,7 @@ function transformPropertyCall(context: TransformationContext, node: PropertyCal if (node.expression.expression.kind === ts.SyntaxKind.SuperKeyword) { // Super calls take the format of super.call(self,...) - const parameters = transformArguments(context, node.arguments, signature, ts.createThis()); + const parameters = transformArguments(context, node.arguments, signature, ts.factory.createThis()); return lua.createCallExpression(context.transformExpression(node.expression), parameters); } @@ -267,11 +267,11 @@ export const transformCallExpression: FunctionVisitor = (node // Handle super calls properly if (node.expression.kind === ts.SyntaxKind.SuperKeyword) { - const parameters = transformArguments(context, node.arguments, signature, ts.createThis()); + const parameters = transformArguments(context, node.arguments, signature, ts.factory.createThis()); return lua.createCallExpression( lua.createTableIndexExpression( - context.transformExpression(ts.createSuper()), + context.transformExpression(ts.factory.createSuper()), lua.createStringLiteral("____constructor") ), parameters @@ -285,7 +285,7 @@ export const transformCallExpression: FunctionVisitor = (node if (signatureDeclaration && getDeclarationContextType(context, signatureDeclaration) === ContextType.Void) { parameters = transformArguments(context, node.arguments, signature); } else { - const callContext = context.isStrict ? ts.createNull() : ts.createIdentifier("_G"); + const callContext = context.isStrict ? ts.factory.createNull() : ts.factory.createIdentifier("_G"); parameters = transformArguments(context, node.arguments, signature, callContext); } diff --git a/src/transformation/visitors/class/index.ts b/src/transformation/visitors/class/index.ts index 9963c09f4..4c0e05eee 100644 --- a/src/transformation/visitors/class/index.ts +++ b/src/transformation/visitors/class/index.ts @@ -152,7 +152,7 @@ function transformClassLikeDeclaration( // Generate a constructor if none was defined in a base class const constructorResult = transformConstructorDeclaration( context, - ts.createConstructor([], [], [], ts.createBlock([], true)), + ts.factory.createConstructorDeclaration([], [], [], ts.factory.createBlock([], true)), localClassName, instanceFields, classDeclaration @@ -168,7 +168,7 @@ function transformClassLikeDeclaration( const superCall = lua.createExpressionStatement( lua.createCallExpression( lua.createTableIndexExpression( - context.transformExpression(ts.createSuper()), + context.transformExpression(ts.factory.createSuper()), lua.createStringLiteral("____constructor") ), [createSelfIdentifier(), lua.createDotsLiteral()] diff --git a/src/transformation/visitors/literal.ts b/src/transformation/visitors/literal.ts index 36c7e48c6..8fa85e4e4 100644 --- a/src/transformation/visitors/literal.ts +++ b/src/transformation/visitors/literal.ts @@ -38,7 +38,7 @@ export function createShorthandIdentifier( const name = isUnsafeName ? createSafeName(propertyName) : propertyName; - let identifier = context.transformExpression(ts.createIdentifier(name)); + let identifier = context.transformExpression(ts.factory.createIdentifier(name)); lua.setNodeOriginal(identifier, propertyIdentifier); if (valueSymbol !== undefined && lua.isIdentifier(identifier)) { identifier.symbolId = getSymbolIdOfSymbol(context, valueSymbol); @@ -137,7 +137,7 @@ const transformObjectLiteralExpression: FunctionVisitor = (expression, context) => { const filteredElements = expression.elements.map(e => - ts.isOmittedExpression(e) ? ts.createIdentifier("undefined") : e + ts.isOmittedExpression(e) ? ts.factory.createIdentifier("undefined") : e ); const values = flattenSpreadExpressions(context, filteredElements).map(e => lua.createTableFieldExpression(e)); diff --git a/src/transformation/visitors/loops/for.ts b/src/transformation/visitors/loops/for.ts index f81a62199..98864082e 100644 --- a/src/transformation/visitors/loops/for.ts +++ b/src/transformation/visitors/loops/for.ts @@ -13,7 +13,7 @@ export const transformForStatement: FunctionVisitor = (statemen // local initializer = value result.push(...statement.initializer.declarations.flatMap(d => transformVariableDeclaration(context, d))); } else { - result.push(...context.transformStatements(ts.createExpressionStatement(statement.initializer))); + result.push(...context.transformStatements(ts.factory.createExpressionStatement(statement.initializer))); } } @@ -25,7 +25,7 @@ export const transformForStatement: FunctionVisitor = (statemen const body: lua.Statement[] = transformLoopBody(context, statement); if (statement.incrementor) { - body.push(...context.transformStatements(ts.createExpressionStatement(statement.incrementor))); + body.push(...context.transformStatements(ts.factory.createExpressionStatement(statement.incrementor))); } // while (condition) do ... end diff --git a/src/transformation/visitors/loops/utils.ts b/src/transformation/visitors/loops/utils.ts index 8ee86f4ea..a7402b653 100644 --- a/src/transformation/visitors/loops/utils.ts +++ b/src/transformation/visitors/loops/utils.ts @@ -36,7 +36,7 @@ export function getVariableDeclarationBinding( checkVariableDeclarationList(context, node); if (node.declarations.length === 0) { - return ts.createIdentifier("____"); + return ts.factory.createIdentifier("____"); } return node.declarations[0].name; diff --git a/src/transformation/visitors/modules/export.ts b/src/transformation/visitors/modules/export.ts index 0fa5d9ee4..955f56866 100644 --- a/src/transformation/visitors/modules/export.ts +++ b/src/transformation/visitors/modules/export.ts @@ -129,12 +129,15 @@ function transformExportSpecifiersFrom( exportSpecifiers: ts.ExportSpecifier[] ): lua.Statement { // First transpile as import clause - const importClause = ts.createImportClause( + const importClause = ts.factory.createImportClause( + false, undefined, - ts.createNamedImports(exportSpecifiers.map(s => ts.createImportSpecifier(s.propertyName, s.name))) + ts.factory.createNamedImports( + exportSpecifiers.map(s => ts.factory.createImportSpecifier(s.propertyName, s.name)) + ) ); - const importDeclaration = ts.createImportDeclaration( + const importDeclaration = ts.factory.createImportDeclaration( statement.decorators, statement.modifiers, importClause, @@ -142,7 +145,7 @@ function transformExportSpecifiersFrom( ); // Wrap in block to prevent imports from hoisting out of `do` statement - const [block] = transformScopeBlock(context, ts.createBlock([importDeclaration]), ScopeType.Block); + const [block] = transformScopeBlock(context, ts.factory.createBlock([importDeclaration]), ScopeType.Block); const result = block.statements; // Now the module is imported, add the imports to the export table diff --git a/src/transformation/visitors/unary-expression.ts b/src/transformation/visitors/unary-expression.ts index 76b0e4941..5960c2200 100644 --- a/src/transformation/visitors/unary-expression.ts +++ b/src/transformation/visitors/unary-expression.ts @@ -25,7 +25,7 @@ export function transformUnaryExpressionStatement( context, expression, expression.operand, - ts.createLiteral(1), + ts.factory.createNumericLiteral(1), replacementOperator ); } else if (ts.isPostfixUnaryExpression(expression)) { @@ -37,7 +37,7 @@ export function transformUnaryExpressionStatement( context, expression, expression.operand, - ts.createLiteral(1), + ts.factory.createNumericLiteral(1), replacementOperator ); } @@ -50,7 +50,7 @@ export const transformPostfixUnaryExpression: FunctionVisitor = () => node => { const transformSourceFile: ts.Transformer = node => { - const empty = ts.createNotEmittedStatement(undefined!); + const empty = ts.factory.createNotEmittedStatement(undefined!); ts.addSyntheticLeadingComment(empty, ts.SyntaxKind.MultiLineCommentTrivia, "* @noSelfInFile ", true); - return ts.updateSourceFileNode(node, [empty, ...node.statements], node.isDeclarationFile); + return ts.factory.updateSourceFile(node, [empty, ...node.statements], node.isDeclarationFile); }; return ts.isBundle(node) - ? ts.updateBundle(node, node.sourceFiles.map(transformSourceFile)) + ? ts.factory.updateBundle(node, node.sourceFiles.map(transformSourceFile)) : transformSourceFile(node); }; diff --git a/test/cli/parse.spec.ts b/test/cli/parse.spec.ts index cb441ca2f..06ed05f75 100644 --- a/test/cli/parse.spec.ts +++ b/test/cli/parse.spec.ts @@ -158,6 +158,7 @@ describe("tsconfig", () => { }); test("should parse options case-sensitively", () => { + // eslint-disable-next-line @typescript-eslint/naming-convention const result = parseConfigFileContent({ tstl: { NoHeader: true } }); expect(result.errors).toHaveDiagnostics(); diff --git a/test/transpile/transformers/fixtures.ts b/test/transpile/transformers/fixtures.ts index c998d6efc..a69829d5d 100644 --- a/test/transpile/transformers/fixtures.ts +++ b/test/transpile/transformers/fixtures.ts @@ -9,7 +9,7 @@ export const program = (program: ts.Program, options: { value: any }): ts.Transf export const config = ({ value }: { value: any }): ts.TransformerFactory => context => file => visitAndReplace(context, file, node => { if (!ts.isReturnStatement(node)) return; - return ts.updateReturn(node, ts.createLiteral(value)); + return ts.factory.updateReturnStatement(node, value ? ts.factory.createTrue() : ts.factory.createFalse()); }); export const checker = ( @@ -20,13 +20,13 @@ export const checker = ( if (!ts.isReturnStatement(node) || !node.expression) return; const type = checker.getTypeAtLocation(node.expression); if ((type.flags & ts.TypeFlags.BooleanLiteral) === 0) return; - return ts.updateReturn(node, ts.createLiteral(value)); + return ts.factory.updateReturnStatement(node, value ? ts.factory.createTrue() : ts.factory.createFalse()); }); export const raw: ts.TransformerFactory = context => file => visitAndReplace(context, file, node => { if (!ts.isReturnStatement(node)) return; - return ts.updateReturn(node, ts.createLiteral(true)); + return ts.factory.updateReturnStatement(node, ts.factory.createTrue()); }); export const compilerOptions = ( @@ -35,6 +35,6 @@ export const compilerOptions = ( assert(options.plugins?.length === 1); return visitAndReplace(context, file, node => { if (!ts.isReturnStatement(node)) return; - return ts.updateReturn(node, ts.createLiteral(true)); + return ts.factory.updateReturnStatement(node, ts.factory.createTrue()); }); }; diff --git a/test/transpile/transformers/transformers.spec.ts b/test/transpile/transformers/transformers.spec.ts index e166198bd..02290d9d1 100644 --- a/test/transpile/transformers/transformers.spec.ts +++ b/test/transpile/transformers/transformers.spec.ts @@ -23,6 +23,7 @@ test("transformer resolution error", () => { .expectToHaveDiagnostics(); }); +// This tests plugin transformers by transforming the `return false` statement to a `return true` statement. describe("factory types", () => { test.each(["program", "config", "checker", "raw", "compilerOptions"] as const)("%s", type => { util.testFunction` diff --git a/test/unit/functions/validation/invalidFunctionAssignments.spec.ts b/test/unit/functions/validation/invalidFunctionAssignments.spec.ts index 7d678496b..cd12b4ba8 100644 --- a/test/unit/functions/validation/invalidFunctionAssignments.spec.ts +++ b/test/unit/functions/validation/invalidFunctionAssignments.spec.ts @@ -10,7 +10,7 @@ test.each(invalidTestFunctionAssignments)( "Invalid function variable declaration (%p)", (testFunction, functionType, isSelfConversion) => { util.testModule` - ${testFunction.definition || ""} + ${testFunction.definition ?? ""} const fn: ${functionType} = ${testFunction.value}; `.expectDiagnosticsToMatchSnapshot( [isSelfConversion ? unsupportedSelfFunctionConversion.code : unsupportedNoSelfFunctionConversion.code], @@ -23,7 +23,7 @@ test.each(invalidTestFunctionAssignments)( "Invalid function assignment (%p)", (testFunction, functionType, isSelfConversion) => { util.testModule` - ${testFunction.definition || ""} + ${testFunction.definition ?? ""} let fn: ${functionType}; fn = ${testFunction.value}; `.expectDiagnosticsToMatchSnapshot( @@ -35,7 +35,7 @@ test.each(invalidTestFunctionAssignments)( test.each(invalidTestFunctionCasts)("Invalid function assignment with cast (%p)", (testFunction, castedFunction) => { util.testModule` - ${testFunction.definition || ""} + ${testFunction.definition ?? ""} let fn: typeof ${testFunction.value}; fn = ${castedFunction}; `.expectDiagnosticsToMatchSnapshot( @@ -48,7 +48,7 @@ test.each(invalidTestFunctionAssignments)( "Invalid function argument (%p)", (testFunction, functionType, isSelfConversion) => { util.testModule` - ${testFunction.definition || ""} + ${testFunction.definition ?? ""} declare function takesFunction(fn: ${functionType}); takesFunction(${testFunction.value}); `.expectDiagnosticsToMatchSnapshot( @@ -68,7 +68,7 @@ test("Invalid lua lib function argument", () => { test.each(invalidTestFunctionCasts)("Invalid function argument with cast (%p)", (testFunction, castedFunction) => { util.testModule` - ${testFunction.definition || ""} + ${testFunction.definition ?? ""} declare function takesFunction(fn: typeof ${testFunction.value}); takesFunction(${castedFunction}); `.expectDiagnosticsToMatchSnapshot( @@ -81,7 +81,7 @@ test.each(invalidTestFunctionAssignments)( "Invalid function generic argument (%p)", (testFunction, functionType, isSelfConversion) => { util.testModule` - ${testFunction.definition || ""} + ${testFunction.definition ?? ""} declare function takesFunction(fn: T); takesFunction(${testFunction.value}); `.expectDiagnosticsToMatchSnapshot( @@ -95,7 +95,7 @@ test.each(invalidTestFunctionAssignments)( "Invalid function return (%p)", (testFunction, functionType, isSelfConversion) => { util.testModule` - ${testFunction.definition || ""} + ${testFunction.definition ?? ""} function returnsFunction(): ${functionType} { return ${testFunction.value}; } @@ -110,7 +110,7 @@ test.each(invalidTestFunctionCasts)( "Invalid function return with cast (%p)", (testFunction, castedFunction, isSelfConversion) => { util.testModule` - ${testFunction.definition || ""} + ${testFunction.definition ?? ""} function returnsFunction(): typeof ${testFunction.value} { return ${castedFunction}; } diff --git a/test/unit/hoisting.spec.ts b/test/unit/hoisting.spec.ts index 9caefb300..144460067 100644 --- a/test/unit/hoisting.spec.ts +++ b/test/unit/hoisting.spec.ts @@ -107,9 +107,9 @@ test("Hoisting with synthetic source file node", () => { .setCustomTransformers({ before: [ () => sourceFile => - ts.updateSourceFileNode( + ts.factory.updateSourceFile( sourceFile, - [ts.createNotEmittedStatement(undefined!), ...sourceFile.statements], + [ts.factory.createNotEmittedStatement(undefined!), ...sourceFile.statements], sourceFile.isDeclarationFile, sourceFile.referencedFiles, sourceFile.typeReferenceDirectives, From c09f98cd4ebcbc5bf07a662a11a7f771939dcac3 Mon Sep 17 00:00:00 2001 From: Perryvw Date: Thu, 25 Mar 2021 20:56:29 +0100 Subject: [PATCH 3/5] 0.39.1 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 8dbf1d54d..2c8f86ac9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "typescript-to-lua", - "version": "0.39.0", + "version": "0.39.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "typescript-to-lua", - "version": "0.39.0", + "version": "0.39.1", "license": "MIT", "dependencies": { "resolve": "^1.15.1", diff --git a/package.json b/package.json index 677789323..7a701c557 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "typescript-to-lua", - "version": "0.39.0", + "version": "0.39.1", "description": "A generic TypeScript to Lua transpiler. Write your code in TypeScript and publish Lua!", "repository": "https://github.com/TypeScriptToLua/TypeScriptToLua", "homepage": "https://typescripttolua.github.io/", From f3dea4271a4dae65cc2e4644ab44d2108a2094ce Mon Sep 17 00:00:00 2001 From: Perry van Wesel Date: Fri, 2 Apr 2021 18:41:08 +0200 Subject: [PATCH 4/5] Use ts.getParseTreeNode before getSourceFile to handle transformer output (#1010) * Use getParseTreeNode before getSourceFile * npm audit fix --- package-lock.json | 29 +++++++++++--------- src/LuaAST.ts | 8 ++++-- src/transformation/utils/typescript/index.ts | 3 +- 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/package-lock.json b/package-lock.json index 2c8f86ac9..72ffc8c45 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1460,6 +1460,7 @@ "jest-resolve": "^26.0.1", "jest-util": "^26.0.1", "jest-worker": "^26.0.0", + "node-notifier": "^7.0.0", "slash": "^3.0.0", "source-map": "^0.6.0", "string-length": "^4.0.1", @@ -3567,7 +3568,8 @@ "esprima": "^4.0.1", "estraverse": "^4.2.0", "esutils": "^2.0.2", - "optionator": "^0.8.1" + "optionator": "^0.8.1", + "source-map": "~0.6.1" }, "bin": { "escodegen": "bin/escodegen.js", @@ -6206,6 +6208,7 @@ "@types/graceful-fs": "^4.1.2", "anymatch": "^3.0.3", "fb-watchman": "^2.0.0", + "fsevents": "^2.1.2", "graceful-fs": "^4.2.4", "jest-serializer": "^26.0.0", "jest-util": "^26.0.1", @@ -8762,9 +8765,9 @@ } }, "node_modules/lodash": { - "version": "4.17.15", - "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz", - "integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==", + "version": "4.17.21", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", + "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==", "dev": true }, "node_modules/lodash.memoize": { @@ -11549,9 +11552,9 @@ "dev": true }, "node_modules/y18n": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/y18n/-/y18n-4.0.0.tgz", - "integrity": "sha512-r9S/ZyXu/Xu9q1tYlpsLIsa3EeLXXk0VwlxqTcFRfg9EhMW+17kbt9G0NrgCmhGb5vT2hyhJZLfDGx+7+5Uj/w==", + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/y18n/-/y18n-4.0.1.tgz", + "integrity": "sha512-wNcy4NvjMYL8gogWWYAO7ZFWFfHcbdbE57tZO8e4cbpj8tfUcwrwqSl3ad8HxpYWCdXcJUCeKKZS62Av1affwQ==", "dev": true }, "node_modules/yargs": { @@ -18795,9 +18798,9 @@ } }, "lodash": { - "version": "4.17.15", - "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz", - "integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==", + "version": "4.17.21", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", + "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==", "dev": true }, "lodash.memoize": { @@ -21039,9 +21042,9 @@ "dev": true }, "y18n": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/y18n/-/y18n-4.0.0.tgz", - "integrity": "sha512-r9S/ZyXu/Xu9q1tYlpsLIsa3EeLXXk0VwlxqTcFRfg9EhMW+17kbt9G0NrgCmhGb5vT2hyhJZLfDGx+7+5Uj/w==", + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/y18n/-/y18n-4.0.1.tgz", + "integrity": "sha512-wNcy4NvjMYL8gogWWYAO7ZFWFfHcbdbE57tZO8e4cbpj8tfUcwrwqSl3ad8HxpYWCdXcJUCeKKZS62Av1affwQ==", "dev": true }, "yargs": { diff --git a/src/LuaAST.ts b/src/LuaAST.ts index 661500e4a..2f0d2e880 100644 --- a/src/LuaAST.ts +++ b/src/LuaAST.ts @@ -174,10 +174,12 @@ export function setNodeOriginal(node: T | undefined, tsOriginal: } function getSourcePosition(sourceNode: ts.Node): TextRange | undefined { - if (sourceNode.getSourceFile() !== undefined && sourceNode.pos >= 0) { + const parseTreeNode = ts.getParseTreeNode(sourceNode) ?? sourceNode; + const sourceFile = parseTreeNode.getSourceFile(); + if (sourceFile !== undefined && parseTreeNode.pos >= 0) { const { line, character } = ts.getLineAndCharacterOfPosition( - sourceNode.getSourceFile(), - sourceNode.pos + sourceNode.getLeadingTriviaWidth() + sourceFile, + parseTreeNode.pos + parseTreeNode.getLeadingTriviaWidth() ); return { line, column: character }; diff --git a/src/transformation/utils/typescript/index.ts b/src/transformation/utils/typescript/index.ts index 46b4992ea..6d7bfa68c 100644 --- a/src/transformation/utils/typescript/index.ts +++ b/src/transformation/utils/typescript/index.ts @@ -32,7 +32,8 @@ export function getFirstDeclarationInFile(symbol: ts.Symbol, sourceFile: ts.Sour } function isStandardLibraryDeclaration(context: TransformationContext, declaration: ts.Declaration): boolean { - const sourceFile = declaration.getSourceFile(); + const parseTreeNode = ts.getParseTreeNode(declaration) ?? declaration; + const sourceFile = parseTreeNode.getSourceFile(); if (!sourceFile) { return false; } From c0d7936598fef6b37ea6d1c9efcc8818b3eb1afb Mon Sep 17 00:00:00 2001 From: Perryvw Date: Fri, 2 Apr 2021 19:01:46 +0200 Subject: [PATCH 5/5] 0.39.2 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 72ffc8c45..683dd5035 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "typescript-to-lua", - "version": "0.39.1", + "version": "0.39.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "typescript-to-lua", - "version": "0.39.1", + "version": "0.39.2", "license": "MIT", "dependencies": { "resolve": "^1.15.1", diff --git a/package.json b/package.json index 7a701c557..790013fa5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "typescript-to-lua", - "version": "0.39.1", + "version": "0.39.2", "description": "A generic TypeScript to Lua transpiler. Write your code in TypeScript and publish Lua!", "repository": "https://github.com/TypeScriptToLua/TypeScriptToLua", "homepage": "https://typescripttolua.github.io/",