diff --git a/javascript/extractor/src/com/semmle/js/parser/TypeScriptASTConverter.java b/javascript/extractor/src/com/semmle/js/parser/TypeScriptASTConverter.java index 8d9079a0b69..91eb2f28646 100644 --- a/javascript/extractor/src/com/semmle/js/parser/TypeScriptASTConverter.java +++ b/javascript/extractor/src/com/semmle/js/parser/TypeScriptASTConverter.java @@ -2183,14 +2183,15 @@ public class TypeScriptASTConverter { } private Node convertTupleType(JsonObject node, SourceLocation loc) throws ParseError { - List elements = new ArrayList<>(); - ((JsonArray)node.get("elements")).iterator().forEachRemaining(elements::add); + List names = new ArrayList<>(); - List names = convertNodes(elements.stream() - .filter(n -> getKind(n).equals("NamedTupleMember")) - .map(n -> n.getAsJsonObject().get("name")) - .collect(Collectors.toList()) - ); + for (JsonElement element : node.get("elements").getAsJsonArray()) { + Identifier id = null; + if (getKind(element).equals("NamedTupleMember")) { + id = (Identifier)convertNode(element.getAsJsonObject().get("name").getAsJsonObject()); + } + names.add(id); + } return new TupleTypeExpr(loc, convertChildrenAsTypes(node, "elements"), names); } diff --git a/javascript/ql/test/library-tests/TypeScript/TypeAnnotations/tests.expected b/javascript/ql/test/library-tests/TypeScript/TypeAnnotations/tests.expected index 9ae9bdfe456..5a0098961bc 100644 --- a/javascript/ql/test/library-tests/TypeScript/TypeAnnotations/tests.expected +++ b/javascript/ql/test/library-tests/TypeScript/TypeAnnotations/tests.expected @@ -116,6 +116,7 @@ test_VariableTypes | tst.ts:184:7:184:8 | a1 | a1 | tst.ts:184:12:184:17 | number | | tst.ts:185:7:185:8 | a2 | a2 | tst.ts:185:12:185:17 | number | | tst.ts:186:7:186:8 | a3 | a3 | tst.ts:186:12:186:17 | number | +| tst.ts:192:18:192:18 | x | x | tst.ts:192:21:192:43 | [first: ... number] | test_QualifiedTypeAccess | tst.ts:63:19:63:21 | N.I | tst.ts:63:19:63:19 | N | tst.ts:63:21:63:21 | I | | tst.ts:64:20:64:24 | N.M.I | tst.ts:64:20:64:22 | N.M | tst.ts:64:24:64:24 | I | @@ -218,9 +219,15 @@ test_TupleTypeExpr | tst.ts:176:16:176:31 | [number, number] | 1 | 2 | tst.ts:176:25:176:30 | number | | tst.ts:179:21:179:44 | [...Str ... umbers] | 0 | 2 | tst.ts:179:22:179:31 | ...Strings | | tst.ts:179:21:179:44 | [...Str ... umbers] | 1 | 2 | tst.ts:179:34:179:43 | ...Numbers | +| tst.ts:192:21:192:43 | [first: ... number] | 0 | 2 | tst.ts:192:29:192:34 | number | +| tst.ts:192:21:192:43 | [first: ... number] | 1 | 2 | tst.ts:192:37:192:42 | number | +| tst.ts:192:47:192:70 | [number ... number] | 0 | 2 | tst.ts:192:48:192:53 | number | +| tst.ts:192:47:192:70 | [number ... number] | 1 | 2 | tst.ts:192:64:192:69 | number | test_TupleTypeElementName | tst.ts:169:34:169:64 | [first: ... number] | 0 | tst.ts:169:35:169:39 | first | | tst.ts:169:34:169:64 | [first: ... number] | 1 | tst.ts:169:50:169:55 | second | +| tst.ts:192:21:192:43 | [first: ... number] | 0 | tst.ts:192:22:192:26 | first | +| tst.ts:192:47:192:70 | [number ... number] | 1 | tst.ts:192:56:192:61 | second | test_FieldTypes | tst.ts:15:3:15:22 | numberField: number; | tst.ts:15:16:15:21 | number | | tst.ts:16:3:16:22 | stringField: string; | tst.ts:16:16:16:21 | string | @@ -297,6 +304,7 @@ test_ReturnTypes | tst.ts:148:1:152:1 | functio ... ;\\n }\\n} | function assertIsString | tst.ts:148:36:148:56 | asserts ... string | | tst.ts:164:1:166:1 | functio ... rr2];\\n} | function concat | tst.ts:164:66:164:77 | [...T, ...U] | | tst.ts:169:1:172:1 | functio ... + b;\\n} | function labelOnTupleElements | tst.ts:169:68:169:73 | number | +| tst.ts:192:1:194:1 | functio ... rn x;\\n} | function weirdId | tst.ts:192:47:192:70 | [number ... number] | test_KeyofTypeExpr | tst.ts:49:16:49:30 | keyof Interface | tst.ts:49:22:49:30 | Interface | | tst.ts:113:26:113:35 | keyof Node | tst.ts:113:32:113:35 | Node | diff --git a/javascript/ql/test/library-tests/TypeScript/TypeAnnotations/tst.ts b/javascript/ql/test/library-tests/TypeScript/TypeAnnotations/tst.ts index 138576c4b4f..24a216bb443 100644 --- a/javascript/ql/test/library-tests/TypeScript/TypeAnnotations/tst.ts +++ b/javascript/ql/test/library-tests/TypeScript/TypeAnnotations/tst.ts @@ -186,4 +186,9 @@ function shortAssignment() { let a3 : number = a1 ||= a2; let a4 = a2 &&= a3; let a5 = a3 ??= a4; +} + +// only label on some tuple elements (is a type-error) +function weirdId(x: [first: number, number]): [number, second: number] { + return x; } \ No newline at end of file