Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Commit

Permalink
Fix regression in computed methods. [closes #807]
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalton committed May 17, 2019
1 parent e173189 commit ab2230e
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 59 deletions.
47 changes: 27 additions & 20 deletions src/acorn/parser/class-fields.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// A loose implementation of class fields syntax.
// https://github.com/tc39/proposal-class-fields
// https://github.com/tc39/proposal-static-class-features

import CHAR_CODE from "../../constant/char-code.js"

import lookahead from "../../parse/lookahead.js"
import branch from "../../parse/branch.js"
import shared from "../../shared.js"
import { tokTypes as tt } from "../../acorn.js"
import wrap from "../../util/wrap.js"
Expand Down Expand Up @@ -42,41 +43,47 @@ function init() {
return Reflect.apply(func, this, args)
}

const next = lookahead(this)
const nextType = next.type
const branched1 = branch(this)
const dummyNode = this.startNode()

if (nextType === tt.parenL) {
branched1.parsePropertyName(dummyNode)

const branched1Type = branched1.type

if (branched1Type === tt.parenL) {
return Reflect.apply(func, this, args)
}

if (nextType !== tt.braceR &&
nextType !== tt.eq &&
nextType !== tt.semi) {
if (branched1Type !== tt.braceR &&
branched1Type !== tt.eq &&
branched1Type !== tt.semi) {
if (this.isContextual("async") ||
this.isContextual("get") ||
this.isContextual("set")) {
return Reflect.apply(func, this, args)
}

if (this.isContextual("static")) {
if (nextType === tt.star) {
if (branched1Type === tt.star) {
return Reflect.apply(func, this, args)
}

const nextNextType = lookahead(next).type
const branched2 = branch(branched1)

branched2.parsePropertyName(dummyNode)

if (nextNextType !== tt.braceR &&
nextNextType !== tt.eq &&
nextNextType !== tt.semi &&
(next.isContextual("async") ||
next.isContextual("get") ||
next.isContextual("set"))) {
const branched2Type = branched2.type

if (branched2Type === tt.parenL) {
return Reflect.apply(func, this, args)
}

next.parsePropertyName(this.startNode())

if (next.type === tt.parenL) {
if (branched2Type !== tt.braceR &&
branched2Type !== tt.eq &&
branched2Type !== tt.semi &&
(branched1.isContextual("async") ||
branched1.isContextual("get") ||
branched1.isContextual("set"))) {
return Reflect.apply(func, this, args)
}
}
Expand All @@ -85,8 +92,8 @@ function init() {
const node = this.startNode()

node.static =
nextType !== tt.braceR &&
nextType !== tt.eq &&
branched1Type !== tt.braceR &&
branched1Type !== tt.eq &&
this.eatContextual("static")

this.parsePropertyName(node)
Expand Down
51 changes: 51 additions & 0 deletions src/parse/branch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import Parser from "../parser.js"

import shared from "../shared.js"

function init() {
let flyweight

function branch(parser) {
if (flyweight === void 0 ||
flyweight === parser) {
flyweight = createFlyweight()
}

flyweight.awaitIdentPos = parser.awaitIdentPos
flyweight.awaitPos = parser.awaitPos
flyweight.containsEsc = parser.containsEsc
flyweight.curLine = parser.curLine
flyweight.end = parser.end
flyweight.exprAllowed = parser.exprAllowed
flyweight.inModule = parser.inModule
flyweight.input = parser.input
flyweight.inTemplateElement = parser.inTemplateElement
flyweight.lastTokEnd = parser.lastTokEnd
flyweight.lastTokStart = parser.lastTokStart
flyweight.lineStart = parser.lineStart
flyweight.pos = parser.pos
flyweight.potentialArrowAt = parser.potentialArrowAt
flyweight.sourceFile = parser.sourceFile
flyweight.start = parser.start
flyweight.strict = parser.strict
flyweight.type = parser.type
flyweight.value = parser.value
flyweight.yieldPos = parser.yieldPos

return flyweight
}

function createFlyweight() {
return Parser.create("", {
allowAwaitOutsideFunction: true,
allowReturnOutsideFunction: true,
ecmaVersion: 10
})
}

return branch
}

export default shared.inited
? shared.module.parseBranch
: shared.module.parseBranch = init()
43 changes: 4 additions & 39 deletions src/parse/lookahead.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,13 @@
import Parser from "../parser.js"

import branch from "./branch.js"
import shared from "../shared.js"

function init() {
let flyweight

function lookahead(parser) {
if (flyweight === void 0 ||
flyweight === parser) {
flyweight = createFlyweight()
}

flyweight.awaitIdentPos = parser.awaitIdentPos
flyweight.awaitPos = parser.awaitPos
flyweight.containsEsc = parser.containsEsc
flyweight.curLine = parser.curLine
flyweight.end = parser.end
flyweight.exprAllowed = parser.exprAllowed
flyweight.inModule = parser.inModule
flyweight.input = parser.input
flyweight.inTemplateElement = parser.inTemplateElement
flyweight.lastTokEnd = parser.lastTokEnd
flyweight.lastTokStart = parser.lastTokStart
flyweight.lineStart = parser.lineStart
flyweight.pos = parser.pos
flyweight.potentialArrowAt = parser.potentialArrowAt
flyweight.sourceFile = parser.sourceFile
flyweight.start = parser.start
flyweight.strict = parser.strict
flyweight.type = parser.type
flyweight.value = parser.value
flyweight.yieldPos = parser.yieldPos
const branched = branch(parser)

flyweight.next()

return flyweight
}
branched.next()

function createFlyweight() {
return Parser.create("", {
allowAwaitOutsideFunction: true,
allowReturnOutsideFunction: true,
ecmaVersion: 10
})
return branched
}

return lookahead
Expand Down
1 change: 1 addition & 0 deletions test/compiler-tests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ describe("compiler tests", () => {
" static async g() {}",
" static *h() {}",
" static async *i() {}",
" [Symbol.iterator]() {}",
" async = 1;",
" get = 1",
" set = 1",
Expand Down

0 comments on commit ab2230e

Please sign in to comment.