Skip to content

Commit 7a55b00

Browse files
committed
JS: Fix location of assert clause
1 parent 38194c6 commit 7a55b00

File tree

3 files changed

+34
-28
lines changed

3 files changed

+34
-28
lines changed

javascript/extractor/src/com/semmle/ts/extractor/TypeScriptASTConverter.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ public class TypeScriptASTConverter {
177177
private static final Pattern EXPORT_DECL_START =
178178
Pattern.compile("^export" + "(" + WHITESPACE_CHAR + "+default)?" + WHITESPACE_CHAR + "+");
179179
private static final Pattern TYPEOF_START = Pattern.compile("^typeof" + WHITESPACE_CHAR + "+");
180+
private static final Pattern ASSERT_START = Pattern.compile("^assert" + WHITESPACE_CHAR + "+");
180181
private static final Pattern WHITESPACE_END_PAREN =
181182
Pattern.compile("^" + WHITESPACE_CHAR + "*\\)");
182183

@@ -2287,6 +2288,11 @@ private Node convertAssertClause(JsonObject node, SourceLocation loc) throws Par
22872288
for (INode child : convertChildren(node, "elements")) {
22882289
properties.add((Property)child);
22892290
}
2291+
// Adjust location to skip over the `assert` keyword.
2292+
Matcher m = ASSERT_START.matcher(loc.getSource());
2293+
if (m.find()) {
2294+
advance(loc, m.group(0));
2295+
}
22902296
return new ObjectExpression(loc, properties);
22912297
}
22922298

