Skip to content
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
16 changes: 15 additions & 1 deletion src/parser/expr.js
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,15 @@ module.exports = {
return result(newExp, args);
}
const attrs = this.read_attr_list();
const isReadonly = this.token === this.tok.T_READ_ONLY;
if (isReadonly) {
if (this.version < 803) {
this.raiseError(
"Anonymous readonly classes are not allowed before PHP 8.3",
);
}
this.next();
}
if (this.token === this.tok.T_CLASS) {
const what = this.node("class");
// Annonymous class declaration
Expand All @@ -775,7 +784,12 @@ module.exports = {
if (this.expect("{")) {
body = this.next().read_class_body(true, false);
}
const whatNode = what(null, propExtends, propImplements, body, [0, 0, 0]);
const whatNode = what(null, propExtends, propImplements, body, [
0,
0,
0,
isReadonly ? 1 : 0,
]);
whatNode.attrGroups = attrs;
return result(whatNode, args);
}
Expand Down
136 changes: 135 additions & 1 deletion test/snapshot/__snapshots__/new.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing

exports[`new #348 - byref usage deprecated 1`] = `
Program {
Expand Down Expand Up @@ -226,6 +226,140 @@ Program {
}
`;

exports[`new anonymous readonly 1`] = `
Program {
"children": [
ExpressionStatement {
"expression": New {
"arguments": [],
"kind": "new",
"what": Class {
"attrGroups": [],
"body": [],
"extends": null,
"implements": null,
"isAbstract": false,
"isAnonymous": true,
"isFinal": false,
"isReadonly": true,
"kind": "class",
"name": null,
},
},
"kind": "expressionstatement",
},
],
"errors": [],
"kind": "program",
}
`;

exports[`new anonymous readonly no parens 1`] = `
Program {
"children": [
ExpressionStatement {
"expression": New {
"arguments": [],
"kind": "new",
"what": Class {
"attrGroups": [],
"body": [],
"extends": null,
"implements": null,
"isAbstract": false,
"isAnonymous": true,
"isFinal": false,
"isReadonly": true,
"kind": "class",
"name": null,
},
},
"kind": "expressionstatement",
},
],
"errors": [],
"kind": "program",
}
`;

exports[`new anonymous readonly with argument 1`] = `
Program {
"children": [
ExpressionStatement {
"expression": New {
"arguments": [
Variable {
"curly": false,
"kind": "variable",
"name": "var",
},
],
"kind": "new",
"what": Class {
"attrGroups": [],
"body": [],
"extends": null,
"implements": null,
"isAbstract": false,
"isAnonymous": true,
"isFinal": false,
"isReadonly": true,
"kind": "class",
"name": null,
},
},
"kind": "expressionstatement",
},
],
"errors": [],
"kind": "program",
}
`;

exports[`new anonymous readonly with multiple argument 1`] = `
Program {
"children": [
ExpressionStatement {
"expression": New {
"arguments": [
Variable {
"curly": false,
"kind": "variable",
"name": "one",
},
Variable {
"curly": false,
"kind": "variable",
"name": "two",
},
Variable {
"curly": false,
"kind": "variable",
"name": "three",
},
],
"kind": "new",
"what": Class {
"attrGroups": [],
"body": [],
"extends": null,
"implements": null,
"isAbstract": false,
"isAnonymous": true,
"isFinal": false,
"isReadonly": true,
"kind": "class",
"name": null,
},
},
"kind": "expressionstatement",
},
],
"errors": [],
"kind": "program",
}
`;

exports[`new anonymous with argument 1`] = `
Program {
"children": [
Expand Down
23 changes: 23 additions & 0 deletions test/snapshot/new.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,29 @@ describe("new", function () {
parser.parseEval("new class($one, $two, $three) {};"),
).toMatchSnapshot();
});
it("anonymous readonly", function () {
expect(parser.parseEval("new readonly class() {};")).toMatchSnapshot();
});
it("anonymous readonly no parens", function () {
expect(parser.parseEval("new readonly class {};")).toMatchSnapshot();
});
it("anonymous readonly with argument", function () {
expect(parser.parseEval("new readonly class($var) {};")).toMatchSnapshot();
});
it("anonymous readonly with multiple argument", function () {
expect(
parser.parseEval("new readonly class($one, $two, $three) {};"),
).toMatchSnapshot();
});
it("anonymous readonly class throws errors in PHP < 8.3", () => {
expect(() =>
parser.parseEval("new readonly class() {};", {
parser: {
version: "8.2",
},
}),
).toThrow("Anonymous readonly classes are not allowed before PHP 8.3");
});
it("static array", () => {
expect(
parser.parseEval("return new self::$mapping[$map]();"),
Expand Down