Skip to content

Commit

Permalink
Consistent returns for read_variable_declaration (#1141)
Browse files Browse the repository at this point in the history
* consistent return value for class property parsing

* x

* simpler if/else
  • Loading branch information
genintho authored Dec 4, 2024
1 parent d9cb05e commit 50d3bfb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 26 deletions.
21 changes: 7 additions & 14 deletions src/parser/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 || []);
},
",",
);
Expand Down
8 changes: 4 additions & 4 deletions test/snapshot/__snapshots__/class.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
],
Expand Down
16 changes: 8 additions & 8 deletions test/snapshot/__snapshots__/graceful.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
],
Expand Down Expand Up @@ -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,
},
],
Expand Down

0 comments on commit 50d3bfb

Please sign in to comment.