Skip to content

Commit

Permalink
Add support for public and private static class fields. [closes #801]
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalton committed May 15, 2019
1 parent 6f27fb0 commit 98b1fb7
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 55 deletions.
38 changes: 27 additions & 11 deletions src/acorn/parser/class-fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,38 @@ function init() {
return Reflect.apply(func, this, args)
}

const nextType = lookahead(this).type

if (nextType === tt.parenL ||
nextType === tt.star ||
(nextType !== tt.braceR &&
nextType !== tt.eq &&
nextType !== tt.semi &&
(this.isContextual("async") ||
this.isContextual("get") ||
this.isContextual("set") ||
this.isContextual("static")))) {
const next = lookahead(this)
const nextType = next.type

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

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

if (this.isContextual("static")) {
next.parsePropertyName(this.startNode())

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

const node = this.startNode()

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

this.parsePropertyName(node)

node.value = this.eat(tt.eq)
Expand Down
37 changes: 31 additions & 6 deletions src/parse/lookahead.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,50 @@
import { Parser } from "../acorn.js"
import Parser from "../parser.js"

import shared from "../shared.js"

function init() {
const flyweight = new Parser({
allowAwaitOutsideFunction: true,
allowReturnOutsideFunction: true,
ecmaVersion: 10
})
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

flyweight.next()

return flyweight
}

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

return lookahead
}

Expand Down
58 changes: 32 additions & 26 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,35 +43,11 @@ function init() {
])

const Parser = {
create,
createOptions,
defaultOptions,
parse(code, options) {
options = Parser.createOptions(options)

const { strict } = options
const parser = new AcornParser(options, code)

acornParserBigInt.enable(parser)
acornParserClassFields.enable(parser)
acornParserErrorMessages.enable(parser)
acornParserFirstAwaitOutsideFunction.enable(parser)
acornParserFirstReturnOutsideFunction.enable(parser)
acornParserFunctionParamsStart.enable(parser)
acornParserHTMLComment.enable(parser)
acornParserImport.enable(parser)
acornParserNumericSeparator.enable(parser)
acornParserRaw.enable(parser)
acornParserTolerance.enable(parser)
acornParserTopLevel.enable(parser)

if (strict !== void 0) {
parser.strict = !! strict

if (! parser.strict) {
parser.reservedWords = reservedWordsRegExp
}
}

const parser = Parser.create(code, options)
const result = parser.parse()

result.inModule = parser.inModule
Expand All @@ -81,6 +57,36 @@ function init() {
}
}

function create(code, options) {
options = Parser.createOptions(options)

const { strict } = options
const parser = new AcornParser(options, code)

acornParserBigInt.enable(parser)
acornParserClassFields.enable(parser)
acornParserErrorMessages.enable(parser)
acornParserFirstAwaitOutsideFunction.enable(parser)
acornParserFirstReturnOutsideFunction.enable(parser)
acornParserFunctionParamsStart.enable(parser)
acornParserHTMLComment.enable(parser)
acornParserImport.enable(parser)
acornParserNumericSeparator.enable(parser)
acornParserRaw.enable(parser)
acornParserTolerance.enable(parser)
acornParserTopLevel.enable(parser)

if (strict !== void 0) {
parser.strict = !! strict

if (! parser.strict) {
parser.reservedWords = reservedWordsRegExp
}
}

return parser
}

function createOptions(value) {
const options = defaults({}, value, Parser.defaultOptions)

Expand Down
30 changes: 18 additions & 12 deletions test/compiler-tests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -393,21 +393,27 @@ describe("compiler tests", () => {
it("should parse class fields syntax", () => {
const code = [
"export class A { a }",
'export class B { b = "b" }',
"export class B { b = 1 }",
"export class C { [c] }",
'export class D { [d] = "d" }',
"export class E { #e }",
"export class F { async }",
"export class G { get }",
"export class H { set }",
"export class I { static }",
"export class J {",
' #j= "j"',
" j() {",
" return this.#j",
"export class D { [d] = 1 }",
"export class E { static e }",
"export class F { static f = 1 }",
"export class G { static [g] }",
"export class H { static [h] = 1 }",
"export class I { #i }",
"export class J { static #j }",
"export class K { static #k = 1 }",
"export class L { async }",
"export class M { get }",
"export class N { set }",
"export class O { static }",
"export class P {",
' #p = "p"',
" p() {",
" return this.#p",
" }",
"}",
"export class K {",
"export class Q {",
" async = 1;",
" get = 1",
" set = 1",
Expand Down

0 comments on commit 98b1fb7

Please sign in to comment.