Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions javascript/extractor/src/com/semmle/jcorn/ESNextParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,7 @@ protected CatchClause parseCatchClause(Position startLoc) {
}

/*
* Support for proposed language feature "Dynamic import"
* (https://github.com/tc39/proposal-dynamic-import).
* Support for `import.meta` and dynamic `import(...)` at the start of an expression statement.
*/
@Override
protected Statement parseImport(Position startLoc) {
Expand All @@ -408,9 +407,13 @@ protected Statement parseImport(Position startLoc) {
int startPos = this.start;
SourceLocation loc = new SourceLocation(startLoc);
this.next();
if (this.eat(TokenType.parenL)) {
if (this.eat(TokenType.dot)) {
MetaProperty meta = parseImportMeta(startLoc);
Expression expr = parseExpressionFromAtom(meta, startPos, startLoc, false);
return parseExpressionStatement(false, startLoc, expr);
} else if (this.eat(TokenType.parenL)) {
DynamicImport di = parseDynamicImport(startLoc);
Expression expr = parseSubscripts(di, startPos, startLoc, false);
Expression expr = parseExpressionFromAtom(di, startPos, startLoc, false);
return parseExpressionStatement(false, startLoc, expr);
} else {
return super.parseImportRest(loc);
Expand Down
54 changes: 45 additions & 9 deletions javascript/extractor/src/com/semmle/jcorn/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -1346,6 +1346,10 @@ private void checkPropClash(Property prop, Map<String, PropInfo> propHash) {
protected Expression parseExpression(boolean noIn) {
Position startLoc = this.startLoc;
Expression expr = this.parseMaybeAssign(noIn, null);
return this.parseExpressionRest(expr, startLoc, noIn);
}

protected Expression parseExpressionRest(Expression expr, Position startLoc, boolean noIn) {
if (this.type == TokenType.comma) {
List<Expression> expressions = CollectionUtil.makeList(expr);
SequenceExpression node = new SequenceExpression(new SourceLocation(startLoc), expressions);
Expand All @@ -1356,6 +1360,20 @@ protected Expression parseExpression(boolean noIn) {
return expr;
}

/**
* Continue parsing a full expression after its leading atomic expression {@code atom} has
* already been parsed.
*/
protected Expression parseExpressionFromAtom(
Expression atom, int startPos, Position startLoc, boolean noIn) {
Expression expr = this.parseSubscripts(atom, startPos, startLoc, false);
expr = this.parseMaybeUnaryRest(expr, startPos, startLoc, false);
expr = this.parseExprOp(expr, startPos, startLoc, -1, noIn);
expr = this.parseMaybeConditionalRest(expr, startLoc, noIn);
expr = this.parseMaybeAssignRest(expr, startPos, startLoc, noIn, null);
return this.parseExpressionRest(expr, startLoc, noIn);
}

public interface AfterLeftParse {
Expression call(Expression left, int startPos, Position startLoc);
}
Expand All @@ -1370,6 +1388,11 @@ protected Expression parseMaybeAssign(boolean noIn, AfterLeftParse afterLeftPars
if (this.type == TokenType.parenL || this.type == TokenType.name)
this.potentialArrowAt = this.start;
Expression left = this.parseMaybeConditional(noIn);
return this.parseMaybeAssignRest(left, startPos, startLoc, noIn, afterLeftParse);
}

protected Expression parseMaybeAssignRest(
Expression left, int startPos, Position startLoc, boolean noIn, AfterLeftParse afterLeftParse) {
if (afterLeftParse != null) left = afterLeftParse.call(left, startPos, startLoc);
if (this.type.isAssign) {
Expression l = this.type == TokenType.eq ? (Expression) this.toAssignable(left, false) : left;
Expand All @@ -1388,6 +1411,11 @@ protected Expression parseMaybeAssign(boolean noIn, AfterLeftParse afterLeftPars
protected Expression parseMaybeConditional(boolean noIn) {
Position startLoc = this.startLoc;
Expression expr = this.parseExprOps(noIn);
return this.parseMaybeConditionalRest(expr, startLoc, noIn);
}

protected Expression parseMaybeConditionalRest(
Expression expr, Position startLoc, boolean noIn) {
if (this.eat(TokenType.question)) {
return parseConditionalRest(noIn, startLoc, expr);
}
Expand Down Expand Up @@ -1474,15 +1502,7 @@ else if (this.strict && operator.equals("delete") && argument instanceof Identif
else sawUnary = true;
expr = this.finishNode(node);
} else {
expr = this.parseExprSubscripts();
while (this.type.isPostfix && !this.canInsertSemicolon()) {
UpdateExpression node =
new UpdateExpression(
new SourceLocation(startLoc), String.valueOf(this.value), expr, false);
this.checkLVal(expr, false, null);
this.next();
expr = this.finishNode(node);
}
return this.parseMaybeUnaryRest(this.parseExprSubscripts(), startPos, startLoc, sawUnary);
}

if (!sawUnary && this.eat(TokenType.starstar))
Expand All @@ -1491,6 +1511,22 @@ else if (this.strict && operator.equals("delete") && argument instanceof Identif
else return expr;
}

protected Expression parseMaybeUnaryRest(
Expression expr, int startPos, Position startLoc, boolean sawUnary) {
while (this.type.isPostfix && !this.canInsertSemicolon()) {
UpdateExpression node =
new UpdateExpression(
new SourceLocation(startLoc), String.valueOf(this.value), expr, false);
this.checkLVal(expr, false, null);
this.next();
expr = this.finishNode(node);
}
if (!sawUnary && this.eat(TokenType.starstar))
return this.buildBinary(
startPos, startLoc, expr, this.parseMaybeUnary(false), "**", false);
else return expr;
}

// Parse call, dot, and `[]`-subscript expressions.
protected Expression parseExprSubscripts() {
int startPos = this.start;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import("./mod.js") || fallback();
import("./mod.js") ? ok() : fail();
import /* comment */ ("./a.js"), import("./b.js");
import ("./nbsp.js") || fallback();
17 changes: 17 additions & 0 deletions javascript/extractor/tests/esnext/input/import-meta.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function createNuxtAppServer() {}

import.meta.webpackHot.accept();
import.meta.env.TEST && init();
if (a) import.meta.webpackHot.accept();
if (ENVIRONMENT_IS_NODE) {
const r = c ? (a.b()) : y => z;
import.meta.url.startsWith("data:") || setup();
}
import.meta.hot ? import.meta.hot.accept() : null;
import.meta.foo = 1;
import.meta.count++;
import.meta.val ** 2;
import /* comment */ .meta.bar();
import
.meta.baz();
import .meta.nbsp();
Loading
Loading