Skip to content

Commit

Permalink
Add allowSuperOutsideMethod option
Browse files Browse the repository at this point in the history
  • Loading branch information
Žiga Zupančič authored and marijnh committed Jun 11, 2021
1 parent ddfd5e3 commit 3ee1b45
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 1 deletion.
3 changes: 3 additions & 0 deletions acorn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ required):
`true` allows to have top-level `await` expressions. They are
still not allowed in non-`async` functions, though.

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

- **allowHashBang**: When this is enabled (off by default), if the
code starts with the characters `#!` (as in a shellscript), the
first line will be treated as a comment.
Expand Down
1 change: 1 addition & 0 deletions acorn/dist/acorn.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ declare namespace acorn {
allowReturnOutsideFunction?: boolean
allowImportExportEverywhere?: boolean
allowAwaitOutsideFunction?: boolean
allowSuperOutsideMethod?: boolean
allowHashBang?: boolean
locations?: boolean
onToken?: ((token: Token) => any) | Token[]
Expand Down
3 changes: 3 additions & 0 deletions acorn/src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ export const defaultOptions = {
// When enabled, await identifiers are allowed to appear at the top-level scope,
// but they are still not allowed in non-async functions.
allowAwaitOutsideFunction: null,
// When enabled, super identifiers are not constrained to
// appearing in methods and do not raise an error when they appear elsewhere.
allowSuperOutsideMethod: null,
// When enabled, hashbang directive in the beginning of file
// is allowed and treated as a line comment.
allowHashBang: false,
Expand Down
2 changes: 1 addition & 1 deletion acorn/src/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export class Parser {
get inAsync() { return (this.currentVarScope().flags & SCOPE_ASYNC) > 0 && !this.currentVarScope().inClassFieldInit }
get allowSuper() {
const {flags, inClassFieldInit} = this.currentThisScope()
return (flags & SCOPE_SUPER) > 0 || inClassFieldInit
return (flags & SCOPE_SUPER) > 0 || inClassFieldInit || this.options.allowSuperOutsideMethod
}
get allowDirectSuper() { return (this.currentThisScope().flags & SCOPE_DIRECT_SUPER) > 0 }
get treatFunctionsAsVar() { return this.treatFunctionsAsVarInScope(this.currentScope()) }
Expand Down
1 change: 1 addition & 0 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -26966,6 +26966,7 @@ test("a.in / b", {
// A number of slash-disambiguation corner cases
test("return {} / 2", {}, {allowReturnOutsideFunction: true});
test("return\n{}\n/foo/", {}, {allowReturnOutsideFunction: true});
test("function f() {super.foo()}", {}, {allowSuperOutsideMethod: true, allowReserved: true});
test("+{} / 2", {});
test("{}\n/foo/", {});
test("x++\n{}\n/foo/", {});
Expand Down

0 comments on commit 3ee1b45

Please sign in to comment.