Skip to content

Commit

Permalink
fix new.target in class field initializer
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea authored and marijnh committed Apr 24, 2021
1 parent 44ed579 commit 2f90bf9
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
5 changes: 4 additions & 1 deletion acorn/src/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,10 @@ export class Parser {
}
get allowDirectSuper() { return (this.currentThisScope().flags & SCOPE_DIRECT_SUPER) > 0 }
get treatFunctionsAsVar() { return this.treatFunctionsAsVarInScope(this.currentScope()) }
get inNonArrowFunction() { return (this.currentThisScope().flags & SCOPE_FUNCTION) > 0 }
get inNonArrowFunction() {
const {flags, inClassFieldInit} = this.currentThisScope()
return (flags & SCOPE_FUNCTION) > 0 || inClassFieldInit
}

static extend(...plugins) {
let cls = this
Expand Down
59 changes: 59 additions & 0 deletions test/tests-class-features-2022.js
Original file line number Diff line number Diff line change
Expand Up @@ -1323,6 +1323,65 @@ test("async function f() { class C { aaa = await } }", {
// `yield` is keyword in strict mode
testFail("function* f() { class C { aaa = yield } }", "The keyword 'yield' is reserved (1:32)", {ecmaVersion: 13})

// new.target
test("class C { a = new.target }", {
"type": "Program",
"start": 0,
"end": 26,
"body": [
{
"type": "ClassDeclaration",
"start": 0,
"end": 26,
"id": {
"type": "Identifier",
"start": 6,
"end": 7,
"name": "C"
},
"superClass": null,
"body": {
"type": "ClassBody",
"start": 8,
"end": 26,
"body": [
{
"type": "PropertyDefinition",
"start": 10,
"end": 24,
"static": false,
"computed": false,
"key": {
"type": "Identifier",
"start": 10,
"end": 11,
"name": "a"
},
"value": {
"type": "MetaProperty",
"start": 14,
"end": 24,
"meta": {
"type": "Identifier",
"start": 14,
"end": 17,
"name": "new"
},
"property": {
"type": "Identifier",
"start": 18,
"end": 24,
"name": "target"
}
}
}
]
}
}
],
"sourceType": "script"
}, {ecmaVersion: 13})

// old ecma version
testFail("class C { aaa }", "Unexpected token (1:14)", {ecmaVersion: 12})

Expand Down

0 comments on commit 2f90bf9

Please sign in to comment.