Skip to content

Commit 2d3d46e

Browse files
authored
Merge pull request github#17166 from erik-krogh/arbitarySpecifiers
JS: Parse arbitary module specifiers
2 parents ef21ee5 + bef4fe6 commit 2d3d46e

File tree

5 files changed

+188
-2
lines changed

5 files changed

+188
-2
lines changed

javascript/extractor/src/com/semmle/jcorn/Parser.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3547,7 +3547,19 @@ protected List<ExportSpecifier> parseExportSpecifiers(Set<String> exports) {
35473547

35483548
SourceLocation loc = new SourceLocation(this.startLoc);
35493549
Identifier local = this.parseIdent(this.type == TokenType._default);
3550-
Identifier exported = this.eatContextual("as") ? this.parseIdent(true) : local;
3550+
Identifier exported;
3551+
if (!this.eatContextual("as")) {
3552+
exported = local;
3553+
} else {
3554+
if (this.type == TokenType.string) {
3555+
// e.g. `export { Foo_new as "Foo::new" }`
3556+
Expression string = this.parseExprAtom(null);
3557+
String str = ((Literal)string).getStringValue();
3558+
exported = this.finishNode(new Identifier(loc, str));
3559+
} else {
3560+
exported = this.parseIdent(true);
3561+
}
3562+
}
35513563
checkExport(exports, exported.getName(), exported.getLoc().getStart());
35523564
nodes.add(this.finishNode(new ExportSpecifier(loc, local, exported)));
35533565
}
@@ -3629,7 +3641,22 @@ protected List<ImportSpecifier> parseImportSpecifiers() {
36293641

36303642
protected ImportSpecifier parseImportSpecifier() {
36313643
SourceLocation loc = new SourceLocation(this.startLoc);
3632-
Identifier imported = this.parseIdent(true), local;
3644+
Identifier imported, local;
3645+
3646+
if (this.type == TokenType.string) {
3647+
// Arbitrary Module Namespace Identifiers
3648+
// e.g. `import { "Foo::new" as Foo_new } from "./foo.wasm"`
3649+
Expression string = this.parseExprAtom(null);
3650+
String str = ((Literal)string).getStringValue();
3651+
imported = this.finishNode(new Identifier(loc, str));
3652+
// only makes sense if there is a local identifier
3653+
if (!this.isContextual("as")) {
3654+
this.raiseRecoverable(this.start, "Unexpected string");
3655+
}
3656+
} else {
3657+
imported = this.parseIdent(true);
3658+
}
3659+
36333660
if (this.eatContextual("as")) {
36343661
local = this.parseIdent(false);
36353662
} else {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import { "foo" } from "foo"; // syntax-error, but it shouldn't crash the extractor
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
#10000=@"/arbitaryModuleSpecifier.js;sourcefile"
2+
files(#10000,"/arbitaryModuleSpecifier.js")
3+
#10001=@"/;folder"
4+
folders(#10001,"/")
5+
containerparent(#10001,#10000)
6+
#10002=@"loc,{#10000},0,0,0,0"
7+
locations_default(#10002,#10000,0,0,0,0)
8+
hasLocation(#10000,#10002)
9+
#20000=@"global_scope"
10+
scopes(#20000,0)
11+
#20001=@"script;{#10000},1,1"
12+
#20002=*
13+
comments(#20002,0,#20001," syntax-error, but it shouldn't crash the extractor","// synt ... tractor")
14+
#20003=@"loc,{#10000},1,30,1,82"
15+
locations_default(#20003,#10000,1,30,1,82)
16+
hasLocation(#20002,#20003)
17+
#20004=*
18+
lines(#20004,#20001,"import { ""foo"" } from ""foo""; // syntax-error, but it shouldn't crash the extractor","
19+
")
20+
#20005=@"loc,{#10000},1,1,1,82"
21+
locations_default(#20005,#10000,1,1,1,82)
22+
hasLocation(#20004,#20005)
23+
numlines(#20001,1,1,1)
24+
#20006=*
25+
tokeninfo(#20006,7,#20001,0,"import")
26+
#20007=@"loc,{#10000},1,1,1,6"
27+
locations_default(#20007,#10000,1,1,1,6)
28+
hasLocation(#20006,#20007)
29+
#20008=*
30+
tokeninfo(#20008,8,#20001,1,"{")
31+
#20009=@"loc,{#10000},1,8,1,8"
32+
locations_default(#20009,#10000,1,8,1,8)
33+
hasLocation(#20008,#20009)
34+
#20010=*
35+
tokeninfo(#20010,4,#20001,2,"""foo""")
36+
#20011=@"loc,{#10000},1,10,1,14"
37+
locations_default(#20011,#10000,1,10,1,14)
38+
hasLocation(#20010,#20011)
39+
#20012=*
40+
tokeninfo(#20012,8,#20001,3,"}")
41+
#20013=@"loc,{#10000},1,16,1,16"
42+
locations_default(#20013,#10000,1,16,1,16)
43+
hasLocation(#20012,#20013)
44+
#20014=*
45+
tokeninfo(#20014,6,#20001,4,"from")
46+
#20015=@"loc,{#10000},1,18,1,21"
47+
locations_default(#20015,#10000,1,18,1,21)
48+
hasLocation(#20014,#20015)
49+
#20016=*
50+
tokeninfo(#20016,4,#20001,5,"""foo""")
51+
#20017=@"loc,{#10000},1,23,1,27"
52+
locations_default(#20017,#10000,1,23,1,27)
53+
hasLocation(#20016,#20017)
54+
#20018=*
55+
tokeninfo(#20018,8,#20001,6,";")
56+
#20019=@"loc,{#10000},1,28,1,28"
57+
locations_default(#20019,#10000,1,28,1,28)
58+
hasLocation(#20018,#20019)
59+
#20020=*
60+
tokeninfo(#20020,0,#20001,7,"")
61+
#20021=@"loc,{#10000},2,1,2,0"
62+
locations_default(#20021,#10000,2,1,2,0)
63+
hasLocation(#20020,#20021)
64+
next_token(#20002,#20020)
65+
toplevels(#20001,0)
66+
#20022=@"loc,{#10000},1,1,2,0"
67+
locations_default(#20022,#10000,1,1,2,0)
68+
hasLocation(#20001,#20022)
69+
#20023=@"module;{#10000},1,1"
70+
scopes(#20023,3)
71+
scopenodes(#20001,#20023)
72+
scopenesting(#20023,#20000)
73+
is_module(#20001)
74+
is_es2015_module(#20001)
75+
#20024=@"var;{foo};{#20023}"
76+
variables(#20024,"foo",#20023)
77+
#20025=@"local_type_name;{foo};{#20023}"
78+
local_type_names(#20025,"foo",#20023)
79+
#20026=@"local_namespace_name;{foo};{#20023}"
80+
local_namespace_names(#20026,"foo",#20023)
81+
variables(#20024,"foo",#20023)
82+
local_type_names(#20025,"foo",#20023)
83+
local_namespace_names(#20026,"foo",#20023)
84+
#20027=*
85+
stmts(#20027,27,#20001,0,"import ... ""foo"";")
86+
#20028=@"loc,{#10000},1,1,1,28"
87+
locations_default(#20028,#10000,1,1,1,28)
88+
hasLocation(#20027,#20028)
89+
stmt_containers(#20027,#20001)
90+
#20029=*
91+
exprs(#20029,4,#20027,-1,"""foo""")
92+
hasLocation(#20029,#20017)
93+
enclosing_stmt(#20029,#20027)
94+
expr_containers(#20029,#20001)
95+
literals("foo","""foo""",#20029)
96+
#20030=*
97+
regexpterm(#20030,14,#20029,0,"foo")
98+
#20031=@"loc,{#10000},1,24,1,26"
99+
locations_default(#20031,#10000,1,24,1,26)
100+
hasLocation(#20030,#20031)
101+
regexp_const_value(#20030,"foo")
102+
#20032=*
103+
exprs(#20032,83,#20027,0,"""foo""")
104+
hasLocation(#20032,#20011)
105+
enclosing_stmt(#20032,#20027)
106+
expr_containers(#20032,#20001)
107+
#20033=*
108+
exprs(#20033,0,#20032,0,"""foo""")
109+
hasLocation(#20033,#20011)
110+
enclosing_stmt(#20033,#20027)
111+
expr_containers(#20033,#20001)
112+
literals("foo","foo",#20033)
113+
#20034=*
114+
exprs(#20034,78,#20032,1,"""foo""")
115+
hasLocation(#20034,#20011)
116+
enclosing_stmt(#20034,#20027)
117+
expr_containers(#20034,#20001)
118+
literals("foo","foo",#20034)
119+
decl(#20034,#20024)
120+
typedecl(#20034,#20025)
121+
namespacedecl(#20034,#20026)
122+
#20035=*
123+
entry_cfg_node(#20035,#20001)
124+
#20036=@"loc,{#10000},1,1,1,0"
125+
locations_default(#20036,#10000,1,1,1,0)
126+
hasLocation(#20035,#20036)
127+
#20037=*
128+
exit_cfg_node(#20037,#20001)
129+
hasLocation(#20037,#20021)
130+
successor(#20027,#20037)
131+
successor(#20032,#20027)
132+
successor(#20035,#20032)
133+
#20038=*
134+
js_parse_errors(#20038,#20001,"Error: Unexpected string","import { ""foo"" } from ""foo""; // syntax-error, but it shouldn't crash the extractor
135+
")
136+
hasLocation(#20038,#20013)
137+
#20039=*
138+
lines(#20039,#20001,"import { ""foo"" } from ""foo""; // syntax-error, but it shouldn't crash the extractor","
139+
")
140+
hasLocation(#20039,#20005)
141+
numlines(#20001,1,0,0)
142+
numlines(#10000,1,1,1)
143+
filetype(#10000,"javascript")
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { "Foo::new" as Foo_new } from "./foo.wasm"
2+
3+
const foo = Foo_new()
4+
5+
export { Foo_new as "Foo::new" }

javascript/ql/test/library-tests/Modules/tests.expected

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ test_BulkReExportDeclarations
33
test_ExportDeclarations
44
| a.js:1:1:3:1 | export ... n 23;\\n} |
55
| a.js:5:1:5:32 | export ... } = o; |
6+
| arbitarySpecifier.js:5:1:5:32 | export ... :new" } |
67
| b.js:5:1:5:18 | export { f as g }; |
78
| b.js:7:1:7:21 | export ... './a'; |
89
| d.js:4:1:4:20 | export * from 'm/c'; |
@@ -18,6 +19,7 @@ test_ExportDefaultDeclarations
1819
| a.js:1:1:3:1 | export ... n 23;\\n} |
1920
| es2015_require.js:3:1:3:25 | export ... ss C {} |
2021
test_ExportSpecifiers
22+
| arbitarySpecifier.js:5:10:5:30 | Foo_new ... o::new" | arbitarySpecifier.js:5:10:5:16 | Foo_new | arbitarySpecifier.js:5:10:5:30 | Foo_new ... o::new" |
2123
| b.js:5:10:5:15 | f as g | b.js:5:10:5:10 | f | b.js:5:15:5:15 | g |
2224
| e.js:2:10:2:10 | x | e.js:2:10:2:10 | x | e.js:2:10:2:10 | x |
2325
| e.js:2:13:2:13 | y | e.js:2:13:2:13 | y | e.js:2:13:2:13 | y |
@@ -41,6 +43,7 @@ test_ImportNamespaceSpecifier
4143
| exports.js:1:8:1:17 | * as dummy |
4244
| m/c.js:1:8:1:13 | * as b |
4345
test_ImportSpecifiers
46+
| arbitarySpecifier.js:1:10:1:30 | "Foo::n ... Foo_new | arbitarySpecifier.js:1:24:1:30 | Foo_new |
4447
| b.js:1:8:1:8 | f | b.js:1:8:1:8 | f |
4548
| d.js:1:10:1:21 | default as g | d.js:1:21:1:21 | g |
4649
| d.js:1:24:1:29 | x as y | d.js:1:29:1:29 | y |
@@ -55,6 +58,7 @@ test_ImportSpecifiers
5558
| tst.html:5:10:5:10 | f | tst.html:5:10:5:10 | f |
5659
| unresolved.js:1:8:1:8 | f | unresolved.js:1:8:1:8 | f |
5760
test_Imports
61+
| arbitarySpecifier.js:1:1:1:50 | import ... o.wasm" | arbitarySpecifier.js:1:39:1:50 | "./foo.wasm" | 1 |
5862
| b.js:1:1:1:20 | import f from './a'; | b.js:1:15:1:19 | './a' | 1 |
5963
| d.js:1:1:1:43 | import ... './a'; | d.js:1:38:1:42 | './a' | 2 |
6064
| d.js:2:1:2:13 | import './b'; | d.js:2:8:2:12 | './b' | 0 |
@@ -72,6 +76,7 @@ test_Module_exports
7276
| a.js:1:1:5:32 | <toplevel> | default | a.js:1:16:3:1 | functio ... n 23;\\n} |
7377
| a.js:1:1:5:32 | <toplevel> | x | a.js:5:18:5:20 | f() |
7478
| a.js:1:1:5:32 | <toplevel> | y | a.js:5:25:5:25 | y |
79+
| arbitarySpecifier.js:1:1:5:32 | <toplevel> | Foo::new | arbitarySpecifier.js:5:10:5:16 | Foo_new |
7580
| b.js:1:1:8:0 | <toplevel> | f2 | a.js:1:16:3:1 | functio ... n 23;\\n} |
7681
| b.js:1:1:8:0 | <toplevel> | g | b.js:5:10:5:10 | f |
7782
| e.js:1:1:4:0 | <toplevel> | g | a.js:1:16:3:1 | functio ... n 23;\\n} |
@@ -84,6 +89,7 @@ test_Module_exports
8489
| reExportNamespace.js:1:1:2:0 | <toplevel> | ns | reExportNamespace.js:1:8:1:14 | * as ns |
8590
| tst.html:4:23:8:0 | <toplevel> | y | tst.html:7:20:7:21 | 42 |
8691
test_NamedImportSpecifier
92+
| arbitarySpecifier.js:1:10:1:30 | "Foo::n ... Foo_new |
8793
| d.js:1:10:1:21 | default as g |
8894
| d.js:1:24:1:29 | x as y |
8995
| g.ts:1:9:1:11 | foo |
@@ -111,6 +117,7 @@ test_getAnImportedModule
111117
| library-tests/Modules/m/c.js | library-tests/Modules/b.js |
112118
| library-tests/Modules/reExportNamespaceClient.js | library-tests/Modules/reExportNamespace.js |
113119
test_getExportedName
120+
| arbitarySpecifier.js:5:10:5:30 | Foo_new ... o::new" | Foo::new |
114121
| b.js:5:10:5:15 | f as g | g |
115122
| b.js:7:8:7:9 | f2 | f2 |
116123
| e.js:2:10:2:10 | x | x |
@@ -119,6 +126,7 @@ test_getExportedName
119126
| m/c.js:5:10:5:15 | g as h | h |
120127
| reExportNamespace.js:1:8:1:14 | * as ns | ns |
121128
test_getImportedName
129+
| arbitarySpecifier.js:1:10:1:30 | "Foo::n ... Foo_new | Foo::new |
122130
| b.js:1:8:1:8 | f | default |
123131
| d.js:1:10:1:21 | default as g | default |
124132
| d.js:1:24:1:29 | x as y | x |
@@ -131,6 +139,7 @@ test_getImportedName
131139
| tst.html:5:10:5:10 | f | default |
132140
| unresolved.js:1:8:1:8 | f | default |
133141
test_getLocalName
142+
| arbitarySpecifier.js:5:10:5:30 | Foo_new ... o::new" | Foo_new |
134143
| b.js:5:10:5:15 | f as g | f |
135144
| b.js:7:8:7:9 | f2 | default |
136145
| e.js:2:10:2:10 | x | x |
@@ -141,6 +150,7 @@ test_getSourceNode
141150
| a.js:1:1:3:1 | export ... n 23;\\n} | default | a.js:1:16:3:1 | functio ... n 23;\\n} |
142151
| a.js:5:1:5:32 | export ... } = o; | x | a.js:5:18:5:20 | f() |
143152
| a.js:5:1:5:32 | export ... } = o; | y | a.js:5:25:5:25 | y |
153+
| arbitarySpecifier.js:5:1:5:32 | export ... :new" } | Foo::new | arbitarySpecifier.js:5:10:5:16 | Foo_new |
144154
| b.js:5:1:5:18 | export { f as g }; | g | b.js:5:10:5:10 | f |
145155
| b.js:7:1:7:21 | export ... './a'; | f2 | a.js:1:16:3:1 | functio ... n 23;\\n} |
146156
| e.js:2:1:2:16 | export { x, y }; | x | e.js:2:10:2:10 | x |

0 commit comments

Comments
 (0)