Skip to content

Commit d5387c4

Browse files
committed
support arbitary import specifiers
1 parent e222b49 commit d5387c4

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3629,7 +3629,22 @@ protected List<ImportSpecifier> parseImportSpecifiers() {
36293629

36303630
protected ImportSpecifier parseImportSpecifier() {
36313631
SourceLocation loc = new SourceLocation(this.startLoc);
3632-
Identifier imported = this.parseIdent(true), local;
3632+
Identifier imported, local;
3633+
3634+
if (this.type == TokenType.string) {
3635+
// Arbitrary Module Namespace Identifiers
3636+
// e.g. `import { "Foo::new" as Foo_new } from "./foo.wasm"`
3637+
Expression string = this.parseExprAtom(null);
3638+
String str = ((Literal)string).getStringValue();
3639+
imported = new Identifier(loc, str);
3640+
// only makes sense if there is a local identifier
3641+
if (!this.isContextual("as")) {
3642+
this.raiseRecoverable(this.start, "Unexpected string");
3643+
}
3644+
} else {
3645+
imported = this.parseIdent(true);
3646+
}
3647+
36333648
if (this.eatContextual("as")) {
36343649
local = this.parseIdent(false);
36353650
} else {

javascript/extractor/src/com/semmle/js/extractor/AutoBuild.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ public int run() throws IOException {
464464
CompletableFuture<?> sourceFuture = extractSource();
465465
sourceFuture.join(); // wait for source extraction to complete
466466
if (hasSeenCode()) { // don't bother with the externs if no code was seen
467-
extractExterns();
467+
// extractExterns();
468468
}
469469
extractXml();
470470
} catch (OutOfMemoryError oom) {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import { "Foo::new" as Foo_new } from "./foo.wasm"

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ test_ImportNamespaceSpecifier
4141
| exports.js:1:8:1:17 | * as dummy |
4242
| m/c.js:1:8:1:13 | * as b |
4343
test_ImportSpecifiers
44+
| arbitarySpecifier.js:1:10:1:30 | "Foo::n ... Foo_new | arbitarySpecifier.js:1:24:1:30 | Foo_new |
4445
| b.js:1:8:1:8 | f | b.js:1:8:1:8 | f |
4546
| d.js:1:10:1:21 | default as g | d.js:1:21:1:21 | g |
4647
| d.js:1:24:1:29 | x as y | d.js:1:29:1:29 | y |
@@ -55,6 +56,7 @@ test_ImportSpecifiers
5556
| tst.html:5:10:5:10 | f | tst.html:5:10:5:10 | f |
5657
| unresolved.js:1:8:1:8 | f | unresolved.js:1:8:1:8 | f |
5758
test_Imports
59+
| arbitarySpecifier.js:1:1:1:50 | import ... o.wasm" | arbitarySpecifier.js:1:39:1:50 | "./foo.wasm" | 1 |
5860
| b.js:1:1:1:20 | import f from './a'; | b.js:1:15:1:19 | './a' | 1 |
5961
| d.js:1:1:1:43 | import ... './a'; | d.js:1:38:1:42 | './a' | 2 |
6062
| d.js:2:1:2:13 | import './b'; | d.js:2:8:2:12 | './b' | 0 |
@@ -84,6 +86,7 @@ test_Module_exports
8486
| reExportNamespace.js:1:1:2:0 | <toplevel> | ns | reExportNamespace.js:1:8:1:14 | * as ns |
8587
| tst.html:4:23:8:0 | <toplevel> | y | tst.html:7:20:7:21 | 42 |
8688
test_NamedImportSpecifier
89+
| arbitarySpecifier.js:1:10:1:30 | "Foo::n ... Foo_new |
8790
| d.js:1:10:1:21 | default as g |
8891
| d.js:1:24:1:29 | x as y |
8992
| g.ts:1:9:1:11 | foo |
@@ -119,6 +122,7 @@ test_getExportedName
119122
| m/c.js:5:10:5:15 | g as h | h |
120123
| reExportNamespace.js:1:8:1:14 | * as ns | ns |
121124
test_getImportedName
125+
| arbitarySpecifier.js:1:10:1:30 | "Foo::n ... Foo_new | Foo::new |
122126
| b.js:1:8:1:8 | f | default |
123127
| d.js:1:10:1:21 | default as g | default |
124128
| d.js:1:24:1:29 | x as y | x |

0 commit comments

Comments
 (0)