Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/parser/expr.js
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,11 @@ module.exports = {
return result(newExp, args);
}
const attrs = this.read_attr_list();
if (this.token === this.tok.T_CLASS) {
const isReadonly = this.token === this.tok.T_READ_ONLY;
if (
this.token === this.tok.T_CLASS ||
(isReadonly && this.next().token === this.tok.T_CLASS)
) {
const what = this.node("class");
// Annonymous class declaration
if (this.next().token === "(") {
Expand All @@ -775,7 +779,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
14 changes: 14 additions & 0 deletions test/snapshot/new.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@ 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("static array", () => {
expect(
parser.parseEval("return new self::$mapping[$map]();"),
Expand Down