Skip to content

Commit b3bb86b

Browse files
committed
hand constant with no type in 8.3
1 parent 929f8b9 commit b3bb86b

File tree

3 files changed

+150
-3
lines changed

3 files changed

+150
-3
lines changed

src/parser/class.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,18 @@ module.exports = {
234234
this.next();
235235
}
236236

237-
const [nullable, type] =
238-
this.version >= 803 ? this.read_optional_type() : [false, null];
237+
let type = null;
238+
let nullable = false;
239+
if (this.version >= 803) {
240+
[nullable, type] = this.read_optional_type();
241+
}
242+
let name = null;
243+
244+
// read_optional_type can return a "type" even if the constant does not actually have a type.
245+
if (type && type.kind === "name" && this.token === "=") {
246+
name = type.name;
247+
type = null;
248+
}
239249

240250
const result = this.node("classconstant");
241251
const items = this.read_list(
@@ -256,9 +266,14 @@ module.exports = {
256266
(this.version >= 700 && this.is("IDENTIFIER"))
257267
) {
258268
constName = this.node("identifier");
259-
const name = this.text();
269+
name = this.text();
260270
this.next();
261271
constName = constName(name);
272+
}
273+
// read_optional_type is not always returning just a type but
274+
else if (name) {
275+
constName = this.node("identifier")(name);
276+
name = null;
262277
} else {
263278
this.expect("IDENTIFIER");
264279
}

test/snapshot/__snapshots__/classconstant.test.js.snap

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,69 @@ Program {
112112
}
113113
`;
114114

115+
exports[`classconstant multiple 8.3 1`] = `
116+
Program {
117+
"children": [
118+
Class {
119+
"attrGroups": [],
120+
"body": [
121+
ClassConstant {
122+
"attrGroups": [],
123+
"constants": [
124+
Constant {
125+
"kind": "constant",
126+
"name": Identifier {
127+
"kind": "identifier",
128+
"name": "NAME_1",
129+
},
130+
"value": String {
131+
"isDoubleQuote": true,
132+
"kind": "string",
133+
"raw": ""Hello world!"",
134+
"unicode": false,
135+
"value": "Hello world!",
136+
},
137+
},
138+
Constant {
139+
"kind": "constant",
140+
"name": Identifier {
141+
"kind": "identifier",
142+
"name": "NAME_2",
143+
},
144+
"value": String {
145+
"isDoubleQuote": true,
146+
"kind": "string",
147+
"raw": ""Other hello world!"",
148+
"unicode": false,
149+
"value": "Other hello world!",
150+
},
151+
},
152+
],
153+
"final": false,
154+
"kind": "classconstant",
155+
"nullable": false,
156+
"type": null,
157+
"visibility": "",
158+
},
159+
],
160+
"extends": null,
161+
"implements": null,
162+
"isAbstract": false,
163+
"isAnonymous": false,
164+
"isFinal": false,
165+
"isReadonly": false,
166+
"kind": "class",
167+
"name": Identifier {
168+
"kind": "identifier",
169+
"name": "Foo",
170+
},
171+
},
172+
],
173+
"errors": [],
174+
"kind": "program",
175+
}
176+
`;
177+
115178
exports[`classconstant private 1`] = `
116179
Program {
117180
"children": [
@@ -308,6 +371,55 @@ Program {
308371
}
309372
`;
310373

374+
exports[`classconstant simple using 8.3 1`] = `
375+
Program {
376+
"children": [
377+
Class {
378+
"attrGroups": [],
379+
"body": [
380+
ClassConstant {
381+
"attrGroups": [],
382+
"constants": [
383+
Constant {
384+
"kind": "constant",
385+
"name": Identifier {
386+
"kind": "identifier",
387+
"name": "CONSTANT",
388+
},
389+
"value": String {
390+
"isDoubleQuote": true,
391+
"kind": "string",
392+
"raw": ""Hello world!"",
393+
"unicode": false,
394+
"value": "Hello world!",
395+
},
396+
},
397+
],
398+
"final": false,
399+
"kind": "classconstant",
400+
"nullable": false,
401+
"type": null,
402+
"visibility": "",
403+
},
404+
],
405+
"extends": null,
406+
"implements": null,
407+
"isAbstract": false,
408+
"isAnonymous": false,
409+
"isFinal": false,
410+
"isReadonly": false,
411+
"kind": "class",
412+
"name": Identifier {
413+
"kind": "identifier",
414+
"name": "Foo",
415+
},
416+
},
417+
],
418+
"errors": [],
419+
"kind": "program",
420+
}
421+
`;
422+
311423
exports[`classconstant type hinted (supported) 1`] = `
312424
Program {
313425
"children": [

test/snapshot/classconstant.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,33 @@ describe("classconstant", () => {
66
parser.parseEval('class Foo { const CONSTANT = "Hello world!"; }'),
77
).toMatchSnapshot();
88
});
9+
it("simple using 8.3", () => {
10+
expect(
11+
parser.parseEval(`class Foo { const CONSTANT = "Hello world!"; }`, {
12+
parser: { version: 803 },
13+
}),
14+
).toMatchSnapshot();
15+
});
16+
917
it("multiple", () => {
1018
expect(
1119
parser.parseEval(
1220
'class Foo { const CONSTANT = "Hello world!", OTHER_CONSTANT = "Other hello world!"; }',
1321
),
1422
).toMatchSnapshot();
1523
});
24+
25+
it("multiple 8.3", () => {
26+
expect(
27+
parser.parseEval(
28+
'class Foo { const NAME_1 = "Hello world!", NAME_2 = "Other hello world!"; }',
29+
{
30+
parser: { version: 803 },
31+
},
32+
),
33+
).toMatchSnapshot();
34+
});
35+
1636
it("public", () => {
1737
expect(
1838
parser.parseEval('class Foo { public const CONSTANT = "Hello world!"; }'),

0 commit comments

Comments
 (0)