Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
/* eslint-disable no-underscore-dangle */
import { tokTypes as tt } from 'acorn';
export const DynamicImportKey = 'Import';
// NOTE: This allows `yield import()` to parse correctly.
tt._import.startsExpr = true;
function parseDynamicImport() {
const node = this.startNode();
this.next();
if (this.type !== tt.parenL) {
this.unexpected();
}
return this.finishNode(node, DynamicImportKey);
}
function parenAfter() {
return /^(\s|\/\/.*|\/\*[^]*?\*\/)*\(/.test(this.input.slice(this.pos));
}
export default function dynamicImport(Parser) {
return class extends Parser {
/* eslint-disable no-underscore-dangle */
import { tokTypes as tt } from 'acorn';
export const DynamicImportKey = 'Import';
// NOTE: This allows `yield import()` to parse correctly.
tt._import.startsExpr = true;
function parseDynamicImport() {
const node = this.startNode();
this.next();
if (this.type !== tt.parenL) {
this.unexpected();
}
return this.finishNode(node, DynamicImportKey);
}
function parenAfter() {
return /^(\s|\/\/.*|\/\*[^]*?\*\/)*\(/.test(this.input.slice(this.pos));
}
export default function dynamicImport(Parser) {
return class extends Parser {
// A more accurate version of acorn-dynamic-import.
// Copyright Jordan Gensler. Released under MIT license:
// https://github.com/kesne/acorn-dynamic-import
// https://github.com/standard-things/esm/blob/0.18.0/src/acorn-ext/dynamic-import.js
import {tokTypes as tt} from "acorn";
// NOTE: This allows `yield import()` to parse correctly.
tt._import.startsExpr = true;
function parseImportCall(parser) {
const node = parser.startNode();
const {start} = parser;
const callExpr = parser.startNode();
const callee = parser.parseExprAtom();
parser.expect(tt.parenL);
callExpr.arguments = [parser.parseMaybeAssign()];
callExpr.callee = callee;
parser.finishNode(callExpr, "CallExpression");
parser.expect(tt.parenR);
const expr = parser.parseSubscripts(callExpr, start);
return parser.parseExpressionStatement(node, expr);
}
function parseImportCallAtom(parser) {
parseExprAtom(refDestructuringErrors) {
if (this.type === tt._import) {
return parseDynamicImport.call(this);
}
return super.parseExprAtom(refDestructuringErrors);
}
};
this.next()
node.object = this.parseParenExpression()
node.body = this.parseStatement()
return this.finishNode(node, "WithStatement")
case tt.braceL:
return this.parseBlock()
case tt.semi:
this.next()
return this.finishNode(node, "EmptyStatement")
case tt._class:
return this.parseClass(true)
case tt._import:
return this.parseImport()
case tt._export:
return this.parseExport()
default:
let expr = this.parseExpression()
if (isDummy(expr)) {
this.next()
if (this.tok.type === tt.eof) return this.finishNode(node, "EmptyStatement")
return this.parseStatement()
} else if (starttype === tt.name && expr.type === "Identifier" && this.eat(tt.colon)) {
node.body = this.parseStatement()
node.label = expr
return this.finishNode(node, "LabeledStatement")
} else {
anonymous.prototype.parseExprAtom = function parseExprAtom (refDestructuringErrors) {
if (this.type !== tt._import || !nextTokenIsDot(this)) { return Parser.prototype.parseExprAtom.call(this, refDestructuringErrors) }
if (!this.options.allowImportExportEverywhere && !this.inModule) {
this.raise(this.start, "'import' and 'export' may appear only with 'sourceType: module'")
}
var node = this.startNode()
node.meta = this.parseIdent(true)
this.expect(tt.dot)
node.property = this.parseIdent(true)
if (node.property.name !== "meta") {
this.raiseRecoverable(node.property.start, "The only valid meta property for import is import.meta")
}
return this.finishNode(node, "MetaProperty")
};
this.next()
node.object = this.parseParenExpression()
node.body = this.parseStatement()
return this.finishNode(node, "WithStatement")
case tt.braceL:
return this.parseBlock()
case tt.semi:
this.next()
return this.finishNode(node, "EmptyStatement")
case tt._class:
return this.parseClass(true)
case tt._import:
if (this.options.ecmaVersion > 10 && this.lookAhead(1).type === tt.parenL) {
node.expression = this.parseExpression()
this.semicolon()
return this.finishNode(node, "ExpressionStatement")
}
return this.parseImport()
case tt._export:
return this.parseExport()
default:
if (this.toks.isAsyncFunction()) {
this.next()
this.next()
return this.parseFunction(node, true, true)
parseStatement(context, topLevel, exports) {
if (this.type === tt._import && parenAfter.call(this)) {
return this.parseExpressionStatement(this.startNode(), this.parseExpression());
}
return super.parseStatement(context, topLevel, exports);
}
parseStatement(context, topLevel, exports) {
if (this.type === tt._import && parenAfter.call(this)) {
return this.parseExpressionStatement(this.startNode(), this.parseExpression());
}
return super.parseStatement(context, topLevel, exports);
}