Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix scope-related regressions and await vs. class property initializers issue #1339

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion acorn/src/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -1043,7 +1043,7 @@ pp.checkUnreserved = function({start, end, name}) {
this.raiseRecoverable(start, "Cannot use 'yield' as identifier inside a generator")
if (this.inAsync && name === "await")
this.raiseRecoverable(start, "Cannot use 'await' as identifier inside an async function")
if (this.currentScope().inClassFieldInit && name === "arguments")
if (this.currentThisScope().inClassFieldInit && name === "arguments")
this.raiseRecoverable(start, "Cannot use 'arguments' in class field initializer")
if (this.inClassStaticBlock && (name === "arguments" || name === "await"))
this.raise(start, `Cannot use ${name} in class static initialization block`)
Expand Down
20 changes: 11 additions & 9 deletions acorn/src/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,19 @@ export class Parser {

get inFunction() { return (this.currentVarScope().flags & SCOPE_FUNCTION) > 0 }

get inGenerator() { return (this.currentVarScope().flags & SCOPE_GENERATOR) > 0 && !this.currentScope().inClassFieldInit }
get inGenerator() { return (this.currentVarScope().flags & SCOPE_GENERATOR) > 0 && !this.currentVarScope().inClassFieldInit }

get inAsync() { return (this.currentScope().flags & SCOPE_ASYNC) > 0 && !this.currentScope().inClassFieldInit }
get inAsync() { return (this.currentVarScope().flags & SCOPE_ASYNC) > 0 && !this.currentVarScope().inClassFieldInit }

get canAwait() {
for (let i = this.scopeStack.length - 1; i >= 0; i--) {
let scope = this.scopeStack[i]
if (scope.flags & SCOPE_CLASS_STATIC_BLOCK || scope.inClassFieldInit) return false
if (scope.flags & SCOPE_FUNCTION) return (scope.flags & SCOPE_ASYNC) > 0
const scope = this.currentVarScope()
if (scope.flags & SCOPE_FUNCTION) {
return (scope.flags & SCOPE_ASYNC) && !scope.inClassFieldInit
}
return (this.inModule && this.options.ecmaVersion >= 13) || this.options.allowAwaitOutsideFunction
if (scope.flags & SCOPE_TOP) {
return ((this.inModule && this.options.ecmaVersion >= 13) || this.options.allowAwaitOutsideFunction) && !scope.inClassFieldInit
}
return false
}

get allowSuper() {
Expand All @@ -122,8 +124,8 @@ export class Parser {
get treatFunctionsAsVar() { return this.treatFunctionsAsVarInScope(this.currentScope()) }

get allowNewDotTarget() {
const {flags} = this.currentThisScope()
return (flags & (SCOPE_FUNCTION | SCOPE_CLASS_STATIC_BLOCK)) > 0 || this.currentScope().inClassFieldInit
const {flags, inClassFieldInit} = this.currentThisScope()
return (flags & (SCOPE_FUNCTION | SCOPE_CLASS_STATIC_BLOCK)) > 0 || inClassFieldInit
}

get inClassStaticBlock() {
Expand Down
10 changes: 6 additions & 4 deletions acorn/src/statement.js
Original file line number Diff line number Diff line change
Expand Up @@ -742,11 +742,13 @@ pp.parseClassField = function(field) {

if (this.eat(tt.eq)) {
// To raise SyntaxError if 'arguments' exists in the initializer.
const scope = this.currentScope()
const inClassFieldInit = scope.inClassFieldInit
scope.inClassFieldInit = true
const thisScope = this.currentThisScope(), varScope = this.currentVarScope()
const thisScopeInClassFieldInit = thisScope.inClassFieldInit
const varScopeInClassFieldInit = varScope.inClassFieldInit
thisScope.inClassFieldInit = varScope.inClassFieldInit = true
field.value = this.parseMaybeAssign()
scope.inClassFieldInit = inClassFieldInit
thisScope.inClassFieldInit = thisScopeInClassFieldInit
varScope.inClassFieldInit = varScopeInClassFieldInit
} else {
field.value = null
}
Expand Down
Loading