typescript-to-lua 1.6.1 → 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 (103) hide show
  1. package/dist/CompilerOptions.d.ts +1 -0
  2. package/dist/LuaPrinter.js +1 -0
  3. package/dist/cli/diagnostics.d.ts +3 -0
  4. package/dist/cli/diagnostics.js +2 -1
  5. package/dist/cli/parse.d.ts +1 -1
  6. package/dist/cli/parse.js +47 -11
  7. package/dist/lualib/ArraySlice.lua +10 -2
  8. package/dist/lualib/ArraySplice.lua +5 -1
  9. package/dist/lualib/Generator.lua +8 -1
  10. package/dist/lualib/ParseFloat.lua +5 -1
  11. package/dist/lualib/Promise.lua +6 -3
  12. package/dist/lualib/SparseArraySpread.lua +5 -1
  13. package/dist/lualib/StringCharCodeAt.lua +5 -1
  14. package/dist/lualib/lualib_bundle.lua +44 -10
  15. package/dist/lualib-build/plugin.js +1 -2
  16. package/dist/measure-performance.d.ts +15 -0
  17. package/dist/measure-performance.js +83 -0
  18. package/dist/transformation/builtins/array.js +2 -1
  19. package/dist/transformation/builtins/function.js +1 -1
  20. package/dist/transformation/builtins/global.d.ts +1 -1
  21. package/dist/transformation/builtins/global.js +13 -12
  22. package/dist/transformation/builtins/index.d.ts +2 -2
  23. package/dist/transformation/builtins/index.js +87 -89
  24. package/dist/transformation/builtins/object.d.ts +1 -1
  25. package/dist/transformation/builtins/object.js +4 -4
  26. package/dist/transformation/builtins/promise.js +4 -2
  27. package/dist/transformation/builtins/string.js +2 -0
  28. package/dist/transformation/context/context.d.ts +15 -0
  29. package/dist/transformation/context/context.js +66 -24
  30. package/dist/transformation/context/visitors.d.ts +1 -1
  31. package/dist/transformation/index.js +6 -5
  32. package/dist/transformation/utils/annotations.d.ts +1 -17
  33. package/dist/transformation/utils/annotations.js +48 -88
  34. package/dist/transformation/utils/assignment-validation.js +27 -23
  35. package/dist/transformation/utils/diagnostics.d.ts +1 -4
  36. package/dist/transformation/utils/diagnostics.js +2 -3
  37. package/dist/transformation/utils/function-context.d.ts +1 -1
  38. package/dist/transformation/utils/function-context.js +57 -51
  39. package/dist/transformation/utils/language-extensions.d.ts +57 -51
  40. package/dist/transformation/utils/language-extensions.js +99 -119
  41. package/dist/transformation/utils/lualib.d.ts +0 -1
  42. package/dist/transformation/utils/lualib.js +2 -8
  43. package/dist/transformation/utils/safe-names.d.ts +1 -1
  44. package/dist/transformation/utils/safe-names.js +3 -6
  45. package/dist/transformation/utils/scope.d.ts +0 -2
  46. package/dist/transformation/utils/scope.js +10 -27
  47. package/dist/transformation/utils/symbols.d.ts +1 -1
  48. package/dist/transformation/utils/symbols.js +7 -20
  49. package/dist/transformation/utils/typescript/index.d.ts +1 -0
  50. package/dist/transformation/utils/typescript/index.js +2 -1
  51. package/dist/transformation/utils/typescript/types.d.ts +2 -5
  52. package/dist/transformation/utils/typescript/types.js +27 -37
  53. package/dist/transformation/visitors/access.js +1 -4
  54. package/dist/transformation/visitors/block.js +4 -4
  55. package/dist/transformation/visitors/call.js +7 -18
  56. package/dist/transformation/visitors/class/index.js +8 -20
  57. package/dist/transformation/visitors/class/members/constructor.js +2 -2
  58. package/dist/transformation/visitors/class/new.js +2 -5
  59. package/dist/transformation/visitors/class/setup.d.ts +1 -1
  60. package/dist/transformation/visitors/class/setup.js +4 -4
  61. package/dist/transformation/visitors/class/utils.d.ts +1 -1
  62. package/dist/transformation/visitors/class/utils.js +2 -9
  63. package/dist/transformation/visitors/conditional.js +4 -4
  64. package/dist/transformation/visitors/function.d.ts +1 -1
  65. package/dist/transformation/visitors/function.js +8 -16
  66. package/dist/transformation/visitors/identifier.d.ts +1 -0
  67. package/dist/transformation/visitors/identifier.js +40 -44
  68. package/dist/transformation/visitors/language-extensions/call-extension.d.ts +10 -0
  69. package/dist/transformation/visitors/language-extensions/call-extension.js +23 -0
  70. package/dist/transformation/visitors/language-extensions/identifier.d.ts +5 -0
  71. package/dist/transformation/visitors/language-extensions/identifier.js +27 -0
  72. package/dist/transformation/visitors/language-extensions/iterable.d.ts +2 -3
  73. package/dist/transformation/visitors/language-extensions/iterable.js +21 -22
  74. package/dist/transformation/visitors/language-extensions/multi.d.ts +0 -1
  75. package/dist/transformation/visitors/language-extensions/multi.js +10 -24
  76. package/dist/transformation/visitors/language-extensions/operators.d.ts +2 -5
  77. package/dist/transformation/visitors/language-extensions/operators.js +89 -96
  78. package/dist/transformation/visitors/language-extensions/range.js +4 -2
  79. package/dist/transformation/visitors/language-extensions/table.d.ts +6 -3
  80. package/dist/transformation/visitors/language-extensions/table.js +46 -75
  81. package/dist/transformation/visitors/language-extensions/vararg.js +7 -3
  82. package/dist/transformation/visitors/literal.js +2 -23
  83. package/dist/transformation/visitors/loops/for-of.js +18 -18
  84. package/dist/transformation/visitors/loops/utils.js +5 -5
  85. package/dist/transformation/visitors/namespace.js +5 -14
  86. package/dist/transformation/visitors/return.js +2 -3
  87. package/dist/transformation/visitors/sourceFile.js +3 -4
  88. package/dist/transformation/visitors/spread.js +0 -5
  89. package/dist/transformation/visitors/switch.js +2 -2
  90. package/dist/transformation/visitors/variable-declaration.js +1 -1
  91. package/dist/transpilation/index.js +4 -8
  92. package/dist/transpilation/plugins.js +3 -0
  93. package/dist/transpilation/resolve.js +144 -161
  94. package/dist/transpilation/transpile.js +10 -0
  95. package/dist/transpilation/transpiler.d.ts +1 -0
  96. package/dist/transpilation/transpiler.js +18 -5
  97. package/dist/tstl.js +23 -4
  98. package/language-extensions/index.d.ts +156 -67
  99. package/package.json +2 -2
  100. package/dist/transformation/visitors/language-extensions/index.d.ts +0 -4
  101. package/dist/transformation/visitors/language-extensions/index.js +0 -17
  102. package/dist/transformation/visitors/language-extensions/pairsIterable.d.ts +0 -5
  103. package/dist/transformation/visitors/language-extensions/pairsIterable.js +0 -53
