diff --git a/src/parser/expr.js b/src/parser/expr.js index 3d73a15f..fd7bf1e4 100644 --- a/src/parser/expr.js +++ b/src/parser/expr.js @@ -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 === "(") { @@ -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); } diff --git a/test/snapshot/__snapshots__/new.test.js.snap b/test/snapshot/__snapshots__/new.test.js.snap index 58d7c35f..e611538c 100644 --- a/test/snapshot/__snapshots__/new.test.js.snap +++ b/test/snapshot/__snapshots__/new.test.js.snap @@ -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 { @@ -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": [ diff --git a/test/snapshot/new.test.js b/test/snapshot/new.test.js index db340d95..6839c6cb 100644 --- a/test/snapshot/new.test.js +++ b/test/snapshot/new.test.js @@ -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]();"),