typescript-to-lua 1.5.0 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,7 +7,7 @@ export declare const lualibDiagnostic: ((message: string, file?: ts.SourceFile |
7
7
  };
8
8
  declare class LuaLibPlugin implements tstl.Plugin {
9
9
  visitors: {
10
- 303: (file: ts.SourceFile, context: tstl.TransformationContext) => tstl.File;
10
+ 305: (file: ts.SourceFile, context: tstl.TransformationContext) => tstl.File;
11
11
  };
12
12
  printer: tstl.Printer;
13
13
  afterPrint(program: ts.Program, options: tstl.CompilerOptions, emitHost: EmitHost, result: ProcessedFile[]): ts.Diagnostic[];
@@ -16,7 +16,7 @@ export interface EmitResolver {
16
16
  moduleExportsSomeValue(moduleReferenceExpression: ts.Expression): boolean;
17
17
  getAllAccessorDeclarations(declaration: ts.AccessorDeclaration): AllAccessorDeclarations;
18
18
  }
19
- export interface DiagnosticsProducingTypeChecker extends ts.TypeChecker {
19
+ export interface TypeCheckerWithEmitResolver extends ts.TypeChecker {
20
20
  getEmitResolver(sourceFile?: ts.SourceFile, cancellationToken?: ts.CancellationToken): EmitResolver;
21
21
  }
22
22
  export declare class TransformationContext {
@@ -24,7 +24,7 @@ export declare class TransformationContext {
24
24
  sourceFile: ts.SourceFile;
25
25
  private visitorMap;
26
26
  readonly diagnostics: ts.Diagnostic[];
27
- readonly checker: DiagnosticsProducingTypeChecker;
27
+ readonly checker: TypeCheckerWithEmitResolver;
28
28
  readonly resolver: EmitResolver;
29
29
  readonly precedingStatementsStack: lua.Statement[][];
30
30
  readonly options: CompilerOptions;
@@ -16,7 +16,7 @@ class TransformationContext {
16
16
  this.sourceFile = sourceFile;
17
17
  this.visitorMap = visitorMap;
18
18
  this.diagnostics = [];
19
- this.checker = this.program.getDiagnosticsProducingTypeChecker();
19
+ this.checker = this.program.getTypeChecker();
20
20
  this.precedingStatementsStack = [];
21
21
  this.options = this.program.getCompilerOptions();
22
22
  this.luaTarget = (_a = this.options.luaTarget) !== null && _a !== void 0 ? _a : CompilerOptions_1.LuaTarget.Universal;
@@ -3,7 +3,7 @@ import * as lua from "../../LuaAST";
3
3
  import { TransformationContext } from "../context";
4
4
  export declare function hasDefaultExportModifier(node: ts.Node): boolean;
5
5
  export declare function hasExportModifier(node: ts.Node): boolean;
6
- export declare const createDefaultExportStringLiteral: (original?: ts.Node | undefined) => lua.StringLiteral;
6
+ export declare const createDefaultExportStringLiteral: (original?: ts.Node) => lua.StringLiteral;
7
7
  export declare function getExportedSymbolDeclaration(symbol: ts.Symbol): ts.Declaration | undefined;
8
8
  export declare function getSymbolFromIdentifier(context: TransformationContext, identifier: lua.Identifier): ts.Symbol | undefined;
9
9
  export declare function getIdentifierExportScope(context: TransformationContext, identifier: lua.Identifier): ts.SourceFile | ts.ModuleDeclaration | undefined;
@@ -59,7 +59,7 @@ function getDeclarationContextType({ program }, signatureDeclaration) {
59
59
  }
60
60
  // When using --noImplicitSelf and the signature is defined in a file targeted by the program apply the @noSelf rule.
61
61
  const options = program.getCompilerOptions();
62
- if (options.noImplicitSelf && program.getRootFileNames().includes(signatureDeclaration.getSourceFile().fileName)) {
62
+ if (options.noImplicitSelf && program.getSourceFile(signatureDeclaration.getSourceFile().fileName) !== undefined) {
63
63
  return ContextType.Void;
64
64
  }
65
65
  // Walk up to find @noSelf or @noSelfInFile
@@ -13,4 +13,4 @@ export declare function wrapInTable(...expressions: lua.Expression[]): lua.Table
13
13
  export declare function wrapInToStringForConcat(expression: lua.Expression): lua.Expression;
14
14
  export declare function createHoistableVariableDeclarationStatement(context: TransformationContext, identifier: lua.Identifier, initializer?: lua.Expression, tsOriginal?: ts.Node): lua.AssignmentStatement | lua.VariableDeclarationStatement;
15
15
  export declare function createLocalOrExportedOrGlobalDeclaration(context: TransformationContext, lhs: lua.Identifier | lua.Identifier[], rhs?: lua.Expression | lua.Expression[], tsOriginal?: ts.Node, overrideExportScope?: ts.SourceFile | ts.ModuleDeclaration): lua.Statement[];
16
- export declare const createNaN: (tsOriginal?: ts.Node | undefined) => lua.BinaryExpression;
16
+ export declare const createNaN: (tsOriginal?: ts.Node) => lua.BinaryExpression;
@@ -6,7 +6,10 @@ export declare const isCompoundAssignmentToken: (token: ts.BinaryOperator) => to
6
6
  export declare const unwrapCompoundAssignmentToken: (token: ts.CompoundAssignmentOperator) => CompoundAssignmentToken;
7
7
  export declare function transformCompoundAssignment(context: TransformationContext, expression: ts.Expression, lhs: ts.Expression, rhs: ts.Expression, operator: CompoundAssignmentToken, isPostfix: boolean): {
8
8
  statements: lua.Statement[];
9
- result: lua.AssignmentLeftHandSideExpression;
9
+ result: lua.Identifier;
10
+ } | {
11
+ statements: lua.Statement[];
12
+ result: lua.TableIndexExpression;
10
13
  };
11
14
  export declare function transformCompoundAssignmentExpression(context: TransformationContext, expression: ts.Expression, lhs: ts.Expression, rhs: ts.Expression, operator: CompoundAssignmentToken, isPostfix: boolean): lua.Expression;
12
15
  export declare function transformCompoundAssignmentStatement(context: TransformationContext, node: ts.Node, lhs: ts.Expression, rhs: ts.Expression, operator: CompoundAssignmentToken): lua.Statement[];
@@ -39,7 +39,7 @@ exports.unwrapCompoundAssignmentToken = unwrapCompoundAssignmentToken;
39
39
  function transformCompoundAssignment(context, expression, lhs, rhs, operator, isPostfix) {
40
40
  const left = (0, utils_1.cast)(context.transformExpression(lhs), lua.isAssignmentLeftHandSideExpression);
41
41
  const [rightPrecedingStatements, right] = (0, preceding_statements_1.transformInPrecedingStatementScope)(context, () => context.transformExpression(rhs));
42
- if (lua.isTableIndexExpression(left) && shouldCacheTableIndexExpressions(left, rightPrecedingStatements)) {
42
+ if (lua.isTableIndexExpression(left)) {
43
43
  // Complex property/element accesses need to cache object/index expressions to avoid repeating side-effects
44
44
  // local __obj, __index = ${objExpression}, ${indexExpression};
45
45
  const obj = context.createTempNameForLuaExpression(left.table);
@@ -60,6 +60,15 @@ function transformCompoundAssignment(context, expression, lhs, rhs, operator, is
60
60
  };
61
61
  }
62
62
  else {
63
+ if (isSetterSkippingCompoundAssignmentOperator(operator)) {
64
+ return {
65
+ statements: [
66
+ objAndIndexDeclaration,
67
+ ...transformSetterSkippingCompoundAssignment(accessExpression, operator, right, rightPrecedingStatements),
68
+ ],
69
+ result: left,
70
+ };
71
+ }
63
72
  // local ____tmp = ____obj[____index] ${replacementOperator} ${right};
64
73
  // ____obj[____index] = ____tmp;
65
74
  // return ____tmp
@@ -83,24 +92,6 @@ function transformCompoundAssignment(context, expression, lhs, rhs, operator, is
83
92
  const assignStatements = (0, assignments_1.transformAssignmentWithRightPrecedingStatements)(context, lhs, operatorExpression, rightPrecedingStatements);
84
93
  return { statements: [tmpDeclaration, ...precedingStatements, ...assignStatements], result: tmpIdentifier };
85
94
  }
86
- else if (ts.isPropertyAccessExpression(lhs) || ts.isElementAccessExpression(lhs)) {
87
- // Simple property/element access expressions need to cache in temp to avoid double-evaluation
88
- // local ____tmp = ${left} ${replacementOperator} ${right};
89
- // ${left} = ____tmp;
90
- // return ____tmp
91
- const tmpIdentifier = context.createTempNameForLuaExpression(left);
92
- const [precedingStatements, operatorExpression] = (0, binary_expression_1.transformBinaryOperation)(context, left, right, rightPrecedingStatements, operator, expression);
93
- const tmpDeclaration = lua.createVariableDeclarationStatement(tmpIdentifier, operatorExpression);
94
- if (isSetterSkippingCompoundAssignmentOperator(operator)) {
95
- const statements = [
96
- tmpDeclaration,
97
- ...transformSetterSkippingCompoundAssignment(tmpIdentifier, operator, right, precedingStatements),
98
- ];
99
- return { statements, result: tmpIdentifier };
100
- }
101
- const assignStatements = (0, assignments_1.transformAssignmentWithRightPrecedingStatements)(context, lhs, tmpIdentifier, precedingStatements);
102
- return { statements: [tmpDeclaration, ...assignStatements], result: tmpIdentifier };
103
- }
104
95
  else {
105
96
  if (rightPrecedingStatements.length > 0 && isSetterSkippingCompoundAssignmentOperator(operator)) {
106
97
  return {
@@ -195,7 +195,9 @@ const transformCallExpression = (node, context) => {
195
195
  // if is optionalContinuation, context will be handled by transformOptionalChain.
196
196
  const useGlobalContext = !context.isStrict && optionalContinuation === undefined;
197
197
  const callContext = useGlobalContext ? ts.factory.createIdentifier("_G") : ts.factory.createNull();
198
- [callPath, parameters] = transformCallAndArguments(context, calledExpression, node.arguments, signature, callContext);
198
+ [callPath, parameters] = transformCallAndArguments(context, calledExpression, node.arguments, signature,
199
+ // Only pass context if noImplicitSelf is not configured
200
+ context.options.noImplicitSelf ? undefined : callContext);
199
201
  }
200
202
  const callExpression = lua.createCallExpression(callPath, parameters, node);
201
203
  if (optionalContinuation && isContextualCall) {
@@ -19,6 +19,7 @@ const fs = require("fs");
19
19
  const path = require("path");
20
20
  const ts = require("typescript");
21
21
  const tsconfig_1 = require("../cli/tsconfig");
22
+ const utils_1 = require("../utils");
22
23
  const output_collector_1 = require("./output-collector");
23
24
  const transpiler_1 = require("./transpiler");
24
25
  __exportStar(require("./transpile"), exports);
@@ -44,8 +45,12 @@ exports.transpileProject = transpileProject;
44
45
  const libCache = {};
45
46
  /** @internal */
46
47
  function createVirtualProgram(input, options = {}) {
48
+ const normalizedFiles = {};
49
+ for (const [path, file] of Object.entries(input)) {
50
+ normalizedFiles[(0, utils_1.normalizeSlashes)(path)] = file;
51
+ }
47
52
  const compilerHost = {
48
- fileExists: fileName => fileName in input || ts.sys.fileExists(fileName),
53
+ fileExists: fileName => fileName in normalizedFiles || ts.sys.fileExists(fileName),
49
54
  getCanonicalFileName: fileName => fileName,
50
55
  getCurrentDirectory: () => "",
51
56
  getDefaultLibFileName: ts.getDefaultLibFileName,
@@ -54,8 +59,8 @@ function createVirtualProgram(input, options = {}) {
54
59
  useCaseSensitiveFileNames: () => false,
55
60
  writeFile() { },
56
61
  getSourceFile(fileName) {
57
- if (fileName in input) {
58
- return ts.createSourceFile(fileName, input[fileName], ts.ScriptTarget.Latest, false);
62
+ if (fileName in normalizedFiles) {
63
+ return ts.createSourceFile(fileName, normalizedFiles[fileName], ts.ScriptTarget.Latest, false);
59
64
  }
60
65
  let filePath;
61
66
  if (fileName.startsWith("lib.")) {
@@ -75,7 +80,7 @@ function createVirtualProgram(input, options = {}) {
75
80
  }
76
81
  },
77
82
  };
78
- return ts.createProgram(Object.keys(input), options, compilerHost);
83
+ return ts.createProgram(Object.keys(normalizedFiles), options, compilerHost);
79
84
  }
80
85
  exports.createVirtualProgram = createVirtualProgram;
81
86
  function transpileVirtualProject(files, options = {}) {
@@ -115,8 +115,8 @@ function resolveFileDependencies(file, context) {
115
115
  const dependencies = [];
116
116
  const diagnostics = [];
117
117
  for (const required of findRequiredPaths(file.code)) {
118
- // Do no resolve lualib
119
- if (required === "lualib_bundle") {
118
+ // Do no resolve lualib, unless it is included from node_modules
119
+ if (required === "lualib_bundle" && !isNodeModulesFile(file.fileName)) {
120
120
  dependencies.push({ fileName: "lualib_bundle", code: "" });
121
121
  continue;
122
122
  }
@@ -16,9 +16,10 @@ class Transpiler {
16
16
  }
17
17
  emit(emitOptions) {
18
18
  var _a, _b;
19
- const { program, writeFile = this.emitHost.writeFile } = emitOptions;
19
+ const { program, writeFile = this.emitHost.writeFile, plugins: optionsPlugins = [] } = emitOptions;
20
20
  const options = program.getCompilerOptions();
21
- const { diagnostics: getPluginsDiagnostics, plugins } = (0, plugins_1.getPlugins)(program);
21
+ const { diagnostics: getPluginsDiagnostics, plugins: configPlugins } = (0, plugins_1.getPlugins)(program);
22
+ const plugins = [...optionsPlugins, ...configPlugins];
22
23
  const { diagnostics, transpiledFiles: freshFiles } = (0, transpile_1.getProgramTranspileResult)(this.emitHost, writeFile, {
23
24
  ...emitOptions,
24
25
  plugins,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "typescript-to-lua",
3
- "version": "1.5.0",
3
+ "version": "1.6.0",
4
4
  "description": "A generic TypeScript to Lua transpiler. Write your code in TypeScript and publish Lua!",
5
5
  "repository": "https://github.com/TypeScriptToLua/TypeScriptToLua",
6
6
  "homepage": "https://typescripttolua.github.io/",
@@ -43,7 +43,7 @@
43
43
  "node": ">=12.13.0"
44
44
  },
45
45
  "peerDependencies": {
46
- "typescript": "~4.6.2"
46
+ "typescript": "~4.7.3"
47
47
  },
48
48
  "dependencies": {
49
49
  "enhanced-resolve": "^5.8.2",
@@ -53,14 +53,14 @@
53
53
  "devDependencies": {
54
54
  "@types/fs-extra": "^8.1.0",
55
55
  "@types/glob": "^7.1.1",
56
- "@types/jest": "^27.4.1",
56
+ "@types/jest": "^27.5.2",
57
57
  "@types/node": "^13.7.7",
58
58
  "@types/resolve": "1.14.0",
59
- "@typescript-eslint/eslint-plugin": "^5.13.0",
60
- "@typescript-eslint/parser": "^5.13.0",
61
- "eslint": "^8.10.0",
62
- "eslint-plugin-import": "^2.25.4",
63
- "eslint-plugin-jest": "^26.1.1",
59
+ "@typescript-eslint/eslint-plugin": "^5.27.0",
60
+ "@typescript-eslint/parser": "^5.27.0",
61
+ "eslint": "^8.17.0",
62
+ "eslint-plugin-import": "^2.26.0",
63
+ "eslint-plugin-jest": "^26.4.6",
64
64
  "fs-extra": "^8.1.0",
65
65
  "javascript-stringify": "^2.0.1",
66
66
  "jest": "^27.3.0",
@@ -70,6 +70,6 @@
70
70
  "prettier": "^2.3.2",
71
71
  "ts-jest": "^27.1.3",
72
72
  "ts-node": "^10.3.0",
73
- "typescript": "~4.6.2"
73
+ "typescript": "~4.7.3"
74
74
  }
75
75
  }