Skip to content

Commit 339066c

Browse files
authored
Merge pull request #2552 from erik-krogh/ImportMeta
Approved by max-schaefer
2 parents 52d231c + 0611dc3 commit 339066c

File tree

8 files changed

+35
-3
lines changed

8 files changed

+35
-3
lines changed

change-notes/1.24/analysis-javascript.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,3 @@
2727
## Changes to libraries
2828

2929
* The predicates `RegExpTerm.getSuccessor` and `RegExpTerm.getPredecessor` have been changed to reflect textual, not operational, matching order. This only makes a difference in lookbehind assertions, which are operationally matched backwards. Previously, `getSuccessor` would mimick this, so in an assertion `(?<=ab)` the term `b` would be considered the predecessor, not the successor, of `a`. Textually, however, `a` is still matched before `b`, and this is the order we now follow.
30-
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[[ condition: enterprise-only ]]
2+
3+
# Improvements to JavaScript analysis
4+
5+
## Changes to code extraction
6+
7+
* `import.meta` expressions no longer result in a syntax error in JavaScript files.

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

Lines changed: 16 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.eat(TokenType.dot)) {
244+
return parseImportMeta(startLoc);
245+
}
243246
this.expect(TokenType.parenL);
244247
return parseDynamicImport(startLoc);
245248
}
@@ -413,6 +416,19 @@ protected Statement parseImport(Position startLoc) {
413416
}
414417
}
415418

419+
/**
420+
* Parses an import.meta expression, assuming that the initial "import" and "." has been consumed.
421+
*/
422+
private MetaProperty parseImportMeta(Position loc) {
423+
Position propertyLoc = this.startLoc;
424+
Identifier property = this.parseIdent(true);
425+
if (!property.getName().equals("meta")) {
426+
this.unexpected(propertyLoc);
427+
}
428+
return this.finishNode(
429+
new MetaProperty(new SourceLocation(loc), new Identifier(new SourceLocation(loc), "import"), property));
430+
}
431+
416432
/**
417433
* Parses a dynamic import, assuming that the keyword `import` and the opening parenthesis have
418434
* 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)