typescript-to-lua 1.6.3 → 1.7.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.
Files changed (97) hide show
  1. package/dist/CompilerOptions.d.ts +1 -0
  2. package/dist/cli/parse.js +5 -0
  3. package/dist/lualib/ArraySlice.lua +10 -2
  4. package/dist/lualib/ArraySplice.lua +5 -1
  5. package/dist/lualib/Generator.lua +8 -1
  6. package/dist/lualib/ParseFloat.lua +5 -1
  7. package/dist/lualib/SparseArraySpread.lua +5 -1
  8. package/dist/lualib/StringCharCodeAt.lua +5 -1
  9. package/dist/lualib/lualib_bundle.lua +38 -7
  10. package/dist/lualib-build/plugin.js +1 -2
  11. package/dist/measure-performance.d.ts +15 -0
  12. package/dist/measure-performance.js +83 -0
  13. package/dist/transformation/builtins/array.js +2 -1
  14. package/dist/transformation/builtins/function.js +1 -1
  15. package/dist/transformation/builtins/global.d.ts +1 -1
  16. package/dist/transformation/builtins/global.js +13 -12
  17. package/dist/transformation/builtins/index.d.ts +2 -2
  18. package/dist/transformation/builtins/index.js +87 -89
  19. package/dist/transformation/builtins/object.d.ts +1 -1
  20. package/dist/transformation/builtins/object.js +4 -4
  21. package/dist/transformation/builtins/promise.js +4 -2
  22. package/dist/transformation/builtins/string.js +2 -0
  23. package/dist/transformation/context/context.d.ts +15 -0
  24. package/dist/transformation/context/context.js +66 -24
  25. package/dist/transformation/context/visitors.d.ts +1 -1
  26. package/dist/transformation/index.js +6 -5
  27. package/dist/transformation/utils/annotations.d.ts +1 -17
  28. package/dist/transformation/utils/annotations.js +48 -88
  29. package/dist/transformation/utils/assignment-validation.js +27 -23
  30. package/dist/transformation/utils/diagnostics.d.ts +1 -4
  31. package/dist/transformation/utils/diagnostics.js +2 -3
  32. package/dist/transformation/utils/function-context.d.ts +1 -1
  33. package/dist/transformation/utils/function-context.js +57 -51
  34. package/dist/transformation/utils/language-extensions.d.ts +57 -51
  35. package/dist/transformation/utils/language-extensions.js +99 -119
  36. package/dist/transformation/utils/lualib.d.ts +0 -1
  37. package/dist/transformation/utils/lualib.js +2 -8
  38. package/dist/transformation/utils/safe-names.d.ts +1 -1
  39. package/dist/transformation/utils/safe-names.js +3 -6
  40. package/dist/transformation/utils/scope.d.ts +0 -2
  41. package/dist/transformation/utils/scope.js +10 -27
  42. package/dist/transformation/utils/symbols.d.ts +1 -1
  43. package/dist/transformation/utils/symbols.js +7 -20
  44. package/dist/transformation/utils/typescript/index.d.ts +1 -0
  45. package/dist/transformation/utils/typescript/index.js +2 -1
  46. package/dist/transformation/utils/typescript/types.d.ts +2 -5
  47. package/dist/transformation/utils/typescript/types.js +27 -37
  48. package/dist/transformation/visitors/access.js +1 -4
  49. package/dist/transformation/visitors/block.js +4 -4
  50. package/dist/transformation/visitors/call.js +7 -18
  51. package/dist/transformation/visitors/class/index.js +8 -20
  52. package/dist/transformation/visitors/class/members/constructor.js +2 -2
  53. package/dist/transformation/visitors/class/new.js +2 -5
  54. package/dist/transformation/visitors/class/setup.d.ts +1 -1
  55. package/dist/transformation/visitors/class/setup.js +4 -4
  56. package/dist/transformation/visitors/class/utils.d.ts +1 -1
  57. package/dist/transformation/visitors/class/utils.js +2 -9
  58. package/dist/transformation/visitors/conditional.js +4 -4
  59. package/dist/transformation/visitors/function.d.ts +1 -1
  60. package/dist/transformation/visitors/function.js +8 -16
  61. package/dist/transformation/visitors/identifier.d.ts +1 -0
  62. package/dist/transformation/visitors/identifier.js +40 -44
  63. package/dist/transformation/visitors/language-extensions/call-extension.d.ts +10 -0
  64. package/dist/transformation/visitors/language-extensions/call-extension.js +23 -0
  65. package/dist/transformation/visitors/language-extensions/identifier.d.ts +5 -0
  66. package/dist/transformation/visitors/language-extensions/identifier.js +27 -0
  67. package/dist/transformation/visitors/language-extensions/iterable.d.ts +2 -3
  68. package/dist/transformation/visitors/language-extensions/iterable.js +21 -22
  69. package/dist/transformation/visitors/language-extensions/multi.d.ts +0 -1
  70. package/dist/transformation/visitors/language-extensions/multi.js +10 -24
  71. package/dist/transformation/visitors/language-extensions/operators.d.ts +2 -5
  72. package/dist/transformation/visitors/language-extensions/operators.js +89 -96
  73. package/dist/transformation/visitors/language-extensions/range.js +4 -2
  74. package/dist/transformation/visitors/language-extensions/table.d.ts +6 -3
  75. package/dist/transformation/visitors/language-extensions/table.js +46 -75
  76. package/dist/transformation/visitors/language-extensions/vararg.js +7 -3
  77. package/dist/transformation/visitors/literal.js +2 -23
  78. package/dist/transformation/visitors/loops/for-of.js +18 -18
  79. package/dist/transformation/visitors/loops/utils.js +5 -5
  80. package/dist/transformation/visitors/namespace.js +5 -14
  81. package/dist/transformation/visitors/return.js +2 -3
  82. package/dist/transformation/visitors/sourceFile.js +3 -4
  83. package/dist/transformation/visitors/spread.js +0 -5
  84. package/dist/transformation/visitors/switch.js +2 -2
  85. package/dist/transformation/visitors/variable-declaration.js +1 -1
  86. package/dist/transpilation/plugins.js +3 -0
  87. package/dist/transpilation/resolve.js +144 -161
  88. package/dist/transpilation/transpile.js +10 -0
  89. package/dist/transpilation/transpiler.d.ts +1 -0
  90. package/dist/transpilation/transpiler.js +18 -5
  91. package/dist/tstl.js +21 -0
  92. package/language-extensions/index.d.ts +156 -67
  93. package/package.json +2 -2
  94. package/dist/transformation/visitors/language-extensions/index.d.ts +0 -4
  95. package/dist/transformation/visitors/language-extensions/index.js +0 -17
  96. package/dist/transformation/visitors/language-extensions/pairsIterable.d.ts +0 -5
  97. package/dist/transformation/visitors/language-extensions/pairsIterable.js +0 -53
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.performHoisting = exports.separateHoistedStatements = exports.isFunctionScopeWithDefinition = exports.hasReferencedSymbol = exports.hasReferencedUndefinedLocalFunction = exports.addScopeVariableDeclaration = exports.popScope = exports.pushScope = exports.findScope = exports.peekScope = exports.markSymbolAsReferencedInCurrentScopes = exports.walkScopesUp = exports.ScopeType = void 0;
3
+ exports.performHoisting = exports.separateHoistedStatements = exports.isFunctionScopeWithDefinition = exports.hasReferencedSymbol = exports.hasReferencedUndefinedLocalFunction = exports.addScopeVariableDeclaration = exports.findScope = exports.peekScope = exports.markSymbolAsReferencedInCurrentScopes = exports.walkScopesUp = exports.ScopeType = void 0;
4
4
  const ts = require("typescript");
