Skip to content

Commit

Permalink
PHP 8.3 parser support untyped Class constant
Browse files Browse the repository at this point in the history
  • Loading branch information
genintho committed Dec 4, 2024
1 parent d9cb05e commit e8fdcdb
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 3 deletions.
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

0 comments on commit e8fdcdb

Please sign in to comment.