Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Typed Class Constants parsing #1147

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/parser/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ module.exports = {
}

const [nullable, type] =
this.version >= 830 ? this.read_optional_type() : [false, null];
this.version >= 803 ? this.read_optional_type() : [false, null];

const result = this.node("classconstant");
const items = this.read_list(
Expand Down Expand Up @@ -364,6 +364,11 @@ module.exports = {
if (nullable) {
this.next();
}

if (this.peek() === "=") {
return [false, null];
}

let type = this.read_types();
if (nullable && !type) {
this.raiseError(
Expand Down
112 changes: 112 additions & 0 deletions test/snapshot/__snapshots__/classconstant.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,69 @@ Program {
}
`;

exports[`classconstant multiple 8.3 1`] = `
Program {
"children": [
Class {
"attrGroups": [],
"body": [
ClassConstant {
"attrGroups": [],
"constants": [
Constant {
"kind": "constant",
"name": Identifier {
"kind": "identifier",
"name": "NAME_1",
},
"value": String {
"isDoubleQuote": true,
"kind": "string",
"raw": ""Hello world!"",
"unicode": false,
"value": "Hello world!",
},
},
Constant {
"kind": "constant",
"name": Identifier {
"kind": "identifier",
"name": "NAME_2",
},
"value": String {
"isDoubleQuote": true,
"kind": "string",
"raw": ""Other hello world!"",
"unicode": false,
"value": "Other hello world!",
},
},
],
"final": false,
"kind": "classconstant",
"nullable": false,
"type": null,
"visibility": "",
},
],
"extends": null,
"implements": null,
"isAbstract": false,
"isAnonymous": false,
"isFinal": false,
"isReadonly": false,
"kind": "class",
"name": Identifier {
"kind": "identifier",
"name": "Foo",
},
},
],
"errors": [],
"kind": "program",
}
`;

exports[`classconstant private 1`] = `
Program {
"children": [
Expand Down Expand Up @@ -308,6 +371,55 @@ Program {
}
`;

exports[`classconstant simple using 8.3 1`] = `
Program {
"children": [
Class {
"attrGroups": [],
"body": [
ClassConstant {
"attrGroups": [],
"constants": [
Constant {
"kind": "constant",
"name": Identifier {
"kind": "identifier",
"name": "CONSTANT",
},
"value": String {
"isDoubleQuote": true,
"kind": "string",
"raw": ""Hello world!"",
"unicode": false,
"value": "Hello world!",
},
},
],
"final": false,
"kind": "classconstant",
"nullable": false,
"type": null,
"visibility": "",
},
],
"extends": null,
"implements": null,
"isAbstract": false,
"isAnonymous": false,
"isFinal": false,
"isReadonly": false,
"kind": "class",
"name": Identifier {
"kind": "identifier",
"name": "Foo",
},
},
],
"errors": [],
"kind": "program",
}
`;

exports[`classconstant type hinted (supported) 1`] = `
Program {
"children": [
Expand Down
24 changes: 22 additions & 2 deletions test/snapshot/classconstant.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,33 @@ describe("classconstant", () => {
parser.parseEval('class Foo { const CONSTANT = "Hello world!"; }'),
).toMatchSnapshot();
});
it("simple using 8.3", () => {
expect(
parser.parseEval(`class Foo { const CONSTANT = "Hello world!"; }`, {
parser: { version: 803 },
}),
).toMatchSnapshot();
});

it("multiple", () => {
expect(
parser.parseEval(
'class Foo { const CONSTANT = "Hello world!", OTHER_CONSTANT = "Other hello world!"; }',
),
).toMatchSnapshot();
});

it("multiple 8.3", () => {
expect(
parser.parseEval(
'class Foo { const NAME_1 = "Hello world!", NAME_2 = "Other hello world!"; }',
{
parser: { version: 803 },
},
),
).toMatchSnapshot();
});

it("public", () => {
expect(
parser.parseEval('class Foo { public const CONSTANT = "Hello world!"; }'),
Expand Down Expand Up @@ -43,15 +63,15 @@ describe("classconstant", () => {
expect(
parser.parseEval(
'class Foo { public const string CONSTANT = "Hello world!"; }',
{ parser: { version: 830 } },
{ parser: { version: 803 } },
),
).toMatchSnapshot();
});
it("type hinted (unsupported)", () => {
expect(() =>
parser.parseEval(
'class Foo { public const string CONSTANT = "Hello world!"; }',
{ parser: { version: 820 } },
{ parser: { version: 802 } },
),
).toThrowErrorMatchingSnapshot();
});
Expand Down
Loading