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

Commit dff4f0d

Browse files
committed
Add support for public and private static class fields. [closes #801]
1 parent a0a21ac commit dff4f0d

File tree

4 files changed

+108
-55
lines changed

4 files changed

+108
-55
lines changed

src/acorn/parser/class-fields.js

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,38 @@ function init() {
4242
return Reflect.apply(func, this, args)
4343
}
4444

45-
const nextType = lookahead(this).type
46-
47-
if (nextType === tt.parenL ||
48-
nextType === tt.star ||
49-
(nextType !== tt.braceR &&
50-
nextType !== tt.eq &&
51-
nextType !== tt.semi &&
52-
(this.isContextual("async") ||
53-
this.isContextual("get") ||
54-
this.isContextual("set") ||
55-
this.isContextual("static")))) {
45+
const next = lookahead(this)
46+
const nextType = next.type
47+
48+
if (nextType === tt.parenL) {
5649
return Reflect.apply(func, this, args)
5750
}
5851

52+
if (nextType !== tt.braceR &&
53+
nextType !== tt.eq &&
54+
nextType !== tt.semi) {
55+
if (this.isContextual("async") ||
56+
this.isContextual("get") ||
57+
this.isContextual("set")) {
58+
return Reflect.apply(func, this, args)
59+
}
60+
61+
if (this.isContextual("static")) {
62+
next.parsePropertyName(this.startNode())
63+
64+
if (next.type === tt.parenL) {
65+
return Reflect.apply(func, this, args)
66+
}
67+
}
68+
}
69+
5970
const node = this.startNode()
6071

72+
node.static =
73+
nextType !== tt.braceR &&
74+
nextType !== tt.eq &&
75+
this.eatContextual("static")
76+
6177
this.parsePropertyName(node)
6278

6379
node.value = this.eat(tt.eq)

src/parse/lookahead.js

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,50 @@
1-
import { Parser } from "../acorn.js"
1+
import Parser from "../parser.js"
22

33
import shared from "../shared.js"
44

55
function init() {
6-
const flyweight = new Parser({
7-
allowAwaitOutsideFunction: true,
8-
allowReturnOutsideFunction: true,
9-
ecmaVersion: 10
10-
})
6+
let flyweight
117

128
function lookahead(parser) {
9+
if (flyweight === void 0 ||
10+
flyweight === parser) {
11+
flyweight = createFlyweight()
12+
}
13+
14+
flyweight.awaitIdentPos = parser.awaitIdentPos
15+
flyweight.awaitPos = parser.awaitPos
16+
flyweight.containsEsc = parser.containsEsc
17+
flyweight.curLine = parser.curLine
18+
flyweight.end = parser.end
19+
flyweight.exprAllowed = parser.exprAllowed
1320
flyweight.inModule = parser.inModule
1421
flyweight.input = parser.input
22+
flyweight.inTemplateElement = parser.inTemplateElement
23+
flyweight.lastTokEnd = parser.lastTokEnd
24+
flyweight.lastTokStart = parser.lastTokStart
25+
flyweight.lineStart = parser.lineStart
1526
flyweight.pos = parser.pos
27+
flyweight.potentialArrowAt = parser.potentialArrowAt
28+
flyweight.sourceFile = parser.sourceFile
29+
flyweight.start = parser.start
1630
flyweight.strict = parser.strict
31+
flyweight.type = parser.type
32+
flyweight.value = parser.value
33+
flyweight.yieldPos = parser.yieldPos
1734

1835
flyweight.next()
1936

2037
return flyweight
2138
}
2239

40+
function createFlyweight() {
41+
return Parser.create("", {
42+
allowAwaitOutsideFunction: true,
43+
allowReturnOutsideFunction: true,
44+
ecmaVersion: 10
45+
})
46+
}
47+
2348
return lookahead
2449
}
2550

src/parser.js

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -43,35 +43,11 @@ function init() {
4343
])
4444

4545
const Parser = {
46+
create,
4647
createOptions,
4748
defaultOptions,
4849
parse(code, options) {
49-
options = Parser.createOptions(options)
50-
51-
const { strict } = options
52-
const parser = new AcornParser(options, code)
53-
54-
acornParserBigInt.enable(parser)
55-
acornParserClassFields.enable(parser)
56-
acornParserErrorMessages.enable(parser)
57-
acornParserFirstAwaitOutsideFunction.enable(parser)
58-
acornParserFirstReturnOutsideFunction.enable(parser)
59-
acornParserFunctionParamsStart.enable(parser)
60-
acornParserHTMLComment.enable(parser)
61-
acornParserImport.enable(parser)
62-
acornParserNumericSeparator.enable(parser)
63-
acornParserRaw.enable(parser)
64-
acornParserTolerance.enable(parser)
65-
acornParserTopLevel.enable(parser)
66-
67-
if (strict !== void 0) {
68-
parser.strict = !! strict
69-
70-
if (! parser.strict) {
71-
parser.reservedWords = reservedWordsRegExp
72-
}
73-
}
74-
50+
const parser = Parser.create(code, options)
7551
const result = parser.parse()
7652

7753
result.inModule = parser.inModule
@@ -81,6 +57,36 @@ function init() {
8157
}
8258
}
8359

60+
function create(code, options) {
61+
options = Parser.createOptions(options)
62+
63+
const { strict } = options
64+
const parser = new AcornParser(options, code)
65+
66+
acornParserBigInt.enable(parser)
67+
acornParserClassFields.enable(parser)
68+
acornParserErrorMessages.enable(parser)
69+
acornParserFirstAwaitOutsideFunction.enable(parser)
70+
acornParserFirstReturnOutsideFunction.enable(parser)
71+
acornParserFunctionParamsStart.enable(parser)
72+
acornParserHTMLComment.enable(parser)
73+
acornParserImport.enable(parser)
74+
acornParserNumericSeparator.enable(parser)
75+
acornParserRaw.enable(parser)
76+
acornParserTolerance.enable(parser)
77+
acornParserTopLevel.enable(parser)
78+
79+
if (strict !== void 0) {
80+
parser.strict = !! strict
81+
82+
if (! parser.strict) {
83+
parser.reservedWords = reservedWordsRegExp
84+
}
85+
}
86+
87+
return parser
88+
}
89+
8490
function createOptions(value) {
8591
const options = defaults({}, value, Parser.defaultOptions)
8692

test/compiler-tests.mjs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -393,21 +393,27 @@ describe("compiler tests", () => {
393393
it("should parse class fields syntax", () => {
394394
const code = [
395395
"export class A { a }",
396-
'export class B { b = "b" }',
396+
"export class B { b = 1 }",
397397
"export class C { [c] }",
398-
'export class D { [d] = "d" }',
399-
"export class E { #e }",
400-
"export class F { async }",
401-
"export class G { get }",
402-
"export class H { set }",
403-
"export class I { static }",
404-
"export class J {",
405-
' #j= "j"',
406-
" j() {",
407-
" return this.#j",
398+
"export class D { [d] = 1 }",
399+
"export class E { static e }",
400+
"export class F { static f = 1 }",
401+
"export class G { static [g] }",
402+
"export class H { static [h] = 1 }",
403+
"export class I { #i }",
404+
"export class J { static #j }",
405+
"export class K { static #k = 1 }",
406+
"export class L { async }",
407+
"export class M { get }",
408+
"export class N { set }",
409+
"export class O { static }",
410+
"export class P {",
411+
' #p = "p"',
412+
" p() {",
413+
" return this.#p",
408414
" }",
409415
"}",
410-
"export class K {",
416+
"export class Q {",
411417
" async = 1;",
412418
" get = 1",
413419
" set = 1",

0 commit comments

Comments
 (0)