javascript/extractor/tests/ts/output/trap/import-assertion.ts.trap

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -726,9 +726,9 @@ locations_default(#20275,#10000,1,9,1,14)
726726
hasLocation(#20274,#20275)
727727
regexp_const_value(#20274,"module")
728728
#20276=*
729-
exprs(#20276,8,#20272,-10,"assert ... json"" }")
730-
#20277=@"loc,{#10000},1,17,1,39"
731-
locations_default(#20277,#10000,1,17,1,39)
729+
exprs(#20276,8,#20272,-10,"{ type: ""json"" }")
730+
#20277=@"loc,{#10000},1,24,1,39"
731+
locations_default(#20277,#10000,1,24,1,39)
732732
hasLocation(#20276,#20277)
733733
enclosing_stmt(#20276,#20272)
734734
expr_containers(#20276,#20001)
@@ -754,9 +754,9 @@ locations_default(#20283,#10000,2,22,2,27)
754754
hasLocation(#20282,#20283)
755755
regexp_const_value(#20282,"module")
756756
#20284=*
757-
exprs(#20284,8,#20280,-10,"assert ... json"" }")
758-
#20285=@"loc,{#10000},2,30,2,52"
759-
locations_default(#20285,#10000,2,30,2,52)
757+
exprs(#20284,8,#20280,-10,"{ type: ""json"" }")
758+
#20285=@"loc,{#10000},2,37,2,52"
759+
locations_default(#20285,#10000,2,37,2,52)
760760
hasLocation(#20284,#20285)
761761
enclosing_stmt(#20284,#20280)
762762
expr_containers(#20284,#20001)
@@ -798,9 +798,9 @@ locations_default(#20294,#10000,3,21,3,26)
798798
hasLocation(#20293,#20294)
799799
regexp_const_value(#20293,"module")
800800
#20295=*
801-
exprs(#20295,8,#20291,-10,"assert ... json"" }")
802-
#20296=@"loc,{#10000},3,29,3,51"
803-
locations_default(#20296,#10000,3,29,3,51)
801+
exprs(#20295,8,#20291,-10,"{ type: ""json"" }")
802+
#20296=@"loc,{#10000},3,36,3,51"
803+
locations_default(#20296,#10000,3,36,3,51)
804804
hasLocation(#20295,#20296)
805805
enclosing_stmt(#20295,#20291)
806806
expr_containers(#20295,#20001)
@@ -846,9 +846,9 @@ locations_default(#20305,#10000,4,17,4,22)
846846
hasLocation(#20304,#20305)
847847
regexp_const_value(#20304,"module")
848848
#20306=*
849-
exprs(#20306,8,#20302,-10,"assert ... json"" }")
850-
#20307=@"loc,{#10000},4,25,4,47"
851-
locations_default(#20307,#10000,4,25,4,47)
849+
exprs(#20306,8,#20302,-10,"{ type: ""json"" }")
850+
#20307=@"loc,{#10000},4,32,4,47"
851+
locations_default(#20307,#10000,4,32,4,47)
852852
hasLocation(#20306,#20307)
853853
enclosing_stmt(#20306,#20302)
854854
expr_containers(#20306,#20001)
@@ -888,9 +888,9 @@ locations_default(#20315,#10000,6,21,6,26)
888888
hasLocation(#20314,#20315)
889889
regexp_const_value(#20314,"module")
890890
#20316=*
891-
exprs(#20316,8,#20312,-10,"assert ... json"" }")
892-
#20317=@"loc,{#10000},6,29,6,51"
893-
locations_default(#20317,#10000,6,29,6,51)
891+
exprs(#20316,8,#20312,-10,"{ type: ""json"" }")
892+
#20317=@"loc,{#10000},6,36,6,51"
893+
locations_default(#20317,#10000,6,36,6,51)
894894
hasLocation(#20316,#20317)
895895
enclosing_stmt(#20316,#20312)
896896
expr_containers(#20316,#20001)
@@ -933,9 +933,9 @@ locations_default(#20326,#10000,7,16,7,21)
933933
hasLocation(#20325,#20326)
934934
regexp_const_value(#20325,"module")
935935
#20327=*
936-
exprs(#20327,8,#20323,-10,"assert ... json"" }")
937-
#20328=@"loc,{#10000},7,24,7,46"
938-
locations_default(#20328,#10000,7,24,7,46)
936+
exprs(#20327,8,#20323,-10,"{ type: ""json"" }")
937+
#20328=@"loc,{#10000},7,31,7,46"
938+
locations_default(#20328,#10000,7,31,7,46)
939939
hasLocation(#20327,#20328)
940940
enclosing_stmt(#20327,#20323)
941941
expr_containers(#20327,#20001)
@@ -961,9 +961,9 @@ locations_default(#20334,#10000,8,22,8,27)
961961
hasLocation(#20333,#20334)
962962
regexp_const_value(#20333,"module")
963963
#20335=*
964-
exprs(#20335,8,#20331,-10,"assert ... json"" }")
965-
#20336=@"loc,{#10000},8,30,8,52"
966-
locations_default(#20336,#10000,8,30,8,52)
964+
exprs(#20335,8,#20331,-10,"{ type: ""json"" }")
965+
#20336=@"loc,{#10000},8,37,8,52"
966+
locations_default(#20336,#10000,8,37,8,52)
967967
hasLocation(#20335,#20336)
968968
enclosing_stmt(#20335,#20331)
969969
expr_containers(#20335,#20001)

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ getImportAssertionFromImport
33
| js-import-assertions.js:2:1:2:53 | import ... son" }; | js-import-assertions.js:2:37:2:52 | { type: "json" } |
44
| js-import-assertions.js:3:1:3:52 | import ... son" }; | js-import-assertions.js:3:36:3:51 | { type: "json" } |
55
| js-import-assertions.js:4:1:4:48 | import ... son" }; | js-import-assertions.js:4:32:4:47 | { type: "json" } |
6-
| ts-import-assertions.ts:3:1:3:40 | import ... son" }; | ts-import-assertions.ts:3:17:3:39 | assert ... json" } |
7-
| ts-import-assertions.ts:4:1:4:53 | import ... son" }; | ts-import-assertions.ts:4:30:4:52 | assert ... json" } |
8-
| ts-import-assertions.ts:5:1:5:52 | import ... son" }; | ts-import-assertions.ts:5:29:5:51 | assert ... json" } |
9-
| ts-import-assertions.ts:6:1:6:48 | import ... son" }; | ts-import-assertions.ts:6:25:6:47 | assert ... json" } |
6+
| ts-import-assertions.ts:3:1:3:40 | import ... son" }; | ts-import-assertions.ts:3:24:3:39 | { type: "json" } |
7+
| ts-import-assertions.ts:4:1:4:53 | import ... son" }; | ts-import-assertions.ts:4:37:4:52 | { type: "json" } |
8+
| ts-import-assertions.ts:5:1:5:52 | import ... son" }; | ts-import-assertions.ts:5:36:5:51 | { type: "json" } |
9+
| ts-import-assertions.ts:6:1:6:48 | import ... son" }; | ts-import-assertions.ts:6:32:6:47 | { type: "json" } |
1010
getImportAssertionFromExport
1111
| js-import-assertions.js:6:1:6:52 | export ... son" }; | js-import-assertions.js:6:36:6:51 | { type: "json" } |
1212
| js-import-assertions.js:7:1:7:47 | export ... son" }; | js-import-assertions.js:7:31:7:46 | { type: "json" } |
1313
| js-import-assertions.js:8:1:8:53 | export ... son" }; | js-import-assertions.js:8:37:8:52 | { type: "json" } |
14-
| ts-import-assertions.ts:8:1:8:52 | export ... son" }; | ts-import-assertions.ts:8:29:8:51 | assert ... json" } |
15-
| ts-import-assertions.ts:9:1:9:47 | export ... son" }; | ts-import-assertions.ts:9:24:9:46 | assert ... json" } |
16-
| ts-import-assertions.ts:10:1:10:53 | export ... son" }; | ts-import-assertions.ts:10:30:10:52 | assert ... json" } |
14+
| ts-import-assertions.ts:8:1:8:52 | export ... son" }; | ts-import-assertions.ts:8:36:8:51 | { type: "json" } |
15+
| ts-import-assertions.ts:9:1:9:47 | export ... son" }; | ts-import-assertions.ts:9:31:9:46 | { type: "json" } |
16+
| ts-import-assertions.ts:10:1:10:53 | export ... son" }; | ts-import-assertions.ts:10:37:10:52 | { type: "json" } |
1717
getImportAttributes
1818
| js-import-assertions.js:10:12:10:57 | import( ... n" } }) | js-import-assertions.js:10:29:10:56 | { asser ... on" } } |
1919
| ts-import-assertions.ts:12:12:12:57 | import( ... n" } }) | ts-import-assertions.ts:12:29:12:56 | { asser ... on" } } |

0 commit comments

Comments
 (0)