Skip to content

Commit 513c16e

Browse files
bmeurermarijnh
authored andcommitted
Support import.meta in scripts (via allowImportExportEverywhere).
Previously the acorn parser would always throw an error when it hits an `import.meta` expression while parsing a script, while it would happily parse `import` and `export` (and in particular dynamic `import()`) outside of modules as long as `allowImportExportEverywhere` was set to `true`. This changes the behavior to also allow `import.meta` in scripts as long as `allowImportExportEverywhere` is `true`. The motivation here is for the Chromium DevTools pretty print feature, where we use the acorn parser, and where we don't necessarily know for sure whether the user is trying to pretty print a module or a script, and for the purpose of pretty printing that doesn't really matter anyways. Issue: https://crbug.com/1185578
1 parent 74b5938 commit 513c16e

4 files changed

Lines changed: 42 additions & 4 deletions

File tree

acorn/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,10 @@ required):
9090

9191
- **allowImportExportEverywhere**: By default, `import` and `export`
9292
declarations can only appear at a program's top level. Setting this
93-
option to `true` allows them anywhere where a statement is allowed.
94-
93+
option to `true` allows them anywhere where a statement is allowed,
94+
and also allows `import.meta` expressions to appear in scripts
95+
(when `sourceType` is not `"module"`).
96+
9597
- **allowAwaitOutsideFunction**: By default, `await` expressions can
9698
only appear inside `async` functions. Setting this option to
9799
`true` allows to have top-level `await` expressions. They are

acorn/src/expression.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ pp.parseImportMeta = function(node) {
513513
this.raiseRecoverable(node.property.start, "The only valid meta property for import is 'import.meta'")
514514
if (containsEsc)
515515
this.raiseRecoverable(node.start, "'import.meta' must not contain escaped characters")
516-
if (this.options.sourceType !== "module")
516+
if (this.options.sourceType !== "module" && !this.options.allowImportExportEverywhere)
517517
this.raiseRecoverable(node.start, "Cannot use 'import.meta' outside a module")
518518

519519
return this.finishNode(node, "MetaProperty")

acorn/src/options.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ export const defaultOptions = {
3333
// error.
3434
allowReturnOutsideFunction: false,
3535
// When enabled, import/export statements are not constrained to
36-
// appearing at the top of the program.
36+
// appearing at the top of the program, and an import.meta expression
37+
// in a script isn't considered an error.
3738
allowImportExportEverywhere: false,
3839
// When enabled, await identifiers are allowed to appear at the top-level scope,
3940
// but they are still not allowed in non-async functions.

test/tests-import-meta.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,41 @@ test(
4040
{ ecmaVersion: 11, sourceType: "module" }
4141
);
4242

43+
test(
44+
"import.meta",
45+
{
46+
"type": "Program",
47+
"start": 0,
48+
"end": 11,
49+
"body": [
50+
{
51+
"type": "ExpressionStatement",
52+
"start": 0,
53+
"end": 11,
54+
"expression": {
55+
"type": "MetaProperty",
56+
"start": 0,
57+
"end": 11,
58+
"meta": {
59+
"type": "Identifier",
60+
"start": 0,
61+
"end": 6,
62+
"name": "import"
63+
},
64+
"property": {
65+
"type": "Identifier",
66+
"start": 7,
67+
"end": 11,
68+
"name": "meta"
69+
}
70+
}
71+
}
72+
],
73+
"sourceType": "script"
74+
},
75+
{ ecmaVersion: 11, sourceType: "script", allowImportExportEverywhere: true }
76+
);
77+
4378
test(
4479
"import.meta.url",
4580
{

0 commit comments

Comments
 (0)