Skip to content

Commit 73d1fac

Browse files
committed
support named tuples where not all tuple elements are named
1 parent 83ed41b commit 73d1fac

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

javascript/extractor/src/com/semmle/js/parser/TypeScriptASTConverter.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2183,14 +2183,15 @@ private Node convertTryStatement(JsonObject node, SourceLocation loc) throws Par
21832183
}
21842184

21852185
private Node convertTupleType(JsonObject node, SourceLocation loc) throws ParseError {
2186-
List<JsonElement> elements = new ArrayList<>();
2187-
((JsonArray)node.get("elements")).iterator().forEachRemaining(elements::add);
2188-
2189-
List<Identifier> names = convertNodes(elements.stream()
2190-
.filter(n -> getKind(n).equals("NamedTupleMember"))
2191-
.map(n -> n.getAsJsonObject().get("name"))
2192-
.collect(Collectors.toList())
2193-
);
2186+
List<Identifier> names = new ArrayList<>();
2187+
2188+
for (JsonElement element : node.get("elements").getAsJsonArray()) {
2189+
Identifier id = null;
2190+
if (getKind(element).equals("NamedTupleMember")) {
2191+
id = (Identifier)convertNode(element.getAsJsonObject().get("name").getAsJsonObject());
2192+
}
2193+
names.add(id);
2194+
}
21942195

21952196
return new TupleTypeExpr(loc, convertChildrenAsTypes(node, "elements"), names);
21962197
}

javascript/ql/test/library-tests/TypeScript/TypeAnnotations/tests.expected

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ test_VariableTypes
116116
| tst.ts:184:7:184:8 | a1 | a1 | tst.ts:184:12:184:17 | number |
117117
| tst.ts:185:7:185:8 | a2 | a2 | tst.ts:185:12:185:17 | number |
118118
| tst.ts:186:7:186:8 | a3 | a3 | tst.ts:186:12:186:17 | number |
119+
| tst.ts:192:18:192:18 | x | x | tst.ts:192:21:192:43 | [first: ... number] |
119120
test_QualifiedTypeAccess
120121
| tst.ts:63:19:63:21 | N.I | tst.ts:63:19:63:19 | N | tst.ts:63:21:63:21 | I |
121122
| 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
218219
| tst.ts:176:16:176:31 | [number, number] | 1 | 2 | tst.ts:176:25:176:30 | number |
219220
| tst.ts:179:21:179:44 | [...Str ... umbers] | 0 | 2 | tst.ts:179:22:179:31 | ...Strings |
220221
| tst.ts:179:21:179:44 | [...Str ... umbers] | 1 | 2 | tst.ts:179:34:179:43 | ...Numbers |
222+
| tst.ts:192:21:192:43 | [first: ... number] | 0 | 2 | tst.ts:192:29:192:34 | number |
223+
| tst.ts:192:21:192:43 | [first: ... number] | 1 | 2 | tst.ts:192:37:192:42 | number |
224+
| tst.ts:192:47:192:70 | [number ... number] | 0 | 2 | tst.ts:192:48:192:53 | number |
225+
| tst.ts:192:47:192:70 | [number ... number] | 1 | 2 | tst.ts:192:64:192:69 | number |
221226
test_TupleTypeElementName
222227
| tst.ts:169:34:169:64 | [first: ... number] | 0 | tst.ts:169:35:169:39 | first |
223228
| tst.ts:169:34:169:64 | [first: ... number] | 1 | tst.ts:169:50:169:55 | second |
229+
| tst.ts:192:21:192:43 | [first: ... number] | 0 | tst.ts:192:22:192:26 | first |
230+
| tst.ts:192:47:192:70 | [number ... number] | 1 | tst.ts:192:56:192:61 | second |
224231
test_FieldTypes
225232
| tst.ts:15:3:15:22 | numberField: number; | tst.ts:15:16:15:21 | number |
226233
| tst.ts:16:3:16:22 | stringField: string; | tst.ts:16:16:16:21 | string |
@@ -297,6 +304,7 @@ test_ReturnTypes
297304
| tst.ts:148:1:152:1 | functio ... ;\\n }\\n} | function assertIsString | tst.ts:148:36:148:56 | asserts ... string |
298305
| tst.ts:164:1:166:1 | functio ... rr2];\\n} | function concat | tst.ts:164:66:164:77 | [...T, ...U] |
299306
| tst.ts:169:1:172:1 | functio ... + b;\\n} | function labelOnTupleElements | tst.ts:169:68:169:73 | number |
307+
| tst.ts:192:1:194:1 | functio ... rn x;\\n} | function weirdId | tst.ts:192:47:192:70 | [number ... number] |
300308
test_KeyofTypeExpr
301309
| tst.ts:49:16:49:30 | keyof Interface | tst.ts:49:22:49:30 | Interface |
302310
| tst.ts:113:26:113:35 | keyof Node | tst.ts:113:32:113:35 | Node |

javascript/ql/test/library-tests/TypeScript/TypeAnnotations/tst.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,9 @@ function shortAssignment() {
186186
let a3 : number = a1 ||= a2;
187187
let a4 = a2 &&= a3;
188188
let a5 = a3 ??= a4;
189+
}
190+
191+
// only label on some tuple elements (is a type-error)
192+
function weirdId(x: [first: number, number]): [number, second: number] {
193+
return x;
189194
}

0 commit comments

Comments
 (0)