Skip to content

Commit

Permalink
Addes support for union types as part of classes
Browse files Browse the repository at this point in the history
  • Loading branch information
cseufert committed Mar 22, 2021
1 parent 968da4e commit 5aed303
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/parser/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ module.exports = {
nullable = true;
this.next();
}
let type = this.read_type();
let type = this.read_types();
if (nullable && !type) {
this.raiseError(
"Expecting a type definition combined with nullable operator"
Expand Down
124 changes: 119 additions & 5 deletions test/snapshot/__snapshots__/class.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -445,11 +445,13 @@ Program {
"name": "prop",
},
"nullable": true,
"type": TypeReference {
"kind": "typereference",
"name": "int",
"raw": "int",
},
"type": Array [
TypeReference {
"kind": "typereference",
"name": "int",
"raw": "int",
},
],
"value": NullKeyword {
"kind": "nullkeyword",
"raw": "null",
Expand Down Expand Up @@ -504,6 +506,118 @@ Program {
}
`;

exports[`Test classes Test class union properties 1`] = `
Program {
"children": Array [
Class {
"body": Array [
PropertyStatement {
"isStatic": true,
"kind": "propertystatement",
"properties": Array [
Property {
"kind": "property",
"name": Identifier {
"kind": "identifier",
"name": "foo",
},
"nullable": false,
"type": Array [
TypeReference {
"kind": "typereference",
"name": "int",
"raw": "int",
},
TypeReference {
"kind": "typereference",
"name": "float",
"raw": "float",
},
],
"value": null,
},
],
"visibility": "",
},
PropertyStatement {
"isStatic": false,
"kind": "propertystatement",
"properties": Array [
Property {
"kind": "property",
"name": Identifier {
"kind": "identifier",
"name": "bar",
},
"nullable": true,
"type": Array [
Name {
"kind": "name",
"name": "Foo",
"resolution": "uqn",
},
Name {
"kind": "name",
"name": "Bar",
"resolution": "uqn",
},
],
"value": null,
},
],
"visibility": "private",
},
PropertyStatement {
"isStatic": false,
"kind": "propertystatement",
"properties": Array [
Property {
"kind": "property",
"name": Identifier {
"kind": "identifier",
"name": "a",
},
"nullable": false,
"type": Array [
Name {
"kind": "name",
"name": "Repo",
"resolution": "uqn",
},
TypeReference {
"kind": "typereference",
"name": "string",
"raw": "string",
},
Name {
"kind": "name",
"name": "null",
"resolution": "uqn",
},
],
"value": null,
},
],
"visibility": "public",
},
],
"extends": null,
"implements": null,
"isAbstract": false,
"isAnonymous": false,
"isFinal": false,
"kind": "class",
"name": Identifier {
"kind": "identifier",
"name": "Test",
},
},
],
"errors": Array [],
"kind": "program",
}
`;

exports[`Test classes Test js properties 1`] = `
Program {
"children": Array [
Expand Down
24 changes: 14 additions & 10 deletions test/snapshot/__snapshots__/graceful.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -303,11 +303,13 @@ Program {
"name": "onst",
},
"nullable": false,
"type": Name {
"kind": "name",
"name": "foo",
"resolution": "uqn",
},
"type": Array [
Name {
"kind": "name",
"name": "foo",
"resolution": "uqn",
},
],
"value": null,
},
],
Expand Down Expand Up @@ -834,11 +836,13 @@ Program {
"name": "mplement",
},
"nullable": false,
"type": Name {
"kind": "name",
"name": "bar",
"resolution": "uqn",
},
"type": Array [
Name {
"kind": "name",
"name": "bar",
"resolution": "uqn",
},
],
"value": null,
},
],
Expand Down
11 changes: 11 additions & 0 deletions test/snapshot/class.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,17 @@ describe("Test classes", function () {
).toMatchSnapshot();
});

it("Test class union properties", function () {
expect(
parser.parseEval(`
class Test {
static int|float $foo;
private ?Foo|Bar $bar;
public Repo|string|null $a;
}`)
).toMatchSnapshot();
});

it("empty", function () {
expect(parser.parseEval("class Foo {}")).toMatchSnapshot();
});
Expand Down

0 comments on commit 5aed303

Please sign in to comment.