Skip to content

Commit 7400599

Browse files
committed
made a new node ast node for union types
1 parent 5aed303 commit 7400599

13 files changed

+788
-756
lines changed

src/ast.js

+1
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,7 @@ AST.prototype.checkNodes = function () {
555555
require("./ast/try"),
556556
require("./ast/typereference"),
557557
require("./ast/unary"),
558+
require("./ast/uniontype"),
558559
require("./ast/unset"),
559560
require("./ast/usegroup"),
560561
require("./ast/useitem"),

src/ast/uniontype.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Copyright (C) 2018 Glayzzle (BSD3 License)
3+
* @authors https://github.com/glayzzle/php-parser/graphs/contributors
4+
* @url http://glayzzle.com
5+
*/
6+
"use strict";
7+
8+
const Declaration = require("./declaration");
9+
const KIND = "uniontype";
10+
11+
/**
12+
* An interface definition
13+
* @constructor Interface
14+
* @extends {Declaration}
15+
* @property {Identifier[]} extends
16+
* @property {Declaration[]} body
17+
*/
18+
module.exports = Declaration.extends(KIND, function UnionType(
19+
types,
20+
docs,
21+
location
22+
) {
23+
Declaration.apply(this, [KIND, types, docs, location]);
24+
this.types = types;
25+
});

src/parser/function.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ module.exports = {
229229
},
230230
read_types() {
231231
const types = [];
232+
const unionType = this.node("uniontype");
232233
let type = this.read_type();
233234
if (!type) return null;
234235
types.push(type);
@@ -237,7 +238,11 @@ module.exports = {
237238
type = this.read_type();
238239
types.push(type);
239240
}
240-
return types.length === 0 ? null : types;
241+
if (types.length === 1) {
242+
return types[0];
243+
} else {
244+
return unionType(types);
245+
}
241246
},
242247
/**
243248
* Reads a list of arguments

test/snapshot/__snapshots__/acid.test.js.snap

+128-144
Original file line numberDiff line numberDiff line change
@@ -1739,26 +1739,24 @@ Program {
17391739
"name": "Am_I_Uggly",
17401740
},
17411741
"nullable": false,
1742-
"type": Array [
1743-
TypeReference {
1744-
"kind": "typereference",
1745-
"loc": Location {
1746-
"end": Position {
1747-
"column": 39,
1748-
"line": 50,
1749-
"offset": 1014,
1750-
},
1751-
"source": "bool",
1752-
"start": Position {
1753-
"column": 35,
1754-
"line": 50,
1755-
"offset": 1010,
1756-
},
1742+
"type": TypeReference {
1743+
"kind": "typereference",
1744+
"loc": Location {
1745+
"end": Position {
1746+
"column": 39,
1747+
"line": 50,
1748+
"offset": 1014,
1749+
},
1750+
"source": "bool",
1751+
"start": Position {
1752+
"column": 35,
1753+
"line": 50,
1754+
"offset": 1010,
17571755
},
1758-
"name": "bool",
1759-
"raw": "bool",
17601756
},
1761-
],
1757+
"name": "bool",
1758+
"raw": "bool",
1759+
},
17621760
"visibility": "public",
17631761
},
17641762
Method {
@@ -1800,26 +1798,24 @@ Program {
18001798
"name": "broken",
18011799
},
18021800
"nullable": false,
1803-
"type": Array [
1804-
TypeReference {
1805-
"kind": "typereference",
1806-
"loc": Location {
1807-
"end": Position {
1808-
"column": 38,
1809-
"line": 51,
1810-
"offset": 1054,
1811-
},
1812-
"source": "bool",
1813-
"start": Position {
1814-
"column": 34,
1815-
"line": 51,
1816-
"offset": 1050,
1817-
},
1801+
"type": TypeReference {
1802+
"kind": "typereference",
1803+
"loc": Location {
1804+
"end": Position {
1805+
"column": 38,
1806+
"line": 51,
1807+
"offset": 1054,
1808+
},
1809+
"source": "bool",
1810+
"start": Position {
1811+
"column": 34,
1812+
"line": 51,
1813+
"offset": 1050,
18181814
},
1819-
"name": "bool",
1820-
"raw": "bool",
18211815
},
1822-
],
1816+
"name": "bool",
1817+
"raw": "bool",
1818+
},
18231819
"visibility": "protected",
18241820
},
18251821
Method {
@@ -1861,26 +1857,24 @@ Program {
18611857
"name": "isWhiteSnowAlive",
18621858
},
18631859
"nullable": false,
1864-
"type": Array [
1865-
TypeReference {
1866-
"kind": "typereference",
1867-
"loc": Location {
1868-
"end": Position {
1869-
"column": 55,
1870-
"line": 52,
1871-
"offset": 1111,
1872-
},
1873-
"source": "bool",
1874-
"start": Position {
1875-
"column": 51,
1876-
"line": 52,
1877-
"offset": 1107,
1878-
},
1860+
"type": TypeReference {
1861+
"kind": "typereference",
1862+
"loc": Location {
1863+
"end": Position {
1864+
"column": 55,
1865+
"line": 52,
1866+
"offset": 1111,
1867+
},
1868+
"source": "bool",
1869+
"start": Position {
1870+
"column": 51,
1871+
"line": 52,
1872+
"offset": 1107,
18791873
},
1880-
"name": "bool",
1881-
"raw": "bool",
18821874
},
1883-
],
1875+
"name": "bool",
1876+
"raw": "bool",
1877+
},
18841878
"visibility": "protected",
18851879
},
18861880
],
@@ -2191,26 +2185,24 @@ Program {
21912185
"name": "arrow",
21922186
},
21932187
"nullable": false,
2194-
"type": Array [
2195-
TypeReference {
2196-
"kind": "typereference",
2197-
"loc": Location {
2198-
"end": Position {
2199-
"column": 29,
2200-
"line": 61,
2201-
"offset": 1238,
2202-
},
2203-
"source": "bool",
2204-
"start": Position {
2205-
"column": 25,
2206-
"line": 61,
2207-
"offset": 1234,
2208-
},
2188+
"type": TypeReference {
2189+
"kind": "typereference",
2190+
"loc": Location {
2191+
"end": Position {
2192+
"column": 29,
2193+
"line": 61,
2194+
"offset": 1238,
2195+
},
2196+
"source": "bool",
2197+
"start": Position {
2198+
"column": 25,
2199+
"line": 61,
2200+
"offset": 1234,
22092201
},
2210-
"name": "bool",
2211-
"raw": "bool",
22122202
},
2213-
],
2203+
"name": "bool",
2204+
"raw": "bool",
2205+
},
22142206
"value": Boolean {
22152207
"kind": "boolean",
22162208
"loc": Location {
@@ -2850,26 +2842,24 @@ Program {
28502842
"name": "draw",
28512843
},
28522844
"nullable": false,
2853-
"type": Array [
2854-
TypeReference {
2855-
"kind": "typereference",
2856-
"loc": Location {
2857-
"end": Position {
2858-
"column": 54,
2859-
"line": 61,
2860-
"offset": 1263,
2861-
},
2862-
"source": "string",
2863-
"start": Position {
2864-
"column": 48,
2865-
"line": 61,
2866-
"offset": 1257,
2867-
},
2845+
"type": TypeReference {
2846+
"kind": "typereference",
2847+
"loc": Location {
2848+
"end": Position {
2849+
"column": 54,
2850+
"line": 61,
2851+
"offset": 1263,
2852+
},
2853+
"source": "string",
2854+
"start": Position {
2855+
"column": 48,
2856+
"line": 61,
2857+
"offset": 1257,
28682858
},
2869-
"name": "string",
2870-
"raw": "string",
28712859
},
2872-
],
2860+
"name": "string",
2861+
"raw": "string",
2862+
},
28732863
"visibility": "public",
28742864
},
28752865
Method {
@@ -4023,26 +4013,24 @@ next:
40234013
"name": "sparta",
40244014
},
40254015
"nullable": true,
4026-
"type": Array [
4027-
TypeReference {
4028-
"kind": "typereference",
4029-
"loc": Location {
4030-
"end": Position {
4031-
"column": 26,
4032-
"line": 79,
4033-
"offset": 1617,
4034-
},
4035-
"source": "int",
4036-
"start": Position {
4037-
"column": 23,
4038-
"line": 79,
4039-
"offset": 1614,
4040-
},
4016+
"type": TypeReference {
4017+
"kind": "typereference",
4018+
"loc": Location {
4019+
"end": Position {
4020+
"column": 26,
4021+
"line": 79,
4022+
"offset": 1617,
4023+
},
4024+
"source": "int",
4025+
"start": Position {
4026+
"column": 23,
4027+
"line": 79,
4028+
"offset": 1614,
40414029
},
4042-
"name": "int",
4043-
"raw": "int",
40444030
},
4045-
],
4031+
"name": "int",
4032+
"raw": "int",
4033+
},
40464034
},
40474035
ExpressionStatement {
40484036
"expression": Assign {
@@ -4151,26 +4139,24 @@ next:
41514139
"name": "bar",
41524140
},
41534141
"nullable": true,
4154-
"type": Array [
4155-
TypeReference {
4156-
"kind": "typereference",
4157-
"loc": Location {
4158-
"end": Position {
4159-
"column": 22,
4160-
"line": 95,
4161-
"offset": 1897,
4162-
},
4163-
"source": "int",
4164-
"start": Position {
4165-
"column": 19,
4166-
"line": 95,
4167-
"offset": 1894,
4168-
},
4142+
"type": TypeReference {
4143+
"kind": "typereference",
4144+
"loc": Location {
4145+
"end": Position {
4146+
"column": 22,
4147+
"line": 95,
4148+
"offset": 1897,
4149+
},
4150+
"source": "int",
4151+
"start": Position {
4152+
"column": 19,
4153+
"line": 95,
4154+
"offset": 1894,
41694155
},
4170-
"name": "int",
4171-
"raw": "int",
41724156
},
4173-
],
4157+
"name": "int",
4158+
"raw": "int",
4159+
},
41744160
"value": Number {
41754161
"kind": "number",
41764162
"loc": Location {
@@ -6715,26 +6701,24 @@ next:
67156701
},
67166702
},
67176703
"nullable": false,
6718-
"type": Array [
6719-
TypeReference {
6720-
"kind": "typereference",
6721-
"loc": Location {
6722-
"end": Position {
6723-
"column": 56,
6724-
"line": 95,
6725-
"offset": 1931,
6726-
},
6727-
"source": "bool",
6728-
"start": Position {
6729-
"column": 52,
6730-
"line": 95,
6731-
"offset": 1927,
6732-
},
6704+
"type": TypeReference {
6705+
"kind": "typereference",
6706+
"loc": Location {
6707+
"end": Position {
6708+
"column": 56,
6709+
"line": 95,
6710+
"offset": 1931,
6711+
},
6712+
"source": "bool",
6713+
"start": Position {
6714+
"column": 52,
6715+
"line": 95,
6716+
"offset": 1927,
67336717
},
6734-
"name": "bool",
6735-
"raw": "bool",
67366718
},
6737-
],
6719+
"name": "bool",
6720+
"raw": "bool",
6721+
},
67386722
"uses": Array [
67396723
Variable {
67406724
"curly": false,

0 commit comments

Comments
 (0)