typescript-to-lua 1.6.3 → 1.7.2

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 (98) 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 +10 -21
  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 +23 -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/modules/import.js +2 -2
  81. package/dist/transformation/visitors/namespace.js +5 -14
  82. package/dist/transformation/visitors/return.js +2 -3
  83. package/dist/transformation/visitors/sourceFile.js +3 -4
  84. package/dist/transformation/visitors/spread.js +0 -5
  85. package/dist/transformation/visitors/switch.js +2 -2
  86. package/dist/transformation/visitors/variable-declaration.js +1 -1
  87. package/dist/transpilation/plugins.js +3 -0
  88. package/dist/transpilation/resolve.js +210 -186
  89. package/dist/transpilation/transpile.js +10 -0
  90. package/dist/transpilation/transpiler.d.ts +1 -0
  91. package/dist/transpilation/transpiler.js +18 -5
  92. package/dist/tstl.js +21 -0
  93. package/language-extensions/index.d.ts +167 -67
  94. package/package.json +2 -2
  95. package/dist/transformation/visitors/language-extensions/index.d.ts +0 -4
  96. package/dist/transformation/visitors/language-extensions/index.js +0 -17
  97. package/dist/transformation/visitors/language-extensions/pairsIterable.d.ts +0 -5
  98. package/dist/transformation/visitors/language-extensions/pairsIterable.js +0 -53
@@ -27,9 +27,22 @@ function hasNoSelfAncestor(declaration) {
27
27
  }
28
28
  }
29
29
  function getExplicitThisParameter(signatureDeclaration) {
30
- return signatureDeclaration.parameters.find(param => ts.isIdentifier(param.name) && param.name.originalKeywordKind === ts.SyntaxKind.ThisKeyword);
30
+ const param = signatureDeclaration.parameters[0];
31
+ if (param && ts.isIdentifier(param.name) && param.name.originalKeywordKind === ts.SyntaxKind.ThisKeyword) {
32
+ return param;
33
+ }
34
+ }
35
+ const signatureDeclarationContextTypes = new WeakMap();
36
+ function getDeclarationContextType(context, signatureDeclaration) {
37
+ const known = signatureDeclarationContextTypes.get(signatureDeclaration);
38
+ if (known !== undefined)
39
+ return known;
40
+ const contextType = computeDeclarationContextType(context, signatureDeclaration);
41
+ signatureDeclarationContextTypes.set(signatureDeclaration, contextType);
42
+ return contextType;
31
43
  }
