Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,7 @@ AST.prototype.checkNodes = function () {
require("./ast/try"),
require("./ast/typereference"),
require("./ast/unary"),
require("./ast/uniontype"),
require("./ast/unset"),
require("./ast/usegroup"),
require("./ast/useitem"),
Expand Down
24 changes: 24 additions & 0 deletions src/ast/uniontype.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Copyright (C) 2018 Glayzzle (BSD3 License)
* @authors https://github.com/glayzzle/php-parser/graphs/contributors
* @url http://glayzzle.com
*/
"use strict";

const Declaration = require("./declaration");
const KIND = "uniontype";

/**
* A union of types
* @constructor UnionType
* @extends {Declaration}
* @property {TypeReference[]} types
*/
module.exports = Declaration.extends(KIND, function UnionType(
types,
docs,
location
) {
Declaration.apply(this, [KIND, null, docs, location]);
this.types = types;
});
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
2 changes: 1 addition & 1 deletion src/parser/expr.js
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ module.exports = {
nullable = true;
this.next();
}
returnType = this.read_type();
returnType = this.read_types();
}
if (this.expect(this.tok.T_DOUBLE_ARROW)) this.next();
const body = this.read_expr();
Expand Down
27 changes: 22 additions & 5 deletions src/parser/function.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ module.exports = {
nullable = true;
this.next();
}
returnType = this.read_type();
returnType = this.read_types();
}
if (type === 1) {
// closure
Expand Down Expand Up @@ -202,14 +202,14 @@ module.exports = {
const node = this.node("parameter");
let parameterName = null;
let value = null;
let type = null;
let types = null;
let nullable = false;
if (this.token === "?") {
this.next();
nullable = true;
}
type = this.read_type();
if (nullable && !type) {
types = this.read_types();
if (nullable && !types) {
this.raiseError(
"Expecting a type definition combined with nullable operator"
);
Expand All @@ -225,7 +225,24 @@ module.exports = {
if (this.token == "=") {
value = this.next().read_expr();
}
return node(parameterName, type, value, isRef, isVariadic, nullable);
return node(parameterName, types, value, isRef, isVariadic, nullable);
},
read_types() {
const types = [];
const unionType = this.node("uniontype");
let type = this.read_type();
if (!type) return null;
types.push(type);
while (this.token === "|") {
this.next();
type = this.read_type();
types.push(type);
}
if (types.length === 1) {
return types[0];
} else {
return unionType(types);
}
},
/**
* Reads a list of arguments
Expand Down
152 changes: 140 additions & 12 deletions test/snapshot/__snapshots__/class.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -469,18 +469,22 @@ Program {
"name": "y",
},
"nullable": false,
"type": Array [
TypeReference {
"kind": "typereference",
"name": "float",
"raw": "float",
},
TypeReference {
"kind": "typereference",
"name": "string",
"raw": "string",
},
],
"type": UnionType {
"kind": "uniontype",
"name": null,
"types": Array [
TypeReference {
"kind": "typereference",
"name": "float",
"raw": "float",
},
TypeReference {
"kind": "typereference",
"name": "string",
"raw": "string",
},
],
},
"value": null,
},
],
Expand All @@ -504,6 +508,130 @@ 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": UnionType {
"kind": "uniontype",
"name": null,
"types": 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": UnionType {
"kind": "uniontype",
"name": null,
"types": 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": UnionType {
"kind": "uniontype",
"name": null,
"types": 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
Loading