@@ -1,135 +1,96 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getTagArgsFromComment = exports.isForRangeType = exports.isVarargType = exports.isLuaIteratorType = exports.isTupleReturnCall = exports.getSignatureAnnotations = exports.getFileAnnotations = exports.getNodeAnnotations = exports.getTypeAnnotations = exports.getSymbolAnnotations = exports.AnnotationKind = void 0;
3
+ exports.getFileAnnotations = exports.getNodeAnnotations = exports.getTypeAnnotations = exports.getSymbolAnnotations = exports.AnnotationKind = void 0;
4
4
  const ts = require("typescript");
5
5
  var AnnotationKind;
6
6
  (function (AnnotationKind) {
7
- AnnotationKind["Extension"] = "extension";
8
- AnnotationKind["MetaExtension"] = "metaExtension";
9
7
  AnnotationKind["CustomConstructor"] = "customConstructor";
10
8
  AnnotationKind["CompileMembersOnly"] = "compileMembersOnly";
11
9
  AnnotationKind["NoResolution"] = "noResolution";
12
- AnnotationKind["PureAbstract"] = "pureAbstract";
13
- AnnotationKind["Phantom"] = "phantom";
14
- AnnotationKind["TupleReturn"] = "tupleReturn";
15
- AnnotationKind["LuaIterator"] = "luaIterator";
16
- AnnotationKind["LuaTable"] = "luaTable";
17
10
  AnnotationKind["NoSelf"] = "noSelf";
18
11
  AnnotationKind["NoSelfInFile"] = "noSelfInFile";
19
- AnnotationKind["Vararg"] = "vararg";
20
- AnnotationKind["ForRange"] = "forRange";
21
12
  })(AnnotationKind = exports.AnnotationKind || (exports.AnnotationKind = {}));