32
- function getDeclarationContextType({ program }, signatureDeclaration) {
44
+ exports.getDeclarationContextType = getDeclarationContextType;
45
+ function computeDeclarationContextType(context, signatureDeclaration) {
33
46
  const thisParameter = getExplicitThisParameter(signatureDeclaration);
34
47
  if (thisParameter) {
35
48
  // Explicit 'this'
@@ -49,15 +62,13 @@ function getDeclarationContextType({ program }, signatureDeclaration) {
49
62
  (signatureDeclaration.parent && ts.isPropertySignature(signatureDeclaration.parent))) {
50
63
  // Class/interface methods only respect @noSelf on their parent
51
64
  const scopeDeclaration = (0, typescript_1.findFirstNodeAbove)(signatureDeclaration, (n) => ts.isClassDeclaration(n) || ts.isClassExpression(n) || ts.isInterfaceDeclaration(n));
52
- if (scopeDeclaration === undefined) {
53
- return ContextType.NonVoid;
54
- }
55
- if ((0, annotations_1.getNodeAnnotations)(scopeDeclaration).has(annotations_1.AnnotationKind.NoSelf)) {
65
+ if (scopeDeclaration !== undefined && (0, annotations_1.getNodeAnnotations)(scopeDeclaration).has(annotations_1.AnnotationKind.NoSelf)) {
56
66
  return ContextType.Void;
57
67
  }
58
68
  return ContextType.NonVoid;
59
69
  }
60
70
  // When using --noImplicitSelf and the signature is defined in a file targeted by the program apply the @noSelf rule.
71
+ const program = context.program;
61
72
  const options = program.getCompilerOptions();
62
73
  if (options.noImplicitSelf) {
63
74
  const sourceFile = program.getSourceFile(signatureDeclaration.getSourceFile().fileName);
@@ -73,61 +84,56 @@ function getDeclarationContextType({ program }, signatureDeclaration) {
73
84
  }
74
85
  return ContextType.NonVoid;
75
86
  }
76
- exports.getDeclarationContextType = getDeclarationContextType;
77
87
  function reduceContextTypes(contexts) {
78
- const reducer = (a, b) => {
79
- if (a === ContextType.None) {
80
- return b;
81
- }
82
- else if (b === ContextType.None) {
83
- return a;
84
- }
85
- else if (a !== b) {
86
- return ContextType.Mixed;
87
- }
88
- else {
89
- return a;
90
- }
91
- };
92
- return contexts.reduce(reducer, ContextType.None);
88
+ let type = ContextType.None;
89
+ for (const context of contexts) {
90
+ type |= context;
91
+ if (type === ContextType.Mixed)
92
+ break;
93
+ }
94
+ return type;
93
95
  }
94
- function getSignatureDeclarations(context, signatures) {
95
- return signatures.flatMap(signature => {
96
- const signatureDeclaration = signature.getDeclaration();
97
- let inferredType;
98
- if (ts.isMethodDeclaration(signatureDeclaration) &&
99
- ts.isObjectLiteralExpression(signatureDeclaration.parent) &&
100
- !getExplicitThisParameter(signatureDeclaration)) {
101
- inferredType = context.checker.getContextualTypeForObjectLiteralElement(signatureDeclaration);
102
- }
103
- else if ((ts.isFunctionExpression(signatureDeclaration) || ts.isArrowFunction(signatureDeclaration)) &&
104
- !getExplicitThisParameter(signatureDeclaration)) {
105
- // Infer type of function expressions/arrow functions
106
- inferredType = (0, typescript_1.inferAssignedType)(context, signatureDeclaration);
107
- }
108
- if (inferredType) {
109
- const inferredSignatures = (0, typescript_1.getAllCallSignatures)(inferredType);
110
- if (inferredSignatures.length > 0) {
111
- return inferredSignatures.map(s => s.getDeclaration());
112
- }
96
+ function getSignatureDeclarations(context, signature) {
97
+ const signatureDeclaration = signature.getDeclaration();
98
+ let inferredType;
99
+ if (ts.isMethodDeclaration(signatureDeclaration) &&
100
+ ts.isObjectLiteralExpression(signatureDeclaration.parent) &&
101
+ !getExplicitThisParameter(signatureDeclaration)) {
102
+ inferredType = context.checker.getContextualTypeForObjectLiteralElement(signatureDeclaration);
103
+ }
104
+ else if ((ts.isFunctionExpression(signatureDeclaration) || ts.isArrowFunction(signatureDeclaration)) &&
105
+ !getExplicitThisParameter(signatureDeclaration)) {
106
+ // Infer type of function expressions/arrow functions
107
+ inferredType = (0, typescript_1.inferAssignedType)(context, signatureDeclaration);
108
+ }
109
+ if (inferredType) {
110
+ const inferredSignatures = (0, typescript_1.getAllCallSignatures)(inferredType);
111
+ if (inferredSignatures.length > 0) {
112
+ return inferredSignatures.map(s => s.getDeclaration());
113
113
  }
114
- return signatureDeclaration;
115
- });
114
+ }
115
+ return [signatureDeclaration];
116
116
  }
117
+ const typeContextTypes = new WeakMap();
117
118
  function getFunctionContextType(context, type) {
118
- var _a;
119
+ const known = typeContextTypes.get(type);
120
+ if (known !== undefined)
121
+ return known;
122
+ const contextType = computeFunctionContextType(context, type);
123
+ typeContextTypes.set(type, contextType);
124
+ return contextType;
125
+ }
126
+ exports.getFunctionContextType = getFunctionContextType;
127
+ function computeFunctionContextType(context, type) {
119
128
  if (type.isTypeParameter()) {
120
- type = (_a = type.getConstraint()) !== null && _a !== void 0 ? _a : type;
121
- }
122
- if (type.isUnion()) {
123
- return reduceContextTypes(type.types.map(t => getFunctionContextType(context, t)));
129
+ const constraint = type.getConstraint();
130
+ if (constraint)
131
+ return getFunctionContextType(context, constraint);
124
132
  }
125
133
  const signatures = context.checker.getSignaturesOfType(type, ts.SignatureKind.Call);
126
134
  if (signatures.length === 0) {
127
135
  return ContextType.None;
128
136
  }
129
- const signatureDeclarations = getSignatureDeclarations(context, signatures);
130
- return reduceContextTypes(signatureDeclarations.map(s => getDeclarationContextType(context, s)));
137
+ return reduceContextTypes(signatures.flatMap(s => getSignatureDeclarations(context, s)).map(s => getDeclarationContextType(context, s)));
131
138
  }
132
- exports.getFunctionContextType = getFunctionContextType;
133
139
  //# sourceMappingURL=function-context.js.map
@@ -2,57 +2,63 @@ import * as ts from "typescript";
2
2
  import { TransformationContext } from "../context";
3
3
  export declare enum ExtensionKind {
4
4
  MultiFunction = "MultiFunction",
5
- MultiType = "MultiType",
6
5
  RangeFunction = "RangeFunction",
7
6
  VarargConstant = "VarargConstant",
8
- IterableType = "IterableType",
9
- PairsIterableType = "PairsIterableType",
10
- AdditionOperatorType = "AdditionOperatorType",
11
- AdditionOperatorMethodType = "AdditionOperatorMethodType",
12
- SubtractionOperatorType = "SubtractionOperatorType",
13
- SubtractionOperatorMethodType = "SubtractionOperatorMethodType",
14
- MultiplicationOperatorType = "MultiplicationOperatorType",
15
- MultiplicationOperatorMethodType = "MultiplicationOperatorMethodType",
16
- DivisionOperatorType = "DivisionOperatorType",
17
- DivisionOperatorMethodType = "DivisionOperatorMethodType",
18
- ModuloOperatorType = "ModuloOperatorType",
19
- ModuloOperatorMethodType = "ModuloOperatorMethodType",
20
- PowerOperatorType = "PowerOperatorType",
21
- PowerOperatorMethodType = "PowerOperatorMethodType",
22
- FloorDivisionOperatorType = "FloorDivisionOperatorType",
23
- FloorDivisionOperatorMethodType = "FloorDivisionOperatorMethodType",
24
- BitwiseAndOperatorType = "BitwiseAndOperatorType",
25
- BitwiseAndOperatorMethodType = "BitwiseAndOperatorMethodType",
26
- BitwiseOrOperatorType = "BitwiseOrOperatorType",
27
- BitwiseOrOperatorMethodType = "BitwiseOrOperatorMethodType",
28
- BitwiseExclusiveOrOperatorType = "BitwiseExclusiveOrOperatorType",
29
- BitwiseExclusiveOrOperatorMethodType = "BitwiseExclusiveOrOperatorMethodType",
30
- BitwiseLeftShiftOperatorType = "BitwiseLeftShiftOperatorType",
31
- BitwiseLeftShiftOperatorMethodType = "BitwiseLeftShiftOperatorMethodType",
32
- BitwiseRightShiftOperatorType = "BitwiseRightShiftOperatorType",
33
- BitwiseRightShiftOperatorMethodType = "BitwiseRightShiftOperatorMethodType",
34
- ConcatOperatorType = "ConcatOperatorType",
35
- ConcatOperatorMethodType = "ConcatOperatorMethodType",
36
- LessThanOperatorType = "LessThanOperatorType",
37
- LessThanOperatorMethodType = "LessThanOperatorMethodType",
38
- GreaterThanOperatorType = "GreaterThanOperatorType",
39
- GreaterThanOperatorMethodType = "GreaterThanOperatorMethodType",
40
- NegationOperatorType = "NegationOperatorType",
41
- NegationOperatorMethodType = "NegationOperatorMethodType",
42
- BitwiseNotOperatorType = "BitwiseNotOperatorType",
43
- BitwiseNotOperatorMethodType = "BitwiseNotOperatorMethodType",
44
- LengthOperatorType = "LengthOperatorType",
45
- LengthOperatorMethodType = "LengthOperatorMethodType",
46
- TableNewType = "TableNewType",
47
- TableDeleteType = "TableDeleteType",
48
- TableDeleteMethodType = "TableDeleteMethodType",
49
- TableGetType = "TableGetType",
50
- TableGetMethodType = "TableGetMethodType",
51
- TableHasType = "TableHasType",
52
- TableHasMethodType = "TableHasMethodType",
53
- TableSetType = "TableSetType",
54
- TableSetMethodType = "TableSetMethodType"
7
+ AdditionOperatorType = "Addition",
8
+ AdditionOperatorMethodType = "AdditionMethod",
9
+ SubtractionOperatorType = "Subtraction",
10
+ SubtractionOperatorMethodType = "SubtractionMethod",
11
+ MultiplicationOperatorType = "Multiplication",
12
+ MultiplicationOperatorMethodType = "MultiplicationMethod",
13
+ DivisionOperatorType = "Division",
14
+ DivisionOperatorMethodType = "DivisionMethod",
15
+ ModuloOperatorType = "Modulo",
16
+ ModuloOperatorMethodType = "ModuloMethod",
17
+ PowerOperatorType = "Power",
18
+ PowerOperatorMethodType = "PowerMethod",
19
+ FloorDivisionOperatorType = "FloorDivision",
20
+ FloorDivisionOperatorMethodType = "FloorDivisionMethod",
21
+ BitwiseAndOperatorType = "BitwiseAnd",
22
+ BitwiseAndOperatorMethodType = "BitwiseAndMethod",
23
+ BitwiseOrOperatorType = "BitwiseOr",
24
+ BitwiseOrOperatorMethodType = "BitwiseOrMethod",
25
+ BitwiseExclusiveOrOperatorType = "BitwiseExclusiveOr",
26
+ BitwiseExclusiveOrOperatorMethodType = "BitwiseExclusiveOrMethod",
27
+ BitwiseLeftShiftOperatorType = "BitwiseLeftShift",
28
+ BitwiseLeftShiftOperatorMethodType = "BitwiseLeftShiftMethod",
29
+ BitwiseRightShiftOperatorType = "BitwiseRightShift",
30
+ BitwiseRightShiftOperatorMethodType = "BitwiseRightShiftMethod",
31
+ ConcatOperatorType = "Concat",
32
+ ConcatOperatorMethodType = "ConcatMethod",
33
+ LessThanOperatorType = "LessThan",
34
+ LessThanOperatorMethodType = "LessThanMethod",
35
+ GreaterThanOperatorType = "GreaterThan",
36
+ GreaterThanOperatorMethodType = "GreaterThanMethod",
37
+ NegationOperatorType = "Negation",
38
+ NegationOperatorMethodType = "NegationMethod",
39
+ BitwiseNotOperatorType = "BitwiseNot",
40
+ BitwiseNotOperatorMethodType = "BitwiseNotMethod",
41
+ LengthOperatorType = "Length",
42
+ LengthOperatorMethodType = "LengthMethod",
43
+ TableNewType = "TableNew",
44
+ TableDeleteType = "TableDelete",
45
+ TableDeleteMethodType = "TableDeleteMethod",
46
+ TableGetType = "TableGet",
47
+ TableGetMethodType = "TableGetMethod",
48
+ TableHasType = "TableHas",
49
+ TableHasMethodType = "TableHasMethod",
50
+ TableSetType = "TableSet",
51
+ TableSetMethodType = "TableSetMethod",
52
+ TableAddKeyType = "TableAddKey",
53
+ TableAddKeyMethodType = "TableAddKeyMethod"
55
54
  }
56
- export declare function isExtensionType(type: ts.Type, extensionKind: ExtensionKind): boolean;
57
- export declare function getExtensionKinds(type: ts.Type): ExtensionKind[];
58
- export declare function isExtensionValue(context: TransformationContext, symbol: ts.Symbol, extensionKind: ExtensionKind): boolean;
55
+ export declare function getExtensionKindForType(context: TransformationContext, type: ts.Type): ExtensionKind | undefined;
56
+ export declare function getExtensionKindForNode(context: TransformationContext, node: ts.Node): ExtensionKind | undefined;
57
+ export declare function getExtensionKindForSymbol(context: TransformationContext, symbol: ts.Symbol): ExtensionKind | undefined;
58
+ export declare enum IterableExtensionKind {
59
+ Iterable = "Iterable",
60
+ Pairs = "Pairs",
61
+ PairsKey = "PairsKey"
62
+ }
63
+ export declare function getIterableExtensionTypeForType(context: TransformationContext, type: ts.Type): IterableExtensionKind | undefined;
64
+ export declare function getIterableExtensionKindForNode(context: TransformationContext, node: ts.Node): IterableExtensionKind | undefined;
@@ -1,131 +1,111 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isExtensionValue = exports.getExtensionKinds = exports.isExtensionType = exports.ExtensionKind = void 0;
3
+ exports.getIterableExtensionKindForNode = exports.getIterableExtensionTypeForType = exports.IterableExtensionKind = exports.getExtensionKindForSymbol = exports.getExtensionKindForNode = exports.getExtensionKindForType = exports.ExtensionKind = void 0;
4
+ const ts = require("typescript");
4
5
  var ExtensionKind;
5
6
  (function (ExtensionKind) {
6
7
  ExtensionKind["MultiFunction"] = "MultiFunction";
7
- ExtensionKind["MultiType"] = "MultiType";
8
8
  ExtensionKind["RangeFunction"] = "RangeFunction";
9
9
  ExtensionKind["VarargConstant"] = "VarargConstant";
10
- ExtensionKind["IterableType"] = "IterableType";
11
- ExtensionKind["PairsIterableType"] = "PairsIterableType";
12
- ExtensionKind["AdditionOperatorType"] = "AdditionOperatorType";
13
- ExtensionKind["AdditionOperatorMethodType"] = "AdditionOperatorMethodType";
14
- ExtensionKind["SubtractionOperatorType"] = "SubtractionOperatorType";
15
- ExtensionKind["SubtractionOperatorMethodType"] = "SubtractionOperatorMethodType";
16
- ExtensionKind["MultiplicationOperatorType"] = "MultiplicationOperatorType";
17
- ExtensionKind["MultiplicationOperatorMethodType"] = "MultiplicationOperatorMethodType";
18
- ExtensionKind["DivisionOperatorType"] = "DivisionOperatorType";
19
- ExtensionKind["DivisionOperatorMethodType"] = "DivisionOperatorMethodType";
20
- ExtensionKind["ModuloOperatorType"] = "ModuloOperatorType";
21
- ExtensionKind["ModuloOperatorMethodType"] = "ModuloOperatorMethodType";
22
- ExtensionKind["PowerOperatorType"] = "PowerOperatorType";
23
- ExtensionKind["PowerOperatorMethodType"] = "PowerOperatorMethodType";
24
- ExtensionKind["FloorDivisionOperatorType"] = "FloorDivisionOperatorType";
25
- ExtensionKind["FloorDivisionOperatorMethodType"] = "FloorDivisionOperatorMethodType";
26
- ExtensionKind["BitwiseAndOperatorType"] = "BitwiseAndOperatorType";
27
- ExtensionKind["BitwiseAndOperatorMethodType"] = "BitwiseAndOperatorMethodType";
28
- ExtensionKind["BitwiseOrOperatorType"] = "BitwiseOrOperatorType";
29
- ExtensionKind["BitwiseOrOperatorMethodType"] = "BitwiseOrOperatorMethodType";
30
- ExtensionKind["BitwiseExclusiveOrOperatorType"] = "BitwiseExclusiveOrOperatorType";
31
- ExtensionKind["BitwiseExclusiveOrOperatorMethodType"] = "BitwiseExclusiveOrOperatorMethodType";
32
- ExtensionKind["BitwiseLeftShiftOperatorType"] = "BitwiseLeftShiftOperatorType";
33
- ExtensionKind["BitwiseLeftShiftOperatorMethodType"] = "BitwiseLeftShiftOperatorMethodType";
34
- ExtensionKind["BitwiseRightShiftOperatorType"] = "BitwiseRightShiftOperatorType";
35
- ExtensionKind["BitwiseRightShiftOperatorMethodType"] = "BitwiseRightShiftOperatorMethodType";
36
- ExtensionKind["ConcatOperatorType"] = "ConcatOperatorType";
37
- ExtensionKind["ConcatOperatorMethodType"] = "ConcatOperatorMethodType";
38
- ExtensionKind["LessThanOperatorType"] = "LessThanOperatorType";
39
- ExtensionKind["LessThanOperatorMethodType"] = "LessThanOperatorMethodType";
40
- ExtensionKind["GreaterThanOperatorType"] = "GreaterThanOperatorType";
41
- ExtensionKind["GreaterThanOperatorMethodType"] = "GreaterThanOperatorMethodType";
42
- ExtensionKind["NegationOperatorType"] = "NegationOperatorType";
43
- ExtensionKind["NegationOperatorMethodType"] = "NegationOperatorMethodType";
44
- ExtensionKind["BitwiseNotOperatorType"] = "BitwiseNotOperatorType";
45
- ExtensionKind["BitwiseNotOperatorMethodType"] = "BitwiseNotOperatorMethodType";
46
- ExtensionKind["LengthOperatorType"] = "LengthOperatorType";
47
- ExtensionKind["LengthOperatorMethodType"] = "LengthOperatorMethodType";
48
- ExtensionKind["TableNewType"] = "TableNewType";
49
- ExtensionKind["TableDeleteType"] = "TableDeleteType";
50
- ExtensionKind["TableDeleteMethodType"] = "TableDeleteMethodType";
51
- ExtensionKind["TableGetType"] = "TableGetType";
52
- ExtensionKind["TableGetMethodType"] = "TableGetMethodType";
53
- ExtensionKind["TableHasType"] = "TableHasType";
54
- ExtensionKind["TableHasMethodType"] = "TableHasMethodType";
55
- ExtensionKind["TableSetType"] = "TableSetType";
56
- ExtensionKind["TableSetMethodType"] = "TableSetMethodType";
10
+ ExtensionKind["AdditionOperatorType"] = "Addition";
11
+ ExtensionKind["AdditionOperatorMethodType"] = "AdditionMethod";
12
+ ExtensionKind["SubtractionOperatorType"] = "Subtraction";
13
+ ExtensionKind["SubtractionOperatorMethodType"] = "SubtractionMethod";
14
+ ExtensionKind["MultiplicationOperatorType"] = "Multiplication";
15
+ ExtensionKind["MultiplicationOperatorMethodType"] = "MultiplicationMethod";
16
+ ExtensionKind["DivisionOperatorType"] = "Division";
17
+ ExtensionKind["DivisionOperatorMethodType"] = "DivisionMethod";
18
+ ExtensionKind["ModuloOperatorType"] = "Modulo";
19
+ ExtensionKind["ModuloOperatorMethodType"] = "ModuloMethod";
20
+ ExtensionKind["PowerOperatorType"] = "Power";
21
+ ExtensionKind["PowerOperatorMethodType"] = "PowerMethod";
22
+ ExtensionKind["FloorDivisionOperatorType"] = "FloorDivision";
23
+ ExtensionKind["FloorDivisionOperatorMethodType"] = "FloorDivisionMethod";
24
+ ExtensionKind["BitwiseAndOperatorType"] = "BitwiseAnd";
25
+ ExtensionKind["BitwiseAndOperatorMethodType"] = "BitwiseAndMethod";
26
+ ExtensionKind["BitwiseOrOperatorType"] = "BitwiseOr";
27
+ ExtensionKind["BitwiseOrOperatorMethodType"] = "BitwiseOrMethod";
28
+ ExtensionKind["BitwiseExclusiveOrOperatorType"] = "BitwiseExclusiveOr";
29
+ ExtensionKind["BitwiseExclusiveOrOperatorMethodType"] = "BitwiseExclusiveOrMethod";
30
+ ExtensionKind["BitwiseLeftShiftOperatorType"] = "BitwiseLeftShift";
31
+ ExtensionKind["BitwiseLeftShiftOperatorMethodType"] = "BitwiseLeftShiftMethod";
32
+ ExtensionKind["BitwiseRightShiftOperatorType"] = "BitwiseRightShift";
33
+ ExtensionKind["BitwiseRightShiftOperatorMethodType"] = "BitwiseRightShiftMethod";
34
+ ExtensionKind["ConcatOperatorType"] = "Concat";
35
+ ExtensionKind["ConcatOperatorMethodType"] = "ConcatMethod";
36
+ ExtensionKind["LessThanOperatorType"] = "LessThan";
37
+ ExtensionKind["LessThanOperatorMethodType"] = "LessThanMethod";
38
+ ExtensionKind["GreaterThanOperatorType"] = "GreaterThan";
39
+ ExtensionKind["GreaterThanOperatorMethodType"] = "GreaterThanMethod";
40
+ ExtensionKind["NegationOperatorType"] = "Negation";
41
+ ExtensionKind["NegationOperatorMethodType"] = "NegationMethod";
42
+ ExtensionKind["BitwiseNotOperatorType"] = "BitwiseNot";
43
+ ExtensionKind["BitwiseNotOperatorMethodType"] = "BitwiseNotMethod";
44
+ ExtensionKind["LengthOperatorType"] = "Length";
45
+ ExtensionKind["LengthOperatorMethodType"] = "LengthMethod";
46
+ ExtensionKind["TableNewType"] = "TableNew";
47
+ ExtensionKind["TableDeleteType"] = "TableDelete";
48
+ ExtensionKind["TableDeleteMethodType"] = "TableDeleteMethod";
49
+ ExtensionKind["TableGetType"] = "TableGet";
50
+ ExtensionKind["TableGetMethodType"] = "TableGetMethod";
51
+ ExtensionKind["TableHasType"] = "TableHas";
52
+ ExtensionKind["TableHasMethodType"] = "TableHasMethod";
53
+ ExtensionKind["TableSetType"] = "TableSet";
54
+ ExtensionKind["TableSetMethodType"] = "TableSetMethod";
55
+ ExtensionKind["TableAddKeyType"] = "TableAddKey";
56
+ ExtensionKind["TableAddKeyMethodType"] = "TableAddKeyMethod";
57
57
  })(ExtensionKind = exports.ExtensionKind || (exports.ExtensionKind = {}));
58
- const extensionKindToValueName = {
59
- [ExtensionKind.MultiFunction]: "$multi",
60
- [ExtensionKind.RangeFunction]: "$range",
61
- [ExtensionKind.VarargConstant]: "$vararg",
62
- };
63
- const extensionKindToTypeBrand = {
64
- [ExtensionKind.MultiFunction]: "__luaMultiFunctionBrand",
65
- [ExtensionKind.MultiType]: "__luaMultiReturnBrand",
66
- [ExtensionKind.RangeFunction]: "__luaRangeFunctionBrand",
67
- [ExtensionKind.VarargConstant]: "__luaVarargConstantBrand",
68
- [ExtensionKind.IterableType]: "__luaIterableBrand",
69
- [ExtensionKind.PairsIterableType]: "__luaPairsIterableBrand",
70
- [ExtensionKind.AdditionOperatorType]: "__luaAdditionBrand",
71
- [ExtensionKind.AdditionOperatorMethodType]: "__luaAdditionMethodBrand",
72
- [ExtensionKind.SubtractionOperatorType]: "__luaSubtractionBrand",
73
- [ExtensionKind.SubtractionOperatorMethodType]: "__luaSubtractionMethodBrand",
74
- [ExtensionKind.MultiplicationOperatorType]: "__luaMultiplicationBrand",
75
- [ExtensionKind.MultiplicationOperatorMethodType]: "__luaMultiplicationMethodBrand",
76
- [ExtensionKind.DivisionOperatorType]: "__luaDivisionBrand",
77
- [ExtensionKind.DivisionOperatorMethodType]: "__luaDivisionMethodBrand",
78
- [ExtensionKind.ModuloOperatorType]: "__luaModuloBrand",
79
- [ExtensionKind.ModuloOperatorMethodType]: "__luaModuloMethodBrand",
80
- [ExtensionKind.PowerOperatorType]: "__luaPowerBrand",
81
- [ExtensionKind.PowerOperatorMethodType]: "__luaPowerMethodBrand",
82
- [ExtensionKind.FloorDivisionOperatorType]: "__luaFloorDivisionBrand",
83
- [ExtensionKind.FloorDivisionOperatorMethodType]: "__luaFloorDivisionMethodBrand",
84
- [ExtensionKind.BitwiseAndOperatorType]: "__luaBitwiseAndBrand",
85
- [ExtensionKind.BitwiseAndOperatorMethodType]: "__luaBitwiseAndMethodBrand",
86
- [ExtensionKind.BitwiseOrOperatorType]: "__luaBitwiseOrBrand",
87
- [ExtensionKind.BitwiseOrOperatorMethodType]: "__luaBitwiseOrMethodBrand",
88
- [ExtensionKind.BitwiseExclusiveOrOperatorType]: "__luaBitwiseExclusiveOrBrand",
89
- [ExtensionKind.BitwiseExclusiveOrOperatorMethodType]: "__luaBitwiseExclusiveOrMethodBrand",
90
- [ExtensionKind.BitwiseLeftShiftOperatorType]: "__luaBitwiseLeftShiftBrand",
91
- [ExtensionKind.BitwiseLeftShiftOperatorMethodType]: "__luaBitwiseLeftShiftMethodBrand",
92
- [ExtensionKind.BitwiseRightShiftOperatorType]: "__luaBitwiseRightShiftBrand",
93
- [ExtensionKind.BitwiseRightShiftOperatorMethodType]: "__luaBitwiseRightShiftMethodBrand",
94
- [ExtensionKind.ConcatOperatorType]: "__luaConcatBrand",
95
- [ExtensionKind.ConcatOperatorMethodType]: "__luaConcatMethodBrand",
96
- [ExtensionKind.LessThanOperatorType]: "__luaLessThanBrand",
97
- [ExtensionKind.LessThanOperatorMethodType]: "__luaLessThanMethodBrand",
98
- [ExtensionKind.GreaterThanOperatorType]: "__luaGreaterThanBrand",
99
- [ExtensionKind.GreaterThanOperatorMethodType]: "__luaGreaterThanMethodBrand",
100
- [ExtensionKind.NegationOperatorType]: "__luaNegationBrand",
101
- [ExtensionKind.NegationOperatorMethodType]: "__luaNegationMethodBrand",
102
- [ExtensionKind.BitwiseNotOperatorType]: "__luaBitwiseNotBrand",
103
- [ExtensionKind.BitwiseNotOperatorMethodType]: "__luaBitwiseNotMethodBrand",
104
- [ExtensionKind.LengthOperatorType]: "__luaLengthBrand",
105
- [ExtensionKind.LengthOperatorMethodType]: "__luaLengthMethodBrand",
106
- [ExtensionKind.TableNewType]: "__luaTableNewBrand",
107
- [ExtensionKind.TableDeleteType]: "__luaTableDeleteBrand",
108
- [ExtensionKind.TableDeleteMethodType]: "__luaTableDeleteMethodBrand",
109
- [ExtensionKind.TableGetType]: "__luaTableGetBrand",
110
- [ExtensionKind.TableGetMethodType]: "__luaTableGetMethodBrand",
111
- [ExtensionKind.TableHasType]: "__luaTableHasBrand",
112
- [ExtensionKind.TableHasMethodType]: "__luaTableHasMethodBrand",
113
- [ExtensionKind.TableSetType]: "__luaTableSetBrand",
114
- [ExtensionKind.TableSetMethodType]: "__luaTableSetMethodBrand",
115
- };
116
- function isExtensionType(type, extensionKind) {
117
- const typeBrand = extensionKindToTypeBrand[extensionKind];
118
- return typeBrand !== undefined && type.getProperty(typeBrand) !== undefined;
58
+ const extensionValues = new Set(Object.values(ExtensionKind));
59
+ function getExtensionKindForType(context, type) {
60
+ const value = getPropertyValue(context, type, "__tstlExtension");
61
+ if (value && extensionValues.has(value)) {
62
+ return value;
63
+ }
119
64
  }
120
- exports.isExtensionType = isExtensionType;
121
- function getExtensionKinds(type) {
122
- return Object.keys(extensionKindToTypeBrand).filter(e => type.getProperty(extensionKindToTypeBrand[e]) !== undefined);
65
+ exports.getExtensionKindForType = getExtensionKindForType;
66
+ const excludedTypeFlags = ((1 << 18) - 1) | // All flags from Any...Never
67
+ ts.TypeFlags.Index |
68
+ ts.TypeFlags.NonPrimitive;
69
+ function getPropertyValue(context, type, propertyName) {
70
+ if (type.flags & excludedTypeFlags)
71
+ return;
72
+ const property = type.getProperty(propertyName);
73
+ if (!property)
74
+ return undefined;
75
+ const propertyType = context.checker.getTypeOfSymbolAtLocation(property, context.sourceFile);
76
+ if (propertyType.isStringLiteral())
77
+ return propertyType.value;
123
78
  }
124
- exports.getExtensionKinds = getExtensionKinds;
125
- function isExtensionValue(context, symbol, extensionKind) {
126
- var _a;
127
- return (symbol.getName() === extensionKindToValueName[extensionKind] &&
128
- ((_a = symbol.declarations) === null || _a === void 0 ? void 0 : _a.some(d => isExtensionType(context.checker.getTypeAtLocation(d), extensionKind))) === true);
79
+ function getExtensionKindForNode(context, node) {
80
+ const originalNode = ts.getOriginalNode(node);
81
+ let type = context.checker.getTypeAtLocation(originalNode);
82
+ if (ts.isOptionalChain(originalNode)) {
83
+ type = context.checker.getNonNullableType(type);
84
+ }
85
+ return getExtensionKindForType(context, type);
129
86
  }
130
- exports.isExtensionValue = isExtensionValue;
87
+ exports.getExtensionKindForNode = getExtensionKindForNode;
88
+ function getExtensionKindForSymbol(context, symbol) {
89
+ const type = context.checker.getTypeOfSymbolAtLocation(symbol, context.sourceFile);
90
+ return getExtensionKindForType(context, type);
91
+ }
92
+ exports.getExtensionKindForSymbol = getExtensionKindForSymbol;
93
+ var IterableExtensionKind;
94
+ (function (IterableExtensionKind) {
95
+ IterableExtensionKind["Iterable"] = "Iterable";
96
+ IterableExtensionKind["Pairs"] = "Pairs";
97
+ IterableExtensionKind["PairsKey"] = "PairsKey";
98
+ })(IterableExtensionKind = exports.IterableExtensionKind || (exports.IterableExtensionKind = {}));
99
+ function getIterableExtensionTypeForType(context, type) {
100
+ const value = getPropertyValue(context, type, "__tstlIterable");
101
+ if (value && value in IterableExtensionKind) {
102
+ return value;
103
+ }
104
+ }
105
+ exports.getIterableExtensionTypeForType = getIterableExtensionTypeForType;
106
+ function getIterableExtensionKindForNode(context, node) {
107
+ const type = context.checker.getTypeAtLocation(node);
108
+ return getIterableExtensionTypeForType(context, type);
109
+ }
110
+ exports.getIterableExtensionKindForNode = getIterableExtensionKindForNode;
131
111
  //# sourceMappingURL=language-extensions.js.map
@@ -3,6 +3,5 @@ import * as lua from "../../LuaAST";
3
3
  import { LuaLibFeature } from "../../LuaLib";
4
4
  import { TransformationContext } from "../context";
5
5
  export { LuaLibFeature };
6
- export declare function getUsedLuaLibFeatures(context: TransformationContext): Set<LuaLibFeature>;
7
6
  export declare function importLuaLibFeature(context: TransformationContext, feature: LuaLibFeature): void;
8
7
  export declare function transformLuaLibFunction(context: TransformationContext, feature: LuaLibFeature, tsParent?: ts.Node, ...params: lua.Expression[]): lua.CallExpression;
@@ -1,17 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.transformLuaLibFunction = exports.importLuaLibFeature = exports.getUsedLuaLibFeatures = exports.LuaLibFeature = void 0;
3
+ exports.transformLuaLibFunction = exports.importLuaLibFeature = exports.LuaLibFeature = void 0;
4
4
  const lua = require("../../LuaAST");
5
5
  const LuaLib_1 = require("../../LuaLib");
6
6
  Object.defineProperty(exports, "LuaLibFeature", { enumerable: true, get: function () { return LuaLib_1.LuaLibFeature; } });
7
- const utils_1 = require("../../utils");
8
- const luaLibFeatures = new WeakMap();
9
- function getUsedLuaLibFeatures(context) {
10
- return (0, utils_1.getOrUpdate)(luaLibFeatures, context, () => new Set());
11
- }
12
- exports.getUsedLuaLibFeatures = getUsedLuaLibFeatures;
13
7
  function importLuaLibFeature(context, feature) {
14
- getUsedLuaLibFeatures(context).add(feature);
8
+ context.usedLuaLibFeatures.add(feature);
15
9
  }
16
10
  exports.importLuaLibFeature = importLuaLibFeature;
17
11
  function transformLuaLibFunction(context, feature, tsParent, ...params) {
@@ -6,5 +6,5 @@ export declare const isValidLuaIdentifier: (name: string, options: CompilerOptio
6
6
  export declare const luaKeywords: ReadonlySet<string>;
7
7
  export declare const isUnsafeName: (name: string, options: CompilerOptions) => boolean;
8
8
  export declare function hasUnsafeSymbolName(context: TransformationContext, symbol: ts.Symbol, tsOriginal: ts.Identifier): boolean;
9
- export declare function hasUnsafeIdentifierName(context: TransformationContext, identifier: ts.Identifier, checkSymbol?: boolean): boolean;
9
+ export declare function hasUnsafeIdentifierName(context: TransformationContext, identifier: ts.Identifier, symbol: ts.Symbol | undefined): boolean;
10
10
  export declare const createSafeName: (name: string) => string;
@@ -80,12 +80,9 @@ function hasUnsafeSymbolName(context, symbol, tsOriginal) {
80
80
  return (0, exports.isUnsafeName)(symbol.name, context.options) && !isAmbient && !(0, export_1.isSymbolExported)(context, symbol);
81
81
  }
82
82
  exports.hasUnsafeSymbolName = hasUnsafeSymbolName;
83
- function hasUnsafeIdentifierName(context, identifier, checkSymbol = true) {
84
- if (checkSymbol) {
85
- const symbol = context.checker.getSymbolAtLocation(identifier);
86
- if (symbol) {
87
- return hasUnsafeSymbolName(context, symbol, identifier);
88
- }
83
+ function hasUnsafeIdentifierName(context, identifier, symbol) {
84
+ if (symbol) {
85
+ return hasUnsafeSymbolName(context, symbol, identifier);
89
86
  }
90
87
  return checkName(context, identifier.text, identifier);
91
88
  }
@@ -36,8 +36,6 @@ export declare function walkScopesUp(context: TransformationContext): IterableIt
36
36
  export declare function markSymbolAsReferencedInCurrentScopes(context: TransformationContext, symbolId: lua.SymbolId, identifier: ts.Identifier): void;
37
37
  export declare function peekScope(context: TransformationContext): Scope;
38
38
  export declare function findScope(context: TransformationContext, scopeTypes: ScopeType): Scope | undefined;
39
- export declare function pushScope(context: TransformationContext, scopeType: ScopeType): Scope;
40
- export declare function popScope(context: TransformationContext): Scope;
41
39
  export declare function addScopeVariableDeclaration(scope: Scope, declaration: lua.VariableDeclarationStatement): void;
42
40
  export declare function hasReferencedUndefinedLocalFunction(context: TransformationContext, scope: Scope): boolean;
43
41
  export declare function hasReferencedSymbol(context: TransformationContext, scope: Scope, symbol: ts.Symbol): boolean | undefined;