diff --git a/acorn/README.md b/acorn/README.md index ecc1d9d30..f27a99444 100644 --- a/acorn/README.md +++ b/acorn/README.md @@ -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. diff --git a/acorn/dist/acorn.d.ts b/acorn/dist/acorn.d.ts index c5391e4af..8e8fbbcb4 100644 --- a/acorn/dist/acorn.d.ts +++ b/acorn/dist/acorn.d.ts @@ -20,6 +20,7 @@ declare namespace acorn { allowReturnOutsideFunction?: boolean allowImportExportEverywhere?: boolean allowAwaitOutsideFunction?: boolean + allowSuperOutsideMethod?: boolean allowHashBang?: boolean locations?: boolean onToken?: ((token: Token) => any) | Token[] diff --git a/acorn/src/options.js b/acorn/src/options.js index 787fe94bc..1cb4efbdf 100644 --- a/acorn/src/options.js +++ b/acorn/src/options.js @@ -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, diff --git a/acorn/src/state.js b/acorn/src/state.js index 70d2acec9..97c711a93 100644 --- a/acorn/src/state.js +++ b/acorn/src/state.js @@ -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()) } diff --git a/test/tests.js b/test/tests.js index ffcb276d2..8d833f462 100644 --- a/test/tests.js +++ b/test/tests.js @@ -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/", {});