22
- function createAnnotation(name, args) {
23
- const kind = Object.values(AnnotationKind).find(k => k.toLowerCase() === name.toLowerCase());
24
- if (kind !== undefined) {
25
- return { kind, args };
26
- }
27
- }
13
+ const annotationValues = new Map(Object.values(AnnotationKind).map(k => [k.toLowerCase(), k]));
28
14
  function collectAnnotations(source, annotationsMap) {
29
15
  var _a, _b;
30
16
  for (const tag of source.getJsDocTags()) {
31
- const annotation = createAnnotation(tag.name, (_b = (_a = tag.text) === null || _a === void 0 ? void 0 : _a.map(p => p.text)) !== null && _b !== void 0 ? _b : []);
32
- if (annotation) {
33
- annotationsMap.set(annotation.kind, annotation);
34
- }
17
+ const tagName = annotationValues.get(tag.name.toLowerCase());
18
+ if (!tagName)
19
+ continue;
20
+ const annotation = {
21
+ kind: tag.name,
22
+ args: (_b = (_a = tag.text) === null || _a === void 0 ? void 0 : _a.map(p => p.text)) !== null && _b !== void 0 ? _b : [],
23
+ };
24
+ annotationsMap.set(tagName, annotation);
35
25
  }
36
26
  }
27
+ const symbolAnnotations = new WeakMap();
37
28
  function getSymbolAnnotations(symbol) {
29
+ const known = symbolAnnotations.get(symbol);
30
+ if (known)
31
+ return known;
38
32
  const annotationsMap = new Map();
39
33
  collectAnnotations(symbol, annotationsMap);
34
+ symbolAnnotations.set(symbol, annotationsMap);
40
35
  return annotationsMap;
41
36
  }
42
37
  exports.getSymbolAnnotations = getSymbolAnnotations;
43
38
  function getTypeAnnotations(type) {
39
+ // types are not frequently repeatedly polled for annotations, so it's not worth caching them
44
40
  const annotationsMap = new Map();
45
- if (type.symbol)
46
- collectAnnotations(type.symbol, annotationsMap);
47
- if (type.aliasSymbol)
48
- collectAnnotations(type.aliasSymbol, annotationsMap);
41
+ if (type.symbol) {
42
+ getSymbolAnnotations(type.symbol).forEach((value, key) => {
43
+ annotationsMap.set(key, value);
44
+ });
45
+ }
46
+ if (type.aliasSymbol) {
47
+ getSymbolAnnotations(type.aliasSymbol).forEach((value, key) => {
48
+ annotationsMap.set(key, value);
49
+ });
50
+ }
49
51
  return annotationsMap;
50
52
  }
51
53
  exports.getTypeAnnotations = getTypeAnnotations;