5
5
  const lua = require("../../LuaAST");
6
6
  const utils_1 = require("../../utils");
@@ -18,12 +18,8 @@ var ScopeType;
18
18
  ScopeType[ScopeType["Catch"] = 128] = "Catch";
19
19
  ScopeType[ScopeType["LoopInitializer"] = 256] = "LoopInitializer";
20
20
  })(ScopeType = exports.ScopeType || (exports.ScopeType = {}));
21
- const scopeStacks = new WeakMap();
22
- function getScopeStack(context) {
23
- return (0, utils_1.getOrUpdate)(scopeStacks, context, () => []);
24
- }
25
21
  function* walkScopesUp(context) {
26
- const scopeStack = getScopeStack(context);
22
+ const scopeStack = context.scopeStack;
27
23
  for (let i = scopeStack.length - 1; i >= 0; --i) {
28
24
  const scope = scopeStack[i];
29
25
  yield scope;
@@ -31,7 +27,7 @@ function* walkScopesUp(context) {
31
27
  }
32
28
  exports.walkScopesUp = walkScopesUp;
33
29
  function markSymbolAsReferencedInCurrentScopes(context, symbolId, identifier) {
34
- for (const scope of getScopeStack(context)) {
30
+ for (const scope of context.scopeStack) {
35
31
  if (!scope.referencedSymbols) {
36
32
  scope.referencedSymbols = new Map();
37
33
  }
@@ -41,34 +37,21 @@ function markSymbolAsReferencedInCurrentScopes(context, symbolId, identifier) {
41
37
  }
42
38
  exports.markSymbolAsReferencedInCurrentScopes = markSymbolAsReferencedInCurrentScopes;
43
39
  function peekScope(context) {
44
- const scopeStack = getScopeStack(context);
40
+ const scopeStack = context.scopeStack;
45
41
  const scope = scopeStack[scopeStack.length - 1];
46
42
  (0, utils_1.assert)(scope);
47
43
  return scope;
48
44
  }
49
45
  exports.peekScope = peekScope;
50
46
  function findScope(context, scopeTypes) {
51
- return [...getScopeStack(context)].reverse().find(s => scopeTypes & s.type);
47
+ for (let i = context.scopeStack.length - 1; i >= 0; --i) {
48
+ const scope = context.scopeStack[i];
49
+ if (scopeTypes & scope.type) {
50
+ return scope;
51
+ }
52
+ }
52
53
  }
53
54
  exports.findScope = findScope;
54
- const scopeIdCounters = new WeakMap();
55
- function pushScope(context, scopeType) {
56
- var _a;
57
- const nextScopeId = ((_a = scopeIdCounters.get(context)) !== null && _a !== void 0 ? _a : 0) + 1;
58
- scopeIdCounters.set(context, nextScopeId);
59
- const scopeStack = getScopeStack(context);
60
- const scope = { type: scopeType, id: nextScopeId };
61
- scopeStack.push(scope);
62
- return scope;
63
- }
64
- exports.pushScope = pushScope;
65
- function popScope(context) {
66
- const scopeStack = getScopeStack(context);
67
- const scope = scopeStack.pop();
68
- (0, utils_1.assert)(scope);
69
- return scope;
70
- }
71
- exports.popScope = popScope;
72
55
  function addScopeVariableDeclaration(scope, declaration) {
73
56
  if (!scope.variableDeclarations) {
74
57
  scope.variableDeclarations = [];
@@ -8,4 +8,4 @@ export interface SymbolInfo {
8
8
  export declare function getSymbolInfo(context: TransformationContext, symbolId: lua.SymbolId): SymbolInfo | undefined;
9
9
  export declare function getSymbolIdOfSymbol(context: TransformationContext, symbol: ts.Symbol): lua.SymbolId | undefined;
10
10
  export declare function trackSymbolReference(context: TransformationContext, symbol: ts.Symbol, identifier: ts.Identifier): lua.SymbolId | undefined;
11
- export declare function getIdentifierSymbolId(context: TransformationContext, identifier: ts.Identifier): lua.SymbolId | undefined;
11
+ export declare function getIdentifierSymbolId(context: TransformationContext, identifier: ts.Identifier, symbol: ts.Symbol | undefined): lua.SymbolId | undefined;
@@ -1,35 +1,23 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getIdentifierSymbolId = exports.trackSymbolReference = exports.getSymbolIdOfSymbol = exports.getSymbolInfo = void 0;
4
- const utils_1 = require("../../utils");
5
4
  const spread_1 = require("../visitors/spread");
6
5
  const scope_1 = require("./scope");
7
- const symbolIdCounters = new WeakMap();
8
- function nextSymbolId(context) {
9
- var _a;
10
- const symbolId = ((_a = symbolIdCounters.get(context)) !== null && _a !== void 0 ? _a : 0) + 1;
11
- symbolIdCounters.set(context, symbolId);
12
- return symbolId;
13
- }
14
- const symbolInfoMap = new WeakMap();
15
- const symbolIdMaps = new WeakMap();
16
6
  function getSymbolInfo(context, symbolId) {
17
- return (0, utils_1.getOrUpdate)(symbolInfoMap, context, () => new Map()).get(symbolId);
7
+ return context.symbolInfoMap.get(symbolId);
18
8
  }
19
9
  exports.getSymbolInfo = getSymbolInfo;
20
10
  function getSymbolIdOfSymbol(context, symbol) {
21
- return (0, utils_1.getOrUpdate)(symbolIdMaps, context, () => new Map()).get(symbol);
11
+ return context.symbolIdMaps.get(symbol);
22
12
  }
23
13
  exports.getSymbolIdOfSymbol = getSymbolIdOfSymbol;
24
14
  function trackSymbolReference(context, symbol, identifier) {
25
- const symbolIds = (0, utils_1.getOrUpdate)(symbolIdMaps, context, () => new Map());
26
15
  // Track first time symbols are seen
27
- let symbolId = symbolIds.get(symbol);
16
+ let symbolId = context.symbolIdMaps.get(symbol);
28
17
  if (symbolId === undefined) {
29
- symbolId = nextSymbolId(context);
30
- symbolIds.set(symbol, symbolId);
31
- const symbolInfo = (0, utils_1.getOrUpdate)(symbolInfoMap, context, () => new Map());
32
- symbolInfo.set(symbolId, { symbol, firstSeenAtPos: identifier.pos });
18
+ symbolId = context.nextSymbolId();
19
+ context.symbolIdMaps.set(symbol, symbolId);
20
+ context.symbolInfoMap.set(symbolId, { symbol, firstSeenAtPos: identifier.pos });
33
21
  }
34
22
  // If isOptimizedVarArgSpread returns true, the identifier will not appear in the resulting Lua.
35
23
  // Only the optimized ellipses (...) will be used.
@@ -39,8 +27,7 @@ function trackSymbolReference(context, symbol, identifier) {
39
27
  return symbolId;
40
28
  }
41
29
  exports.trackSymbolReference = trackSymbolReference;
42
- function getIdentifierSymbolId(context, identifier) {
43
- const symbol = context.checker.getSymbolAtLocation(identifier);
30
+ function getIdentifierSymbolId(context, identifier, symbol) {
44
31
  if (symbol) {
45
32
  return trackSymbolReference(context, symbol, identifier);
46
33
  }
@@ -9,6 +9,7 @@ export declare function hasExportEquals(sourceFile: ts.SourceFile): boolean;
9
9
  export declare function findFirstNodeAbove<T extends ts.Node>(node: ts.Node, callback: (n: ts.Node) => n is T): T | undefined;
10
10
  export declare function findFirstNonOuterParent(node: ts.Node): ts.Node;
11
11
  export declare function getFirstDeclarationInFile(symbol: ts.Symbol, sourceFile: ts.SourceFile): ts.Declaration | undefined;
12
+ export declare function isStandardLibraryDeclaration(context: TransformationContext, declaration: ts.Declaration): boolean;
12
13
  export declare function isStandardLibraryType(context: TransformationContext, type: ts.Type, name: string | undefined): boolean;
13
14
  export declare function hasStandardLibrarySignature(context: TransformationContext, callExpression: ts.CallExpression): boolean;
14
15
  export declare function inferAssignedType(context: TransformationContext, expression: ts.Expression): ts.Type;
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.isConstIdentifier = exports.getFunctionTypeForCall = exports.isExpressionWithEvaluationEffect = exports.getAllCallSignatures = exports.inferAssignedType = exports.hasStandardLibrarySignature = exports.isStandardLibraryType = exports.getFirstDeclarationInFile = exports.findFirstNonOuterParent = exports.findFirstNodeAbove = exports.hasExportEquals = void 0;
17
+ exports.isConstIdentifier = exports.getFunctionTypeForCall = exports.isExpressionWithEvaluationEffect = exports.getAllCallSignatures = exports.inferAssignedType = exports.hasStandardLibrarySignature = exports.isStandardLibraryType = exports.isStandardLibraryDeclaration = exports.getFirstDeclarationInFile = exports.findFirstNonOuterParent = exports.findFirstNodeAbove = exports.hasExportEquals = void 0;
18
18
  const ts = require("typescript");
19
19
  __exportStar(require("./nodes"), exports);
20
20
  __exportStar(require("./types"), exports);
@@ -62,6 +62,7 @@ function isStandardLibraryDeclaration(context, declaration) {
62
62
  }
63
63
  return context.program.isSourceFileDefaultLibrary(sourceFile);
64
64
  }
65
+ exports.isStandardLibraryDeclaration = isStandardLibraryDeclaration;
65
66
  function isStandardLibraryType(context, type, name) {
66
67
  const symbol = type.getSymbol();
67
68
  if (!symbol || (name ? symbol.name !== name : symbol.name === "__type")) {
@@ -1,12 +1,9 @@
1
1
  import * as ts from "typescript";
2
2
  import { TransformationContext } from "../../context";
3
- export declare function isTypeWithFlags(context: TransformationContext, type: ts.Type, flags: ts.TypeFlags): boolean;
4
- export declare function typeAlwaysSatisfies(context: TransformationContext, type: ts.Type, predicate: (type: ts.Type) => boolean): boolean;
5
- export declare function typeCanSatisfy(context: TransformationContext, type: ts.Type, predicate: (type: ts.Type) => boolean): boolean;
6
- export declare function isNullishType(context: TransformationContext, type: ts.Type): boolean;
3
+ export declare function typeAlwaysHasSomeOfFlags(context: TransformationContext, type: ts.Type, flags: ts.TypeFlags): boolean;
4
+ export declare function typeCanHaveSomeOfFlags(context: TransformationContext, type: ts.Type, flags: ts.TypeFlags): boolean;
7
5
  export declare function isStringType(context: TransformationContext, type: ts.Type): boolean;
8
6
  export declare function isNumberType(context: TransformationContext, type: ts.Type): boolean;
9
- export declare function isNullableType(context: TransformationContext, type: ts.Type, isType: (c: TransformationContext, t: ts.Type) => boolean): boolean;
10
7
  /**
11
8
  * Iterate over a type and its bases until the callback returns true.
12
9
  */
@@ -1,30 +1,25 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.canBeFalsyWhenNotNull = exports.canBeFalsy = exports.isFunctionType = exports.isArrayType = exports.forTypeOrAnySupertype = exports.isNullableType = exports.isNumberType = exports.isStringType = exports.isNullishType = exports.typeCanSatisfy = exports.typeAlwaysSatisfies = exports.isTypeWithFlags = void 0;
3
+ exports.canBeFalsyWhenNotNull = exports.canBeFalsy = exports.isFunctionType = exports.isArrayType = exports.forTypeOrAnySupertype = exports.isNumberType = exports.isStringType = exports.typeCanHaveSomeOfFlags = exports.typeAlwaysHasSomeOfFlags = void 0;
4
4
  const ts = require("typescript");
5
- function isTypeWithFlags(context, type, flags) {
6
- const predicate = (type) => (type.flags & flags) !== 0;
7
- return typeAlwaysSatisfies(context, type, predicate);
8
- }
9
- exports.isTypeWithFlags = isTypeWithFlags;
10
- function typeAlwaysSatisfies(context, type, predicate) {
5
+ function typeAlwaysHasSomeOfFlags(context, type, flags) {
11
6
  const baseConstraint = context.checker.getBaseConstraintOfType(type);
12
7
  if (baseConstraint) {
13
8
  type = baseConstraint;
14
9
  }
15
- if (predicate(type)) {
10
+ if (type.flags & flags) {
16
11
  return true;
17
12
  }
18
13
  if (type.isUnion()) {
19
- return type.types.every(t => typeAlwaysSatisfies(context, t, predicate));
14
+ return type.types.every(t => typeAlwaysHasSomeOfFlags(context, t, flags));
20
15
  }
21
16
  if (type.isIntersection()) {
22
- return type.types.some(t => typeAlwaysSatisfies(context, t, predicate));
17
+ return type.types.some(t => typeAlwaysHasSomeOfFlags(context, t, flags));
23
18
  }
24
19
  return false;
25
20
  }
26
- exports.typeAlwaysSatisfies = typeAlwaysSatisfies;
27
- function typeCanSatisfy(context, type, predicate) {
21
+ exports.typeAlwaysHasSomeOfFlags = typeAlwaysHasSomeOfFlags;
22
+ function typeCanHaveSomeOfFlags(context, type, flags) {
28
23
  const baseConstraint = context.checker.getBaseConstraintOfType(type);
29
24
  if (!baseConstraint) {
30
25
  // type parameter with no constraint can be anything, assume it might satisfy predicate
@@ -34,36 +29,29 @@ function typeCanSatisfy(context, type, predicate) {
34
29
  else {
35
30
  type = baseConstraint;
36
31
  }
37
- if (predicate(type)) {
32
+ if (type.flags & flags) {
38
33
  return true;
39
34
  }
40
35
  if (type.isUnion()) {
41
- return type.types.some(t => typeCanSatisfy(context, t, predicate));
36
+ return type.types.some(t => typeCanHaveSomeOfFlags(context, t, flags));
42
37
  }
43
38
  if (type.isIntersection()) {
44
- return type.types.some(t => typeCanSatisfy(context, t, predicate));
39
+ return type.types.some(t => typeCanHaveSomeOfFlags(context, t, flags));
45
40
  }
46
41
  return false;
47
42
  }
48
- exports.typeCanSatisfy = typeCanSatisfy;
49
- function isNullishType(context, type) {
50
- return isTypeWithFlags(context, type, ts.TypeFlags.Undefined | ts.TypeFlags.Null | ts.TypeFlags.VoidLike);
51
- }
52
- exports.isNullishType = isNullishType;
43
+ exports.typeCanHaveSomeOfFlags = typeCanHaveSomeOfFlags;
53
44
  function isStringType(context, type) {
54
- return isTypeWithFlags(context, type, ts.TypeFlags.String | ts.TypeFlags.StringLike | ts.TypeFlags.StringLiteral);
45
+ return typeAlwaysHasSomeOfFlags(context, type, ts.TypeFlags.StringLike);
55
46
  }
56
47
  exports.isStringType = isStringType;
57
48
  function isNumberType(context, type) {
58
- return isTypeWithFlags(context, type, ts.TypeFlags.Number | ts.TypeFlags.NumberLike | ts.TypeFlags.NumberLiteral);
49
+ return typeAlwaysHasSomeOfFlags(context, type, ts.TypeFlags.NumberLike);
59
50
  }
60
51
  exports.isNumberType = isNumberType;
61
- function isNullableType(context, type, isType) {
62
- return (typeCanSatisfy(context, type, t => isType(context, t)) &&
63
- typeAlwaysSatisfies(context, type, t => isType(context, t) || isNullishType(context, t)));
64
- }
65
- exports.isNullableType = isNullableType;
66
52
  function isExplicitArrayType(context, type) {
53
+ if (context.checker.isArrayType(type) || context.checker.isTupleType(type))
54
+ return true;
67
55
  if (type.symbol) {
68
56
  const baseConstraint = context.checker.getBaseConstraintOfType(type);
69
57
  if (baseConstraint && baseConstraint !== type) {
@@ -73,25 +61,22 @@ function isExplicitArrayType(context, type) {
73
61
  if (type.isUnionOrIntersection()) {
74
62
  return type.types.some(t => isExplicitArrayType(context, t));
75
63
  }
76
- const flags = ts.NodeBuilderFlags.InTypeAlias | ts.NodeBuilderFlags.AllowEmptyTuple;
77
- let typeNode = context.checker.typeToTypeNode(type, undefined, flags);
78
- if (typeNode && ts.isTypeOperatorNode(typeNode) && typeNode.operator === ts.SyntaxKind.ReadonlyKeyword) {
79
- typeNode = typeNode.type;
80
- }
81
- return typeNode !== undefined && (ts.isArrayTypeNode(typeNode) || ts.isTupleTypeNode(typeNode));
64
+ return false;
82
65
  }
83
66
  /**
84
67
  * Iterate over a type and its bases until the callback returns true.
85
68
  */
86
69
  function forTypeOrAnySupertype(context, type, predicate) {
87
- var _a;
88
70
  if (predicate(type)) {
89
71
  return true;
90
72
  }
91
73
  if (!type.isClassOrInterface() && type.symbol) {
92
74
  type = context.checker.getDeclaredTypeOfSymbol(type.symbol);
93
75
  }
94
- return ((_a = type.getBaseTypes()) !== null && _a !== void 0 ? _a : []).some(superType => forTypeOrAnySupertype(context, superType, predicate));
76
+ const baseTypes = type.getBaseTypes();
77
+ if (!baseTypes)
78
+ return false;
79
+ return baseTypes.some(superType => forTypeOrAnySupertype(context, superType, predicate));
95
80
  }
96
81
  exports.forTypeOrAnySupertype = forTypeOrAnySupertype;
97
82
  function isArrayType(context, type) {
@@ -104,6 +89,8 @@ function isFunctionType(type) {
104
89
  exports.isFunctionType = isFunctionType;
105
90
  function canBeFalsy(context, type) {
106
91
  const strictNullChecks = context.options.strict === true || context.options.strictNullChecks === true;
92
+ if (!strictNullChecks && !type.isLiteral())
93
+ return true;
107
94
  const falsyFlags = ts.TypeFlags.Boolean |
108
95
  ts.TypeFlags.BooleanLiteral |
109
96
  ts.TypeFlags.Never |
@@ -112,17 +99,20 @@ function canBeFalsy(context, type) {
112
99
  ts.TypeFlags.Any |
113
100
  ts.TypeFlags.Undefined |
114
101
  ts.TypeFlags.Null;
115
- return typeCanSatisfy(context, type, type => (type.flags & falsyFlags) !== 0 || (!strictNullChecks && !type.isLiteral()));
102
+ return typeCanHaveSomeOfFlags(context, type, falsyFlags);
116
103
  }
117
104
  exports.canBeFalsy = canBeFalsy;
118
105
  function canBeFalsyWhenNotNull(context, type) {
106
+ const strictNullChecks = context.options.strict === true || context.options.strictNullChecks === true;
107
+ if (!strictNullChecks && !type.isLiteral())
108
+ return true;
119
109
  const falsyFlags = ts.TypeFlags.Boolean |
120
110
  ts.TypeFlags.BooleanLiteral |
121
111
  ts.TypeFlags.Never |
122
112
  ts.TypeFlags.Void |
123
113
  ts.TypeFlags.Unknown |
124
114
  ts.TypeFlags.Any;
125
- return typeCanSatisfy(context, type, type => (type.flags & falsyFlags) !== 0);
115
+ return typeCanHaveSomeOfFlags(context, type, falsyFlags);
126
116
  }
127
117
  exports.canBeFalsyWhenNotNull = canBeFalsyWhenNotNull;
128
118
  //# sourceMappingURL=types.js.map
@@ -76,10 +76,6 @@ function transformPropertyAccessExpressionWithCapture(context, node, thisValueCa
76
76
  const property = node.name.text;
77
77
  const type = context.checker.getTypeAtLocation(node.expression);
78
78
  const isOptionalLeft = (0, optional_chaining_1.isOptionalContinuation)(node.expression);
79
- const annotations = (0, annotations_1.getTypeAnnotations)(type);
80
- if (annotations.has(annotations_1.AnnotationKind.LuaTable)) {
81
- context.diagnostics.push((0, diagnostics_1.annotationRemoved)(node, annotations_1.AnnotationKind.LuaTable));
82
- }
83
79
  const constEnumValue = (0, enum_1.tryGetConstEnumValue)(context, node);
84
80
  if (constEnumValue) {
85
81
  return { expression: constEnumValue };
@@ -91,6 +87,7 @@ function transformPropertyAccessExpressionWithCapture(context, node, thisValueCa
91
87
  return (0, optional_chaining_1.transformOptionalChainWithCapture)(context, node, thisValueCapture);
92
88
  }
93
89
  // Do not output path for member only enums
90
+ const annotations = (0, annotations_1.getTypeAnnotations)(type);
94
91
  if (annotations.has(annotations_1.AnnotationKind.CompileMembersOnly)) {
95
92
  if (isOptionalLeft) {
96
93
  context.diagnostics.push((0, diagnostics_1.unsupportedOptionalCompileMembersOnly)(node));
@@ -9,16 +9,16 @@ function transformBlockOrStatement(context, statement) {
9
9
  }
10
10
  exports.transformBlockOrStatement = transformBlockOrStatement;
11
11
  function transformScopeBlock(context, node, scopeType) {
12
- (0, scope_1.pushScope)(context, scopeType);
12
+ context.pushScope(scopeType);
13
13
  const statements = (0, scope_1.performHoisting)(context, context.transformStatements(node.statements));
14
- const scope = (0, scope_1.popScope)(context);
14
+ const scope = context.popScope();
15
15
  return [lua.createBlock(statements, node), scope];
16
16
  }
17
17
  exports.transformScopeBlock = transformScopeBlock;
18
18
  const transformBlock = (node, context) => {
19
- (0, scope_1.pushScope)(context, scope_1.ScopeType.Block);
19
+ context.pushScope(scope_1.ScopeType.Block);
20
20
  const statements = (0, scope_1.performHoisting)(context, context.transformStatements(node.statements));
21
- (0, scope_1.popScope)(context);
21
+ context.popScope();
22
22
  return lua.createDoStatement(statements, node);
23
23
  };
24
24
  exports.transformBlock = transformBlock;
@@ -4,7 +4,6 @@ exports.getCalledExpression = exports.transformCallExpression = exports.transfor
4
4
  const ts = require("typescript");
5
5
  const lua = require("../../LuaAST");
6
6
  const builtins_1 = require("../builtins");
7
- const annotations_1 = require("../utils/annotations");
8
7
  const assignment_validation_1 = require("../utils/assignment-validation");
9
8
  const function_context_1 = require("../utils/function-context");
10
9
  const lua_ast_1 = require("../utils/lua-ast");
@@ -16,17 +15,17 @@ const diagnostics_1 = require("../utils/diagnostics");
16
15
  const expression_list_1 = require("./expression-list");
17
16
  const preceding_statements_1 = require("../utils/preceding-statements");
18
17
  const optional_chaining_1 = require("./optional-chaining");
19
- const language_extensions_1 = require("./language-extensions");
20
18
  const import_1 = require("./modules/import");
19
+ const call_extension_1 = require("./language-extensions/call-extension");
21
20
  function validateArguments(context, params, signature) {
22
21
  if (!signature || signature.parameters.length < params.length) {
23
22
  return;
24
23
  }
25
24
  for (const [index, param] of params.entries()) {
26
25
  const signatureParameter = signature.parameters[index];
27
- const paramType = context.checker.getTypeAtLocation(param);
28
26
  if (signatureParameter.valueDeclaration !== undefined) {
29
27
  const signatureType = context.checker.getTypeAtLocation(signatureParameter.valueDeclaration);
28
+ const paramType = context.checker.getTypeAtLocation(param);
30
29
  (0, assignment_validation_1.validateAssignment)(context, param, paramType, signatureType, signatureParameter.name);
31
30
  }
32
31
  }
@@ -157,20 +156,14 @@ const transformCallExpression = (node, context) => {
157
156
  ? (0, optional_chaining_1.getOptionalContinuationData)(calledExpression)
158
157
  : undefined;
159
158
  const wrapResultInTable = (0, multi_1.isMultiReturnCall)(context, node) && (0, multi_1.shouldMultiReturnCallBeWrapped)(context, node);
160
- if ((0, annotations_1.isTupleReturnCall)(context, node)) {
161
- context.diagnostics.push((0, diagnostics_1.annotationRemoved)(node, annotations_1.AnnotationKind.TupleReturn));
162
- }
163
- const builtinOrExtensionResult = (_a = (0, builtins_1.transformBuiltinCallExpression)(context, node, optionalContinuation !== undefined)) !== null && _a !== void 0 ? _a : (0, language_extensions_1.transformLanguageExtensionCallExpression)(context, node, optionalContinuation !== undefined);
159
+ const builtinOrExtensionResult = (_a = (0, builtins_1.transformBuiltinCallExpression)(context, node)) !== null && _a !== void 0 ? _a : (0, call_extension_1.transformLanguageExtensionCallExpression)(context, node);
164
160
  if (builtinOrExtensionResult) {
165
- // unsupportedOptionalCall diagnostic already present
161
+ if (optionalContinuation !== undefined) {
162
+ context.diagnostics.push((0, diagnostics_1.unsupportedBuiltinOptionalCall)(node));
163
+ }
166
164
  return wrapResultInTable ? (0, lua_ast_1.wrapInTable)(builtinOrExtensionResult) : builtinOrExtensionResult;
167
165
  }
168
166
  if (ts.isPropertyAccessExpression(calledExpression)) {
169
- const ownerType = context.checker.getTypeAtLocation(calledExpression.expression);
170
- const annotations = (0, annotations_1.getTypeAnnotations)(ownerType);
171
- if (annotations.has(annotations_1.AnnotationKind.LuaTable)) {
172
- context.diagnostics.push((0, diagnostics_1.annotationRemoved)(node, annotations_1.AnnotationKind.LuaTable));
173
- }
174
167
  const result = transformPropertyCall(context, node, calledExpression);
175
168
  return wrapResultInTable ? (0, lua_ast_1.wrapInTable)(result) : result;
176
169
  }
@@ -211,11 +204,7 @@ function isContextualCallExpression(context, signature) {
211
204
  return (0, function_context_1.getDeclarationContextType)(context, declaration) !== function_context_1.ContextType.Void;
212
205
  }
213
206
  function getCalledExpression(node) {
214
- function unwrapExpression(expression) {
215
- expression = ts.skipOuterExpressions(expression);
216
- return ts.isNonNullExpression(expression) ? unwrapExpression(expression.expression) : expression;
217
- }
218
- return unwrapExpression(node.expression);
207
+ return ts.skipOuterExpressions(node.expression);
219
208
  }
220
209
  exports.getCalledExpression = getCalledExpression;
221
210
  //# sourceMappingURL=call.js.map
@@ -3,9 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.transformSuperExpression = exports.transformClassAsExpression = exports.transformThisExpression = exports.transformClassDeclaration = void 0;
4
4
  const ts = require("typescript");
5
5
  const lua = require("../../../LuaAST");
6
- const utils_1 = require("../../../utils");
7
- const annotations_1 = require("../../utils/annotations");
8
- const diagnostics_1 = require("../../utils/diagnostics");
9
6
  const export_1 = require("../../utils/export");
10
7
  const lua_ast_1 = require("../../utils/lua-ast");
11
8
  const safe_names_1 = require("../../utils/safe-names");
@@ -15,7 +12,7 @@ const accessors_1 = require("./members/accessors");
15
12
  const constructor_1 = require("./members/constructor");
16
13
  const fields_1 = require("./members/fields");
17
14
  const method_1 = require("./members/method");
18
- const utils_2 = require("./utils");
15
+ const utils_1 = require("./utils");
19
16
  const setup_1 = require("./setup");
20
17
  const transformClassDeclaration = (declaration, context) => {
21
18
  // If declaration is a default export, transform to export variable assignment instead
@@ -36,7 +33,6 @@ function transformClassAsExpression(expression, context) {
36
33
  return name;
37
34
  }
38
35
  exports.transformClassAsExpression = transformClassAsExpression;
39
- const classSuperInfos = new WeakMap();
40
36
  function transformClassLikeDeclaration(classDeclaration, context, nameOverride) {
41
37
  let className;
42
38
  if (nameOverride !== undefined) {
@@ -48,22 +44,14 @@ function transformClassLikeDeclaration(classDeclaration, context, nameOverride)
48
44
  else {
49
45
  className = lua.createIdentifier(context.createTempName("class"), classDeclaration);
50
46
  }
51
- const annotations = (0, annotations_1.getTypeAnnotations)(context.checker.getTypeAtLocation(classDeclaration));
52
- if (annotations.has(annotations_1.AnnotationKind.Extension)) {
53
- context.diagnostics.push((0, diagnostics_1.annotationRemoved)(classDeclaration, annotations_1.AnnotationKind.Extension));
54
- }
55
- if (annotations.has(annotations_1.AnnotationKind.MetaExtension)) {
56
- context.diagnostics.push((0, diagnostics_1.annotationRemoved)(classDeclaration, annotations_1.AnnotationKind.MetaExtension));
57
- }
58
47
  // Get type that is extended
59
- const extendedTypeNode = (0, utils_2.getExtendedNode)(context, classDeclaration);
60
- const extendedType = (0, utils_2.getExtendedType)(context, classDeclaration);
61
- const superInfo = (0, utils_1.getOrUpdate)(classSuperInfos, context, () => []);
62
- superInfo.push({ className, extendedTypeNode });
48
+ const extendedTypeNode = (0, utils_1.getExtendedNode)(classDeclaration);
49
+ const extendedType = (0, utils_1.getExtendedType)(context, classDeclaration);
50
+ context.classSuperInfos.push({ className, extendedTypeNode });
63
51
  // Get all properties with value
64
52
  const properties = classDeclaration.members.filter(ts.isPropertyDeclaration).filter(member => member.initializer);
65
53
  // Divide properties into static and non-static
66
- const instanceFields = properties.filter(prop => !(0, utils_2.isStaticNode)(prop));
54
+ const instanceFields = properties.filter(prop => !(0, utils_1.isStaticNode)(prop));
67
55
  const result = [];
68
56
  let localClassName;
69
57
  if ((0, safe_names_1.isUnsafeName)(className.text, context.options)) {
@@ -129,7 +117,7 @@ function transformClassLikeDeclaration(classDeclaration, context, nameOverride)
129
117
  }
130
118
  }
131
119
  else if (ts.isPropertyDeclaration(member)) {
132
- if ((0, utils_2.isStaticNode)(member)) {
120
+ if ((0, utils_1.isStaticNode)(member)) {
133
121
  const statement = (0, fields_1.transformStaticPropertyDeclaration)(context, member, localClassName);
134
122
  if (statement)
135
123
  decorationStatements.push(statement);
@@ -153,11 +141,11 @@ function transformClassLikeDeclaration(classDeclaration, context, nameOverride)
153
141
  result.push(classAssignment);
154
142
  }
155
143
  }
156
- superInfo.pop();
144
+ context.classSuperInfos.pop();
157
145
  return { statements: result, name: className };
158
146
  }
159
147
  const transformSuperExpression = (expression, context) => {
160
- const superInfos = (0, utils_1.getOrUpdate)(classSuperInfos, context, () => []);
148
+ const superInfos = context.classSuperInfos;
161
149
  const superInfo = superInfos[superInfos.length - 1];
162
150
  if (!superInfo)
163
151
  return lua.createAnonymousIdentifier(expression);
@@ -22,7 +22,7 @@ function transformConstructorDeclaration(context, statement, className, instance
22
22
  return undefined;
23
23
  }
24
24
  // Transform body
25
- const scope = (0, scope_1.pushScope)(context, scope_1.ScopeType.Function);
25
+ const scope = context.pushScope(scope_1.ScopeType.Function);
26
26
  const body = (0, function_1.transformFunctionBodyContent)(context, statement.body);
27
27
  const [params, dotsLiteral, restParamName] = (0, function_1.transformParameters)(context, statement.parameters, (0, lua_ast_1.createSelfIdentifier)());
28
28
  // Make sure default parameters are assigned before fields are initialized
@@ -58,7 +58,7 @@ function transformConstructorDeclaration(context, statement, className, instance
58
58
  bodyWithFieldInitializers.push(...body);
59
59
  const block = lua.createBlock(bodyWithFieldInitializers);
60
60
  const constructorWasGenerated = statement.pos === -1;
61
- (0, scope_1.popScope)(context);
61
+ context.popScope();
62
62
  return lua.createAssignmentStatement(createConstructorName(className), lua.createFunctionExpression(block, params, dotsLiteral, lua.NodeFlags.Declaration), constructorWasGenerated ? classDeclaration : statement);
63
63
  }
64
64
  exports.transformConstructorDeclaration = transformConstructorDeclaration;
@@ -10,16 +10,13 @@ const call_1 = require("../call");
10
10
  const table_1 = require("../language-extensions/table");
11
11
  const transformNewExpression = (node, context) => {
12
12
  var _a, _b;
13
- const type = context.checker.getTypeAtLocation(node);
14
- const annotations = (0, annotations_1.getTypeAnnotations)(type);
15
- if (annotations.has(annotations_1.AnnotationKind.LuaTable)) {
16
- context.diagnostics.push((0, diagnostics_1.annotationRemoved)(node, annotations_1.AnnotationKind.LuaTable));
17
- }
18
13
  if ((0, table_1.isTableNewCall)(context, node)) {
19
14
  return lua.createTableExpression(undefined, node);
20
15
  }
21
16
  const signature = context.checker.getResolvedSignature(node);
22
17
  const [name, params] = (0, call_1.transformCallAndArguments)(context, node.expression, (_a = node.arguments) !== null && _a !== void 0 ? _a : [ts.factory.createTrue()], signature);
18
+ const type = context.checker.getTypeAtLocation(node);
19
+ const annotations = (0, annotations_1.getTypeAnnotations)(type);
23
20
  const customConstructorAnnotation = annotations.get(annotations_1.AnnotationKind.CustomConstructor);
24
21
  if (customConstructorAnnotation) {
25
22
  if (customConstructorAnnotation.args.length === 1) {
@@ -2,4 +2,4 @@ import * as ts from "typescript";
2
2
  import * as lua from "../../../LuaAST";
3
3
  import { TransformationContext } from "../../context";
4
4
  export declare function createClassSetup(context: TransformationContext, statement: ts.ClassLikeDeclarationBase, className: lua.Identifier, localClassName: lua.Identifier, extendsType?: ts.Type): lua.Statement[];
5
- export declare function getReflectionClassName(context: TransformationContext, declaration: ts.ClassLikeDeclarationBase, className: lua.Identifier): lua.Expression;
5
+ export declare function getReflectionClassName(declaration: ts.ClassLikeDeclarationBase, className: lua.Identifier): lua.Expression;
@@ -34,16 +34,16 @@ function createClassSetup(context, statement, className, localClassName, extends
34
34
  }
35
35
  }
36
36
  // localClassName.name = className
37
- result.push(lua.createAssignmentStatement(lua.createTableIndexExpression(lua.cloneIdentifier(localClassName), lua.createStringLiteral("name")), getReflectionClassName(context, statement, className), statement));
37
+ result.push(lua.createAssignmentStatement(lua.createTableIndexExpression(lua.cloneIdentifier(localClassName), lua.createStringLiteral("name")), getReflectionClassName(statement, className), statement));
38
38
  if (extendsType) {
39
- const extendedNode = (0, utils_2.getExtendedNode)(context, statement);
39
+ const extendedNode = (0, utils_2.getExtendedNode)(statement);
40
40
  (0, utils_1.assert)(extendedNode);
41
41
  result.push(lua.createExpressionStatement((0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ClassExtends, (0, utils_2.getExtendsClause)(statement), lua.cloneIdentifier(localClassName), context.transformExpression(extendedNode.expression))));
42
42
  }
43
43
  return result;
44
44
  }
45
45
  exports.createClassSetup = createClassSetup;
46
- function getReflectionClassName(context, declaration, className) {
46
+ function getReflectionClassName(declaration, className) {
47
47
  if (declaration.name) {
48
48
  return lua.createStringLiteral(declaration.name.text);
49
49
  }
@@ -53,7 +53,7 @@ function getReflectionClassName(context, declaration, className) {
53
53
  else if ((0, export_1.hasDefaultExportModifier)(declaration)) {
54
54
  return lua.createStringLiteral("default");
55
55
  }
56
- if ((0, utils_2.getExtendedNode)(context, declaration)) {
56
+ if ((0, utils_2.getExtendedNode)(declaration)) {
57
57
  return lua.createTableIndexExpression(className, lua.createStringLiteral("name"));
58
58
  }
59
59
  return lua.createStringLiteral("");
@@ -2,5 +2,5 @@ import * as ts from "typescript";
2
2
  import { TransformationContext } from "../../context";
3
3
  export declare function isStaticNode(node: ts.Node): boolean;
4
4
  export declare function getExtendsClause(node: ts.ClassLikeDeclarationBase): ts.HeritageClause | undefined;
5
- export declare function getExtendedNode(context: TransformationContext, node: ts.ClassLikeDeclarationBase): ts.ExpressionWithTypeArguments | undefined;
5
+ export declare function getExtendedNode(node: ts.ClassLikeDeclarationBase): ts.ExpressionWithTypeArguments | undefined;
6
6
  export declare function getExtendedType(context: TransformationContext, node: ts.ClassLikeDeclarationBase): ts.Type | undefined;
@@ -2,8 +2,6 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getExtendedType = exports.getExtendedNode = exports.getExtendsClause = exports.isStaticNode = void 0;
4
4
  const ts = require("typescript");
5
- const annotations_1 = require("../../utils/annotations");
6
- const diagnostics_1 = require("../../utils/diagnostics");
7
5
  function isStaticNode(node) {
8
6
  var _a;
9
7
  return ((_a = node.modifiers) !== null && _a !== void 0 ? _a : []).some(m => m.kind === ts.SyntaxKind.StaticKeyword);
@@ -14,20 +12,15 @@ function getExtendsClause(node) {
14
12
  return ((_a = node.heritageClauses) !== null && _a !== void 0 ? _a : []).find(clause => clause.token === ts.SyntaxKind.ExtendsKeyword);
15
13
  }
16
14
  exports.getExtendsClause = getExtendsClause;
17
- function getExtendedNode(context, node) {
15
+ function getExtendedNode(node) {
18
16
  const extendsClause = getExtendsClause(node);
19
17
  if (!extendsClause)
20
18
  return;
21
- const superType = context.checker.getTypeAtLocation(extendsClause.types[0]);
22
- const annotations = (0, annotations_1.getTypeAnnotations)(superType);
23
- if (annotations.has(annotations_1.AnnotationKind.PureAbstract)) {
24
- context.diagnostics.push((0, diagnostics_1.annotationRemoved)(extendsClause, annotations_1.AnnotationKind.PureAbstract));
25
- }
26
19
  return extendsClause.types[0];
27
20
  }
28
21
  exports.getExtendedNode = getExtendedNode;
29
22
  function getExtendedType(context, node) {
30
- const extendedNode = getExtendedNode(context, node);
23
+ const extendedNode = getExtendedNode(node);
31
24
  return extendedNode && context.checker.getTypeAtLocation(extendedNode);
32
25
  }
33
26
  exports.getExtendedType = getExtendedType;