Skip to content

Commit 7508f2f

Browse files
committed
fix glayzzle#113 - add support for type hint
1 parent 5dc184c commit 7508f2f

File tree

7 files changed

+214
-52
lines changed

7 files changed

+214
-52
lines changed

src/ast.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,7 @@ AST.prototype.prepare = function(kind, docs, parser) {
423423
require("./ast/traitprecedence"),
424424
require("./ast/traituse"),
425425
require("./ast/try"),
426+
require("./ast/typereference"),
426427
require("./ast/unary"),
427428
require("./ast/unset"),
428429
require("./ast/usegroup"),

src/ast/typereference.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 Reference = require("./reference");
9+
const KIND = "typereference";
10+
11+
/**
12+
* Defines a class reference node
13+
* @constructor TypeReference
14+
* @extends {Reference}
15+
* @property {string} name
16+
*/
17+
const TypeReference = Reference.extends(KIND, function TypeReference(
18+
name,
19+
docs,
20+
location
21+
) {
22+
Reference.apply(this, [KIND, docs, location]);
23+
this.name = name;
24+
});
25+
26+
TypeReference.types = ["int", "float", "string", "bool", "object", "array"];
27+
28+
module.exports = TypeReference;

src/parser/function.js

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -240,20 +240,33 @@ module.exports = {
240240
* ```
241241
*/
242242
read_type: function() {
243-
const result = this.node("classreference");
244-
switch (this.token) {
245-
case this.tok.T_ARRAY:
243+
const result = this.node("typereference");
244+
let type = null;
245+
if (this.token === this.tok.T_ARRAY || this.token === this.tok.T_CALLABLE) {
246+
let type = this.text();
247+
this.next();
248+
return result(type);
249+
} else if (this.token === this.tok.T_STRING) {
250+
let type = this.text();
251+
const backup = [this.token, this.lexer.getState()];
252+
this.next();
253+
if (
254+
this.token !== this.tok.T_NS_SEPARATOR &&
255+
this.ast.typereference.types.indexOf(type.toLowerCase()) > -1
256+
) {
257+
return result(type);
258+
} else {
259+
// rollback a classic namespace
260+
this.lexer.tokens.push(backup);
246261
this.next();
247-
return result(["", "array"], false);
248-
case this.tok.T_NAMESPACE:
249-
case this.tok.T_NS_SEPARATOR:
250-
case this.tok.T_STRING:
251262
return this.read_namespace_name();
252-
case this.tok.T_CALLABLE:
253-
this.next();
254-
return result(["", "callable"], false);
255-
default:
256-
return null;
263+
}
264+
} else if (
265+
this.token === this.tok.T_NAMESPACE ||
266+
this.token === this.tok.T_NS_SEPARATOR
267+
) {
268+
return this.read_namespace_name();
257269
}
270+
return null;
258271
}
259272
};

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

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,8 +1432,8 @@ Program {
14321432
"name": "Am_I_Uggly",
14331433
},
14341434
"nullable": false,
1435-
"type": ClassReference {
1436-
"kind": "classreference",
1435+
"type": TypeReference {
1436+
"kind": "typereference",
14371437
"loc": Location {
14381438
"end": Position {
14391439
"column": 39,
@@ -1448,7 +1448,6 @@ Program {
14481448
},
14491449
},
14501450
"name": "bool",
1451-
"resolution": "uqn",
14521451
},
14531452
"visibility": "public",
14541453
},
@@ -1491,8 +1490,8 @@ Program {
14911490
"name": "broken",
14921491
},
14931492
"nullable": false,
1494-
"type": ClassReference {
1495-
"kind": "classreference",
1493+
"type": TypeReference {
1494+
"kind": "typereference",
14961495
"loc": Location {
14971496
"end": Position {
14981497
"column": 38,
@@ -1507,7 +1506,6 @@ Program {
15071506
},
15081507
},
15091508
"name": "bool",
1510-
"resolution": "uqn",
15111509
},
15121510
"visibility": "protected",
15131511
},
@@ -1550,8 +1548,8 @@ Program {
15501548
"name": "isWhiteSnowAlive",
15511549
},
15521550
"nullable": false,
1553-
"type": ClassReference {
1554-
"kind": "classreference",
1551+
"type": TypeReference {
1552+
"kind": "typereference",
15551553
"loc": Location {
15561554
"end": Position {
15571555
"column": 55,
@@ -1566,7 +1564,6 @@ Program {
15661564
},
15671565
},
15681566
"name": "bool",
1569-
"resolution": "uqn",
15701567
},
15711568
"visibility": "protected",
15721569
},
@@ -1847,8 +1844,8 @@ Program {
18471844
},
18481845
"name": "arrow",
18491846
"nullable": false,
1850-
"type": ClassReference {
1851-
"kind": "classreference",
1847+
"type": TypeReference {
1848+
"kind": "typereference",
18521849
"loc": Location {
18531850
"end": Position {
18541851
"column": 29,
@@ -1863,7 +1860,6 @@ Program {
18631860
},
18641861
},
18651862
"name": "bool",
1866-
"resolution": "uqn",
18671863
},
18681864
"value": Boolean {
18691865
"kind": "boolean",
@@ -2492,8 +2488,8 @@ Program {
24922488
"name": "draw",
24932489
},
24942490
"nullable": false,
2495-
"type": ClassReference {
2496-
"kind": "classreference",
2491+
"type": TypeReference {
2492+
"kind": "typereference",
24972493
"loc": Location {
24982494
"end": Position {
24992495
"column": 54,
@@ -2508,7 +2504,6 @@ Program {
25082504
},
25092505
},
25102506
"name": "string",
2511-
"resolution": "uqn",
25122507
},
25132508
"visibility": "public",
25142509
},
@@ -3609,8 +3604,8 @@ next:
36093604
"name": "sparta",
36103605
},
36113606
"nullable": true,
3612-
"type": ClassReference {
3613-
"kind": "classreference",
3607+
"type": TypeReference {
3608+
"kind": "typereference",
36143609
"loc": Location {
36153610
"end": Position {
36163611
"column": 26,
@@ -3625,7 +3620,6 @@ next:
36253620
},
36263621
},
36273622
"name": "int",
3628-
"resolution": "uqn",
36293623
},
36303624
},
36313625
ExpressionStatement {
@@ -3720,8 +3714,8 @@ next:
37203714
},
37213715
"name": "bar",
37223716
"nullable": true,
3723-
"type": ClassReference {
3724-
"kind": "classreference",
3717+
"type": TypeReference {
3718+
"kind": "typereference",
37253719
"loc": Location {
37263720
"end": Position {
37273721
"column": 22,
@@ -3736,7 +3730,6 @@ next:
37363730
},
37373731
},
37383732
"name": "int",
3739-
"resolution": "uqn",
37403733
},
37413734
"value": Number {
37423735
"kind": "number",
@@ -6292,8 +6285,8 @@ next:
62926285
},
62936286
},
62946287
"nullable": false,
6295-
"type": ClassReference {
6296-
"kind": "classreference",
6288+
"type": TypeReference {
6289+
"kind": "typereference",
62976290
"loc": Location {
62986291
"end": Position {
62996292
"column": 56,
@@ -6308,7 +6301,6 @@ next:
63086301
},
63096302
},
63106303
"name": "bool",
6311-
"resolution": "uqn",
63126304
},
63136305
"uses": Array [
63146306
Variable {

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -647,10 +647,9 @@ Program {
647647
"kind": "parameter",
648648
"name": "data",
649649
"nullable": false,
650-
"type": ClassReference {
651-
"kind": "classreference",
652-
"name": "\\\\array",
653-
"resolution": "fqn",
650+
"type": TypeReference {
651+
"kind": "typereference",
652+
"name": "array",
654653
},
655654
"value": Identifier {
656655
"kind": "identifier",

0 commit comments

Comments
 (0)