54
+ const nodeAnnotations = new WeakMap();
52
55
  function getNodeAnnotations(node) {
56
+ const known = nodeAnnotations.get(node);
57
+ if (known)
58
+ return known;
53
59
  const annotationsMap = new Map();
54
- for (const tag of ts.getJSDocTags(node)) {
55
- const tagName = tag.tagName.text;
56
- const annotation = createAnnotation(tagName, getTagArgsFromComment(tag));
57
- if (annotation) {
58
- annotationsMap.set(annotation.kind, annotation);
59
- }
60
- }
60
+ collectAnnotationsFromTags(annotationsMap, ts.getAllJSDocTags(node, ts.isJSDocUnknownTag));
61
+ nodeAnnotations.set(node, annotationsMap);
61
62
  return annotationsMap;
62
63
  }
63
64
  exports.getNodeAnnotations = getNodeAnnotations;
65
+ function collectAnnotationsFromTags(annotationsMap, tags) {
66
+ for (const tag of tags) {
67
+ const tagName = annotationValues.get(tag.tagName.text.toLowerCase());
68
+ if (!tagName)
69
+ continue;
70
+ annotationsMap.set(tagName, { kind: tagName, args: getTagArgsFromComment(tag) });
71
+ }
72
+ }
73
+ const fileAnnotations = new WeakMap();
64
74
  function getFileAnnotations(sourceFile) {
75
+ const known = fileAnnotations.get(sourceFile);
76
+ if (known)
77
+ return known;
65
78
  const annotationsMap = new Map();
66
79
  if (sourceFile.statements.length > 0) {
67
80
  // Manually collect jsDoc because `getJSDocTags` includes tags only from closest comment
68
81
  const jsDoc = sourceFile.statements[0].jsDoc;
69
82
  if (jsDoc) {
70
- for (const tag of jsDoc.flatMap(x => { var _a; return (_a = x.tags) !== null && _a !== void 0 ? _a : []; })) {
71
- const tagName = tag.tagName.text;
72
- const annotation = createAnnotation(tagName, getTagArgsFromComment(tag));
73
- if (annotation) {
74
- annotationsMap.set(annotation.kind, annotation);
83
+ for (const jsDocElement of jsDoc) {
84
+ if (jsDocElement.tags) {
85
+ collectAnnotationsFromTags(annotationsMap, jsDocElement.tags);
75
86
  }
76
87
  }
77
88
  }
78
89
  }
90
+ fileAnnotations.set(sourceFile, annotationsMap);
79
91
  return annotationsMap;
80
92
  }
81
93
  exports.getFileAnnotations = getFileAnnotations;
82
- function getSignatureAnnotations(context, signature) {
83
- const annotationsMap = new Map();
84
- collectAnnotations(signature, annotationsMap);
85
- // Function properties on interfaces have the JSDoc tags on the parent PropertySignature
86
- const declaration = signature.getDeclaration();
87
- if ((declaration === null || declaration === void 0 ? void 0 : declaration.parent) && ts.isPropertySignature(declaration.parent)) {
88
- const symbol = context.checker.getSymbolAtLocation(declaration.parent.name);
89
- if (symbol) {
90
- collectAnnotations(symbol, annotationsMap);
91
- }
92
- }
93
- return annotationsMap;
94
- }
95
- exports.getSignatureAnnotations = getSignatureAnnotations;
96
- function isTupleReturnCall(context, node) {
97
- if (!ts.isCallExpression(node)) {
98
- return false;
99
- }
100
- const signature = context.checker.getResolvedSignature(node);
101
- if (signature) {
102
- if (getSignatureAnnotations(context, signature).has(AnnotationKind.TupleReturn)) {
103
- return true;
104
- }
105
- // Only check function type for directive if it is declared as an interface or type alias
106
- const declaration = signature.getDeclaration();
107
- const isInterfaceOrAlias = (declaration === null || declaration === void 0 ? void 0 : declaration.parent) &&
108
- ((ts.isInterfaceDeclaration(declaration.parent) && ts.isCallSignatureDeclaration(declaration)) ||
109
- ts.isTypeAliasDeclaration(declaration.parent));
110
- if (!isInterfaceOrAlias) {
111
- return false;
112
- }
113
- }
114
- const type = context.checker.getTypeAtLocation(node.expression);
115
- return getTypeAnnotations(type).has(AnnotationKind.TupleReturn);
116
- }
117
- exports.isTupleReturnCall = isTupleReturnCall;
118
- function isLuaIteratorType(context, node) {
119
- const type = context.checker.getTypeAtLocation(node);
120
- return getTypeAnnotations(type).has(AnnotationKind.LuaIterator);
121
- }
122
- exports.isLuaIteratorType = isLuaIteratorType;
123
- function isVarargType(context, node) {
124
- const type = context.checker.getTypeAtLocation(node);
125
- return getTypeAnnotations(type).has(AnnotationKind.Vararg);
126
- }
127
- exports.isVarargType = isVarargType;
128
- function isForRangeType(context, node) {
129
- const type = context.checker.getTypeAtLocation(node);
130
- return getTypeAnnotations(type).has(AnnotationKind.ForRange);
131
- }
132
- exports.isForRangeType = isForRangeType;
133
94
  function getTagArgsFromComment(tag) {
134
95
  if (tag.comment) {
135
96
  if (typeof tag.comment === "string") {
@@ -141,5 +102,4 @@ function getTagArgsFromComment(tag) {
141
102
  }
142
103
  return [];
143
104
  }
144
- exports.getTagArgsFromComment = getTagArgsFromComment;
145
105
  //# sourceMappingURL=annotations.js.map
@@ -8,6 +8,7 @@ const function_context_1 = require("./function-context");
8
8
  // TODO: Clear if types are reused between compilations
9
9
  const typeValidationCache = new WeakMap();
10
10
  function validateAssignment(context, node, fromType, toType, toName) {
11
+ var _a, _b;
11
12
  if (toType === fromType) {
12
13
  return;
13
14
  }
@@ -21,13 +22,9 @@ function validateAssignment(context, node, fromType, toType, toName) {
21
22
  return;
22
23
  fromTypeCache.add(toType);
23
24
  validateFunctionAssignment(context, node, fromType, toType, toName);
24
- const fromTypeNode = context.checker.typeToTypeNode(fromType, undefined, undefined);
25
- const toTypeNode = context.checker.typeToTypeNode(toType, undefined, undefined);
26
- if (!fromTypeNode || !toTypeNode) {
27
- return;
28
- }
29
- if ((ts.isArrayTypeNode(toTypeNode) || ts.isTupleTypeNode(toTypeNode)) &&
30
- (ts.isArrayTypeNode(fromTypeNode) || ts.isTupleTypeNode(fromTypeNode))) {
25
+ const checker = context.checker;
26
+ if ((checker.isTupleType(toType) || checker.isArrayType(toType)) &&
27
+ (checker.isTupleType(fromType) || checker.isArrayType(fromType))) {
31
28
  // Recurse into arrays/tuples
32
29
  const fromTypeArguments = fromType.typeArguments;
33
30
  const toTypeArguments = toType.typeArguments;
@@ -39,25 +36,32 @@ function validateAssignment(context, node, fromType, toType, toName) {
39
36
  validateAssignment(context, node, fromTypeArguments[i], toTypeArguments[i], toName);
40
37
  }
41
38
  }
42
- if ((toType.flags & ts.TypeFlags.Object) !== 0 &&
43
- (ts.isTypeLiteralNode(toTypeNode) ||
44
- (toType.objectFlags & ts.ObjectFlags.ClassOrInterface) !== 0) &&
45
- toType.symbol &&
46
- toType.symbol.members &&
47
- fromType.symbol &&
48
- fromType.symbol.members) {
39
+ const fromMembers = (_a = fromType.symbol) === null || _a === void 0 ? void 0 : _a.members;
40
+ const toMembers = (_b = toType.symbol) === null || _b === void 0 ? void 0 : _b.members;
41
+ if (fromMembers && toMembers) {
49
42
  // Recurse into interfaces
50
- toType.symbol.members.forEach((toMember, escapedMemberName) => {
51
- if (fromType.symbol.members) {
52
- const fromMember = fromType.symbol.members.get(escapedMemberName);
43
+ if (toMembers.size < fromMembers.size) {
44
+ toMembers.forEach((toMember, escapedMemberName) => {
45
+ const fromMember = fromMembers.get(escapedMemberName);
53
46
  if (fromMember) {
54
- const toMemberType = context.checker.getTypeOfSymbolAtLocation(toMember, node);
55
- const fromMemberType = context.checker.getTypeOfSymbolAtLocation(fromMember, node);
56
- const memberName = ts.unescapeLeadingUnderscores(escapedMemberName);
57
- validateAssignment(context, node, fromMemberType, toMemberType, toName ? `${toName}.${memberName}` : memberName);
47
+ validateMember(toMember, fromMember, escapedMemberName);
48
+ }
49
+ });
50
+ }
51
+ else {
52
+ fromMembers.forEach((fromMember, escapedMemberName) => {
53
+ const toMember = toMembers.get(escapedMemberName);
54
+ if (toMember) {
55
+ validateMember(toMember, fromMember, escapedMemberName);
58
56
  }
59
- }
60
- });
57
+ });
58
+ }
59
+ }
60
+ function validateMember(toMember, fromMember, escapedMemberName) {
61
+ const toMemberType = context.checker.getTypeOfSymbolAtLocation(toMember, node);
62
+ const fromMemberType = context.checker.getTypeOfSymbolAtLocation(fromMember, node);
63
+ const memberName = ts.unescapeLeadingUnderscores(escapedMemberName);
64
+ validateAssignment(context, node, fromMemberType, toMemberType, toName ? `${toName}.${memberName}` : memberName);
61
65
  }
62
66
  }
63
67
  exports.validateAssignment = validateAssignment;
@@ -74,10 +74,7 @@ export declare const invalidMultiTypeToEmptyPatternOrArrayLiteral: ((node: ts.No
74
74
  export declare const invalidMultiReturnAccess: ((node: ts.Node, ...args: any[]) => ts.Diagnostic) & {
75
75
  code: number;
76
76
  };
77
- export declare const invalidOperatorMappingUse: ((node: ts.Node, ...args: any[]) => ts.Diagnostic) & {
78
- code: number;
79
- };
80
- export declare const invalidTableExtensionUse: ((node: ts.Node, ...args: any[]) => ts.Diagnostic) & {
77
+ export declare const invalidCallExtensionUse: ((node: ts.Node, ...args: any[]) => ts.Diagnostic) & {
81
78
  code: number;
82
79
  };
83
80
  export declare const annotationRemoved: ((node: ts.Node, kind: AnnotationKind) => ts.Diagnostic) & {
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.undefinedInArrayLiteral = exports.unsupportedOptionalCompileMembersOnly = exports.unsupportedBuiltinOptionalCall = exports.awaitMustBeInAsyncFunction = exports.notAllowedOptionalAssignment = exports.annotationDeprecated = exports.annotationRemoved = exports.invalidTableExtensionUse = exports.invalidOperatorMappingUse = exports.invalidMultiReturnAccess = exports.invalidMultiTypeToEmptyPatternOrArrayLiteral = exports.invalidMultiTypeToNonArrayLiteral = exports.invalidMultiFunctionReturnType = exports.invalidMultiFunctionUse = exports.unsupportedVarDeclaration = exports.invalidAmbientIdentifierName = exports.unsupportedProperty = exports.unsupportedForTargetButOverrideAvailable = exports.unsupportedForTarget = exports.unsupportedRightShiftOperator = exports.unsupportedAccessorInObjectLiteral = exports.invalidPairsIterableWithoutDestructuring = exports.invalidMultiIterableWithoutDestructuring = exports.invalidRangeControlVariable = exports.invalidVarargUse = exports.invalidRangeUse = exports.annotationInvalidArgumentCount = exports.decoratorInvalidContext = exports.unsupportedOverloadAssignment = exports.unsupportedSelfFunctionConversion = exports.unsupportedNoSelfFunctionConversion = exports.forbiddenForIn = exports.unsupportedNodeKind = void 0;
3
+ exports.undefinedInArrayLiteral = exports.unsupportedOptionalCompileMembersOnly = exports.unsupportedBuiltinOptionalCall = exports.awaitMustBeInAsyncFunction = exports.notAllowedOptionalAssignment = exports.annotationDeprecated = exports.annotationRemoved = exports.invalidCallExtensionUse = exports.invalidMultiReturnAccess = exports.invalidMultiTypeToEmptyPatternOrArrayLiteral = exports.invalidMultiTypeToNonArrayLiteral = exports.invalidMultiFunctionReturnType = exports.invalidMultiFunctionUse = exports.unsupportedVarDeclaration = exports.invalidAmbientIdentifierName = exports.unsupportedProperty = exports.unsupportedForTargetButOverrideAvailable = exports.unsupportedForTarget = exports.unsupportedRightShiftOperator = exports.unsupportedAccessorInObjectLiteral = exports.invalidPairsIterableWithoutDestructuring = exports.invalidMultiIterableWithoutDestructuring = exports.invalidRangeControlVariable = exports.invalidVarargUse = exports.invalidRangeUse = exports.annotationInvalidArgumentCount = exports.decoratorInvalidContext = exports.unsupportedOverloadAssignment = exports.unsupportedSelfFunctionConversion = exports.unsupportedNoSelfFunctionConversion = exports.forbiddenForIn = exports.unsupportedNodeKind = void 0;
4
4
  const ts = require("typescript");
5
5
  const CompilerOptions_1 = require("../../CompilerOptions");
6
6
  const utils_1 = require("../../utils");
@@ -52,8 +52,7 @@ exports.invalidMultiFunctionReturnType = createErrorDiagnosticFactory("The $mult
52
52
  exports.invalidMultiTypeToNonArrayLiteral = createErrorDiagnosticFactory("Expected an array literal.");
53
53
  exports.invalidMultiTypeToEmptyPatternOrArrayLiteral = createErrorDiagnosticFactory("There must be one or more elements specified here.");
54
54
  exports.invalidMultiReturnAccess = createErrorDiagnosticFactory("The LuaMultiReturn type can only be accessed via an element access expression of a numeric type.");
55
- exports.invalidOperatorMappingUse = createErrorDiagnosticFactory("This function must always be directly called and cannot be referred to.");
56
- exports.invalidTableExtensionUse = createErrorDiagnosticFactory("This function must be called directly and cannot be referred to.");
55
+ exports.invalidCallExtensionUse = createErrorDiagnosticFactory("This function must be called directly and cannot be referred to.");
57
56
  exports.annotationRemoved = createErrorDiagnosticFactory((kind) => `'@${kind}' has been removed and will no longer have any effect.` +
58
57
  `See https://typescripttolua.github.io/docs/advanced/compiler-annotations#${kind.toLowerCase()} for more information.`);
59
58
  exports.annotationDeprecated = createWarningDiagnosticFactory((kind) => `'@${kind}' is deprecated and will be removed in a future update. Please update your code before upgrading to the next release, otherwise your project will no longer compile. ` +
@@ -6,5 +6,5 @@ export declare enum ContextType {
6
6
  NonVoid = 2,
7
7
  Mixed = 3
8
8
  }
9
- export declare function getDeclarationContextType({ program }: TransformationContext, signatureDeclaration: ts.SignatureDeclaration): ContextType;
9
+ export declare function getDeclarationContextType(context: TransformationContext, signatureDeclaration: ts.SignatureDeclaration): ContextType;
10
10
  export declare function getFunctionContextType(context: TransformationContext, type: ts.Type): ContextType;
@@ -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;