Skip to content

Commit 50d3bfb

Browse files
authored
Consistent returns for read_variable_declaration (#1141)
* consistent return value for class property parsing * x * simpler if/else
1 parent d9cb05e commit 50d3bfb

File tree

3 files changed

+19
-26
lines changed

3 files changed

+19
-26
lines changed

src/parser/class.js

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -201,22 +201,15 @@ module.exports = {
201201
const name = this.text().substring(1); // ignore $
202202
this.next();
203203
propName = propName(name);
204-
if (this.token === ";" || this.token === ",") {
205-
return result(propName, null, readonly, nullable, type, attrs || []);
206-
} else if (this.token === "=") {
204+
205+
let value = null;
206+
207+
this.expect([",", ";", "="]);
208+
if (this.token === "=") {
207209
// https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L815
208-
return result(
209-
propName,
210-
this.next().read_expr(),
211-
readonly,
212-
nullable,
213-
type,
214-
attrs || [],
215-
);
216-
} else {
217-
this.expect([",", ";", "="]);
218-
return result(propName, null, nullable, type, attrs || []);
210+
value = this.next().read_expr();
219211
}
212+
return result(propName, value, readonly, nullable, type, attrs || []);
220213
},
221214
",",
222215
);

test/snapshot/__snapshots__/class.test.js.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,19 +1200,19 @@ Program {
12001200
"kind": "propertystatement",
12011201
"properties": [
12021202
Property {
1203-
"attrGroups": null,
1203+
"attrGroups": [],
12041204
"kind": "property",
12051205
"name": Identifier {
12061206
"kind": "identifier",
12071207
"name": "id",
12081208
},
1209-
"nullable": TypeReference {
1209+
"nullable": false,
1210+
"readonly": true,
1211+
"type": TypeReference {
12101212
"kind": "typereference",
12111213
"name": "int",
12121214
"raw": "int",
12131215
},
1214-
"readonly": false,
1215-
"type": [],
12161216
"value": null,
12171217
},
12181218
],

test/snapshot/__snapshots__/graceful.test.js.snap

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -300,19 +300,19 @@ Program {
300300
"kind": "propertystatement",
301301
"properties": [
302302
Property {
303-
"attrGroups": null,
303+
"attrGroups": [],
304304
"kind": "property",
305305
"name": Identifier {
306306
"kind": "identifier",
307307
"name": "onst",
308308
},
309-
"nullable": Name {
309+
"nullable": false,
310+
"readonly": false,
311+
"type": Name {
310312
"kind": "name",
311313
"name": "foo",
312314
"resolution": "uqn",
313315
},
314-
"readonly": false,
315-
"type": [],
316316
"value": null,
317317
},
318318
],
@@ -843,19 +843,19 @@ Program {
843843
"kind": "propertystatement",
844844
"properties": [
845845
Property {
846-
"attrGroups": null,
846+
"attrGroups": [],
847847
"kind": "property",
848848
"name": Identifier {
849849
"kind": "identifier",
850850
"name": "mplement",
851851
},
852-
"nullable": Name {
852+
"nullable": false,
853+
"readonly": false,
854+
"type": Name {
853855
"kind": "name",
854856
"name": "bar",
855857
"resolution": "uqn",
856858
},
857-
"readonly": false,
858-
"type": [],
859859
"value": null,
860860
},
861861
],

0 commit comments

Comments
 (0)