Skip to content

Commit b21a360

Browse files
committed
chang to reject the combination of allowAwaitOutsideFunction and commonjs
1 parent e610dd2 commit b21a360

File tree

7 files changed

+17
-9
lines changed

7 files changed

+17
-9
lines changed

acorn/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ required):
6565
and parsing of `import` and `export` declarations.
6666

6767
**NOTE**: If set to `"module"`, then static `import` / `export` syntax
68-
will be valid, even if `ecmaVersion` is less than 6. If set to `"commonjs"`, it is the same as `"script"` except that the top-level scope behaves like a function.
68+
will be valid, even if `ecmaVersion` is less than 6. If set to `"commonjs"`,
69+
it is the same as `"script"` except that the top-level scope behaves like a function.
6970

7071
- **onInsertedSemicolon**: If given a callback, that callback will be
7172
called whenever a missing semicolon is inserted by the parser. The
@@ -97,7 +98,7 @@ required):
9798
for `ecmaVersion` 2022 and later, `false` for lower versions.
9899
Setting this option to `true` allows to have top-level `await`
99100
expressions. They are still not allowed in non-`async` functions,
100-
though.
101+
though. Setting this option to `true` is not allowed when `sourceType: "commonjs"`.
101102

102103
- **allowSuperOutsideMethod**: By default, `super` outside a method
103104
raises an error. Set this to `true` to accept such code.

acorn/src/options.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ export function getOptions(opts) {
137137
if (isArray(options.onComment))
138138
options.onComment = pushComment(options, options.onComment)
139139

140+
if (options.sourceType === "commonjs" && options.allowAwaitOutsideFunction)
141+
throw new Error("Cannot use allowAwaitOutsideFunction with sourceType: commonjs")
142+
140143
return options
141144
}
142145

acorn/src/state.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import {getOptions} from "./options.js"
55
import {wordsRegexp} from "./util.js"
66
import {
77
SCOPE_TOP, SCOPE_FUNCTION, SCOPE_ASYNC, SCOPE_GENERATOR, SCOPE_SUPER, SCOPE_DIRECT_SUPER,
8-
SCOPE_ARROW, SCOPE_CLASS_STATIC_BLOCK, SCOPE_CLASS_FIELD_INIT, SCOPE_SWITCH,
9-
functionFlags
8+
SCOPE_ARROW, SCOPE_CLASS_STATIC_BLOCK, SCOPE_CLASS_FIELD_INIT, SCOPE_SWITCH
109
} from "./scopeflags.js"
1110

1211
export class Parser {
@@ -87,7 +86,7 @@ export class Parser {
8786
this.enterScope(
8887
this.options.sourceType === "commonjs"
8988
// In commonjs, the top-level scope behaves like a function scope
90-
? functionFlags(this.options.allowAwaitOutsideFunction, false)
89+
? SCOPE_FUNCTION
9190
: SCOPE_TOP
9291
)
9392

test/driver.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ exports.runTests = function(config, callback) {
2727
try {
2828
var ast = parse(test.code, testOpts);
2929
} catch(e) {
30-
if (!(e instanceof SyntaxError)) { console.log(e.stack); throw e; }
3130
if (test.error) {
3231
if (test.error.charAt(0) === "~" ? e.message.indexOf(test.error.slice(1)) > -1 : e.message === test.error)
3332
callback("ok", test.code);
3433
else
3534
callback("fail", test.code, "Expected error message: " + test.error + "\nGot error message: " + e.message);
3635
} else {
36+
if (!(e instanceof SyntaxError)) { console.log(e.stack); throw e; }
3737
callback("error", test.code, e.message || e.toString());
3838
}
3939
continue

test/run.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
parse: (code, option) => acorn.parse(code, Object.assign({}, option, { sourceType: 'commonjs' })),
9494
filter: function (test) {
9595
var opts = test.options || {};
96-
return opts.commonjs !== false && (!opts.sourceType || opts.sourceType === 'script');
96+
return opts.commonjs !== false && !opts.allowAwaitOutsideFunction && (!opts.sourceType || opts.sourceType === 'script');
9797
}
9898
}
9999
},
@@ -104,7 +104,7 @@
104104
filter: function (test) {
105105
var opts = test.options || {};
106106
if (opts.loose === false) return false;
107-
return opts.commonjs !== false && (!opts.sourceType || opts.sourceType === 'script');
107+
return opts.commonjs !== false && !opts.allowAwaitOutsideFunction && (!opts.sourceType || opts.sourceType === 'script');
108108
}
109109
}
110110
}

test/tests-await-top-level.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ test("await 1", {
3030
}
3131
}
3232
]
33-
}, {allowAwaitOutsideFunction: true, ecmaVersion: 8, commonjs: false})
33+
}, {allowAwaitOutsideFunction: true, ecmaVersion: 8})
3434
testFail("function foo() {return await 1}", "Unexpected token (1:29)", {ecmaVersion: 8})
3535
testFail("function foo() {return await 1}", "Unexpected token (1:29)", {
3636
allowAwaitOutsideFunction: true,

test/tests-commonjs.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,8 @@ testFail(`class X { static { return; } }`, "'return' outside of function (1:19)"
9090

9191
// Top-level await using declaration with commonjs
9292
testFail("await using x = resource;", "Await using cannot appear outside of async function (1:0)", {ecmaVersion: 17, sourceType: "commonjs"});
93+
94+
// Disallowing allowAwaitOutsideFunction with commonjs
95+
testFail("await 1", "Cannot use allowAwaitOutsideFunction with sourceType: commonjs", {allowAwaitOutsideFunction: true, sourceType: "commonjs"});
96+
testFail("x", "Cannot use allowAwaitOutsideFunction with sourceType: commonjs", {allowAwaitOutsideFunction: true, sourceType: "commonjs"});
97+
test("x", {}, {allowAwaitOutsideFunction: false, sourceType: "commonjs"});

0 commit comments

Comments
 (0)