Skip to content

Commit 5aed303

Browse files
committed
Addes support for union types as part of classes
1 parent 968da4e commit 5aed303

File tree

4 files changed

+145
-16
lines changed

4 files changed

+145
-16
lines changed

src/parser/class.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ module.exports = {
309309
nullable = true;
310310
this.next();
311311
}
312-
let type = this.read_type();
312+
let type = this.read_types();
313313
if (nullable && !type) {
314314
this.raiseError(
315315
"Expecting a type definition combined with nullable operator"

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

Lines changed: 119 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -445,11 +445,13 @@ Program {
445445
"name": "prop",
446446
},
447447
"nullable": true,
448-
"type": TypeReference {
449-
"kind": "typereference",
450-
"name": "int",
451-
"raw": "int",
452-
},
448+
"type": Array [
449+
TypeReference {
450+
"kind": "typereference",
451+
"name": "int",
452+
"raw": "int",
453+
},
454+
],
453455
"value": NullKeyword {
454456
"kind": "nullkeyword",
455457
"raw": "null",
@@ -504,6 +506,118 @@ Program {
504506
}
505507
`;
506508

509+
exports[`Test classes Test class union properties 1`] = `
510+
Program {
511+
"children": Array [
512+
Class {
513+
"body": Array [
514+
PropertyStatement {
515+
"isStatic": true,
516+
"kind": "propertystatement",
517+
"properties": Array [
518+
Property {
519+
"kind": "property",
520+
"name": Identifier {
521+
"kind": "identifier",
522+
"name": "foo",
523+
},
524+
"nullable": false,
525+
"type": Array [
526+
TypeReference {
527+
"kind": "typereference",
528+
"name": "int",
529+
"raw": "int",
530+
},
531+
TypeReference {
532+
"kind": "typereference",
533+
"name": "float",
534+
"raw": "float",
535+
},
536+
],
537+
"value": null,
538+
},
539+
],
540+
"visibility": "",
541+
},
542+
PropertyStatement {
543+
"isStatic": false,
544+
"kind": "propertystatement",
545+
"properties": Array [
546+
Property {
547+
"kind": "property",
548+
"name": Identifier {
549+
"kind": "identifier",
550+
"name": "bar",
551+
},
552+
"nullable": true,
553+
"type": Array [
554+
Name {
555+
"kind": "name",
556+
"name": "Foo",
557+
"resolution": "uqn",
558+
},
559+
Name {
560+
"kind": "name",
561+
"name": "Bar",
562+
"resolution": "uqn",
563+
},
564+
],
565+
"value": null,
566+
},
567+
],
568+
"visibility": "private",
569+
},
570+
PropertyStatement {
571+
"isStatic": false,
572+
"kind": "propertystatement",
573+
"properties": Array [
574+
Property {
575+
"kind": "property",
576+
"name": Identifier {
577+
"kind": "identifier",
578+
"name": "a",
579+
},
580+
"nullable": false,
581+
"type": Array [
582+
Name {
583+
"kind": "name",
584+
"name": "Repo",
585+
"resolution": "uqn",
586+
},
587+
TypeReference {
588+
"kind": "typereference",
589+
"name": "string",
590+
"raw": "string",
591+
},
592+
Name {
593+
"kind": "name",
594+
"name": "null",
595+
"resolution": "uqn",
596+
},
597+
],
598+
"value": null,
599+
},
600+
],
601+
"visibility": "public",
602+
},
603+
],
604+
"extends": null,
605+
"implements": null,
606+
"isAbstract": false,
607+
"isAnonymous": false,
608+
"isFinal": false,
609+
"kind": "class",
610+
"name": Identifier {
611+
"kind": "identifier",
612+
"name": "Test",
613+
},
614+
},
615+
],
616+
"errors": Array [],
617+
"kind": "program",
618+
}
619+
`;
620+
507621
exports[`Test classes Test js properties 1`] = `
508622
Program {
509623
"children": Array [

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -303,11 +303,13 @@ Program {
303303
"name": "onst",
304304
},
305305
"nullable": false,
306-
"type": Name {
307-
"kind": "name",
308-
"name": "foo",
309-
"resolution": "uqn",
310-
},
306+
"type": Array [
307+
Name {
308+
"kind": "name",
309+
"name": "foo",
310+
"resolution": "uqn",
311+
},
312+
],
311313
"value": null,
312314
},
313315
],
@@ -834,11 +836,13 @@ Program {
834836
"name": "mplement",
835837
},
836838
"nullable": false,
837-
"type": Name {
838-
"kind": "name",
839-
"name": "bar",
840-
"resolution": "uqn",
841-
},
839+
"type": Array [
840+
Name {
841+
"kind": "name",
842+
"name": "bar",
843+
"resolution": "uqn",
844+
},
845+
],
842846
"value": null,
843847
},
844848
],

test/snapshot/class.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,17 @@ describe("Test classes", function () {
129129
).toMatchSnapshot();
130130
});
131131

132+
it("Test class union properties", function () {
133+
expect(
134+
parser.parseEval(`
135+
class Test {
136+
static int|float $foo;
137+
private ?Foo|Bar $bar;
138+
public Repo|string|null $a;
139+
}`)
140+
).toMatchSnapshot();
141+
});
142+
132143
it("empty", function () {
133144
expect(parser.parseEval("class Foo {}")).toMatchSnapshot();
134145
});

0 commit comments

Comments
 (0)