From 50d3bfb4e3df3601af7e80a4f7b3e3fa3a69abb7 Mon Sep 17 00:00:00 2001 From: Thomas Genin Date: Wed, 4 Dec 2024 13:23:28 -0800 Subject: [PATCH] Consistent returns for `read_variable_declaration` (#1141) * consistent return value for class property parsing * x * simpler if/else --- src/parser/class.js | 21 +++++++------------ .../snapshot/__snapshots__/class.test.js.snap | 8 +++---- .../__snapshots__/graceful.test.js.snap | 16 +++++++------- 3 files changed, 19 insertions(+), 26 deletions(-) diff --git a/src/parser/class.js b/src/parser/class.js index 996f5acf3..ccb4e9414 100644 --- a/src/parser/class.js +++ b/src/parser/class.js @@ -201,22 +201,15 @@ module.exports = { const name = this.text().substring(1); // ignore $ this.next(); propName = propName(name); - if (this.token === ";" || this.token === ",") { - return result(propName, null, readonly, nullable, type, attrs || []); - } else if (this.token === "=") { + + let value = null; + + this.expect([",", ";", "="]); + if (this.token === "=") { // https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L815 - return result( - propName, - this.next().read_expr(), - readonly, - nullable, - type, - attrs || [], - ); - } else { - this.expect([",", ";", "="]); - return result(propName, null, nullable, type, attrs || []); + value = this.next().read_expr(); } + return result(propName, value, readonly, nullable, type, attrs || []); }, ",", ); diff --git a/test/snapshot/__snapshots__/class.test.js.snap b/test/snapshot/__snapshots__/class.test.js.snap index 6fd21a3aa..d408ec7d5 100644 --- a/test/snapshot/__snapshots__/class.test.js.snap +++ b/test/snapshot/__snapshots__/class.test.js.snap @@ -1200,19 +1200,19 @@ Program { "kind": "propertystatement", "properties": [ Property { - "attrGroups": null, + "attrGroups": [], "kind": "property", "name": Identifier { "kind": "identifier", "name": "id", }, - "nullable": TypeReference { + "nullable": false, + "readonly": true, + "type": TypeReference { "kind": "typereference", "name": "int", "raw": "int", }, - "readonly": false, - "type": [], "value": null, }, ], diff --git a/test/snapshot/__snapshots__/graceful.test.js.snap b/test/snapshot/__snapshots__/graceful.test.js.snap index cf45e3329..d1bf9b28c 100644 --- a/test/snapshot/__snapshots__/graceful.test.js.snap +++ b/test/snapshot/__snapshots__/graceful.test.js.snap @@ -300,19 +300,19 @@ Program { "kind": "propertystatement", "properties": [ Property { - "attrGroups": null, + "attrGroups": [], "kind": "property", "name": Identifier { "kind": "identifier", "name": "onst", }, - "nullable": Name { + "nullable": false, + "readonly": false, + "type": Name { "kind": "name", "name": "foo", "resolution": "uqn", }, - "readonly": false, - "type": [], "value": null, }, ], @@ -843,19 +843,19 @@ Program { "kind": "propertystatement", "properties": [ Property { - "attrGroups": null, + "attrGroups": [], "kind": "property", "name": Identifier { "kind": "identifier", "name": "mplement", }, - "nullable": Name { + "nullable": false, + "readonly": false, + "type": Name { "kind": "name", "name": "bar", "resolution": "uqn", }, - "readonly": false, - "type": [], "value": null, }, ],