Skip to content

Commit e610dd2

Browse files
committed
add test and fix commonjs with allowAwaitOutsideFunction
1 parent 4a5f923 commit e610dd2

File tree

7 files changed

+35
-8
lines changed

7 files changed

+35
-8
lines changed

acorn/src/state.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ 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
8+
SCOPE_ARROW, SCOPE_CLASS_STATIC_BLOCK, SCOPE_CLASS_FIELD_INIT, SCOPE_SWITCH,
9+
functionFlags
910
} from "./scopeflags.js"
1011

1112
export class Parser {
@@ -85,7 +86,8 @@ export class Parser {
8586
this.scopeStack = []
8687
this.enterScope(
8788
this.options.sourceType === "commonjs"
88-
? SCOPE_FUNCTION // In commonjs, the top-level scope behaves like a function scope
89+
// In commonjs, the top-level scope behaves like a function scope
90+
? functionFlags(this.options.allowAwaitOutsideFunction, false)
8991
: SCOPE_TOP
9092
)
9193

test/run.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,28 @@
8585
return opts.loose !== false;
8686
}
8787
}
88+
},
89+
90+
// Test whether the test for `sourceType: 'script'` produces the same result for `'commonjs'`.
91+
'Normal with sourceType: commonjs': {
92+
config: {
93+
parse: (code, option) => acorn.parse(code, Object.assign({}, option, { sourceType: 'commonjs' })),
94+
filter: function (test) {
95+
var opts = test.options || {};
96+
return opts.commonjs !== false && (!opts.sourceType || opts.sourceType === 'script');
97+
}
98+
}
99+
},
100+
'Loose with sourceType: commonjs': {
101+
config: {
102+
parse: (code, option) => acorn_loose.parse(code, Object.assign({}, option, { sourceType: 'commonjs' })),
103+
loose: true,
104+
filter: function (test) {
105+
var opts = test.options || {};
106+
if (opts.loose === false) return false;
107+
return opts.commonjs !== false && (!opts.sourceType || opts.sourceType === 'script');
108+
}
109+
}
88110
}
89111
};
90112

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})
33+
}, {allowAwaitOutsideFunction: true, ecmaVersion: 8, commonjs: false})
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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,6 @@ test("return {} / 2", {
8787

8888
// Illegal return statement with commonjs
8989
testFail(`class X { static { return; } }`, "'return' outside of function (1:19)", {ecmaVersion: 13, sourceType: "commonjs"});
90+
91+
// Top-level await using declaration with commonjs
92+
testFail("await using x = resource;", "Await using cannot appear outside of async function (1:0)", {ecmaVersion: 17, sourceType: "commonjs"});

test/tests-harmony.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14873,9 +14873,9 @@ test("function foo() { new.target; }", {
1487314873
}, {ecmaVersion: 6});
1487414874

1487514875
testFail("new.prop", "The only valid meta property for new is 'new.target' (1:4)", {ecmaVersion: 6});
14876-
testFail("new.target", "'new.target' can only be used in functions and class static block (1:0)", {ecmaVersion: 6});
14876+
testFail("new.target", "'new.target' can only be used in functions and class static block (1:0)", {ecmaVersion: 6, commonjs: false});
1487714877
test("function x() { return () => new.target }", {}, {ecmaVersion: 6});
14878-
testFail("let y = () => new.target", "'new.target' can only be used in functions and class static block (1:14)", {ecmaVersion: 6});
14878+
testFail("let y = () => new.target", "'new.target' can only be used in functions and class static block (1:14)", {ecmaVersion: 6, commonjs: false});
1487914879

1488014880
test("export default function foo() {} false", {
1488114881
body: [

test/tests-using.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,14 +1102,14 @@ testFail("let await using x = resource;", "Cannot use keyword 'await' outside an
11021102
// let using is not allowed
11031103
testFail("let using x = resource;", "Unexpected token (1:10)", {ecmaVersion: 17, sourceType: "module"});
11041104
// top level using is not allowed
1105-
testFail("using x = resource;", "Using declaration cannot appear in the top level when source type is `script` or in the bare case statement (1:0)", {ecmaVersion: 17, sourceType: "script"});
1105+
testFail("using x = resource;", "Using declaration cannot appear in the top level when source type is `script` or in the bare case statement (1:0)", {ecmaVersion: 17, sourceType: "script", commonjs: false});
11061106

11071107
// BoundNames contains "let"
11081108
testFail("async function test() { await using let = resource; }", "The keyword 'let' is reserved (1:36)", {ecmaVersion: 17, sourceType: "module"});
11091109
// BoundNames contains duplicate entries
11101110
testFail("async function test() { await using x = resource1, x = resource2; }", "Identifier 'x' has already been declared (1:51)", {ecmaVersion: 17, sourceType: "module"});
11111111
// top level await using is not allowed
1112-
testFail("await using x = resource;", "Using declaration cannot appear in the top level when source type is `script` or in the bare case statement (1:0)", {ecmaVersion: 17, sourceType: "script"});
1112+
testFail("await using x = resource;", "Using declaration cannot appear in the top level when source type is `script` or in the bare case statement (1:0)", {ecmaVersion: 17, sourceType: "script", commonjs: false});
11131113

11141114
// Basic missing initializer
11151115
testFail("{ using x; }", "Missing initializer in using declaration (1:9)", {ecmaVersion: 17, sourceType: "module"});

test/tests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27458,7 +27458,7 @@ testFail("\"\\u",
2745827458
"Bad character escape sequence (1:3)");
2745927459

2746027460
testFail("return",
27461-
"'return' outside of function (1:0)");
27461+
"'return' outside of function (1:0)", { commonjs: false });
2746227462

2746327463
testFail("break",
2746427464
"Unsyntactic break (1:0)");

0 commit comments

Comments
 (0)