Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4fdfa51

Browse files
committed
add support for import.meta expressions in JavaScript
1 parent 66d49a4 commit 4fdfa51

6 files changed

Lines changed: 29 additions & 2 deletions

File tree

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,9 @@ protected Expression parseExprAtom(DestructuringErrors refDestructuringErrors) {
240240
if (this.type == TokenType._import) {
241241
Position startLoc = this.startLoc;
242242
this.next();
243+
if (this.type == TokenType.dot) {
244+
return parseImportMeta(startLoc);
245+
}
243246
this.expect(TokenType.parenL);
244247
return parseDynamicImport(startLoc);
245248
}
@@ -413,6 +416,20 @@ protected Statement parseImport(Position startLoc) {
413416
}
414417
}
415418

419+
/**
420+
* Parses an import.meta expression, assuming that the initial "import" has been consumed.
421+
*/
422+
private MetaProperty parseImportMeta(Position loc) {
423+
Identifier meta = new Identifier(new SourceLocation(loc), "import");
424+
this.next();
425+
Position propertyLoc = this.startLoc;
426+
Identifier property = this.parseIdent(true);
427+
if (!property.getName().equals("meta")) {
428+
this.unexpected(propertyLoc);
429+
}
430+
return this.finishNode(new MetaProperty(new SourceLocation(loc), meta, property));
431+
}
432+
416433
/**
417434
* Parses a dynamic import, assuming that the keyword `import` and the opening parenthesis have
418435
* already been consumed.

javascript/extractor/src/com/semmle/js/ast/MetaProperty.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
/**
44
* A meta property access (cf. ECMAScript 2015 Language Specification, Chapter 12.3.8).
55
*
6-
* <p>Currently the only recognised meta properties are <code>new.target</code> and <code>
7-
* function.sent</code>.
6+
* <p>Currently the only recognised meta properties are <code>new.target</code>,
7+
* <code>import.meta</code> and <code> function.sent</code>.
88
*/
99
public class MetaProperty extends Expression {
1010
private final Identifier meta, property;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import semmle.javascript.ES2015Modules
2+
3+
query predicate test_ImportMetaExpr(ImportMetaExpr meta) {
4+
any()
5+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
var foo = new URL('../cli.svg', import.meta.url).pathname;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ test_getExportedName
2626
| m/c.js:5:10:5:15 | g as h | g |
2727
test_OtherImports
2828
| es2015_require.js:1:11:1:24 | require('./d') | d.js:1:1:5:0 | <toplevel> |
29+
test_ImportMetaExpr
30+
| importmeta.js:1:33:1:43 | import.meta |
2931
test_ReExportDeclarations
3032
| b.js:7:1:7:21 | export ... './a'; | b.js:7:16:7:20 | './a' |
3133
| d.js:4:1:4:20 | export * from 'm/c'; | d.js:4:15:4:19 | 'm/c' |
@@ -102,6 +104,7 @@ test_NamedImportSpecifier
102104
test_GlobalVariableRef
103105
| a.js:5:31:5:31 | o |
104106
| exports.js:3:9:3:15 | exports |
107+
| importmeta.js:1:15:1:17 | URL |
105108
| tst.html:6:3:6:7 | alert |
106109
test_BulkReExportDeclarations
107110
| d.js:4:1:4:20 | export * from 'm/c'; |

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import ImportSpecifiers
22
import getLocalName
33
import getExportedName
44
import OtherImports
5+
import ImportMeta
56
import ReExportDeclarations
67
import ImportDefaultSpecifiers
78
import getImportedName

0 commit comments

Comments
 (0)