Skip to content
This repository was archived by the owner on Mar 29, 2023. It is now read-only.

Commit a241e49

Browse files
feat: identifier for class/interface/trait nodes
1 parent 940e0d0 commit a241e49

19 files changed

+878
-128
lines changed

src/ast/declaration.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const IS_PRIVATE = "private";
1717
* A declaration statement (function, class, interface...)
1818
* @constructor Declaration
1919
* @extends {Statement}
20-
* @property {string} name
20+
* @property {Identifier|string} name
2121
*/
2222
const Declaration = Statement.extends(KIND, function Declaration(
2323
kind,

src/ast/trait.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,14 @@ const KIND = "trait";
1212
* A trait definition
1313
* @constructor Trait
1414
* @extends {Declaration}
15-
* @property {Identifier|null} extends
16-
* @property {Identifier[]} implements
1715
* @property {Declaration[]} body
1816
*/
1917
module.exports = Declaration.extends(KIND, function Trait(
2018
name,
21-
ext,
22-
impl,
2319
body,
2420
docs,
2521
location
2622
) {
2723
Declaration.apply(this, [KIND, name, docs, location]);
28-
this.extends = ext;
29-
this.implements = impl;
3024
this.body = body;
3125
});

src/parser/class.js

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@ module.exports = {
2222
return null;
2323
}
2424
this.next().expect(this.tok.T_STRING);
25-
const propName = this.text();
25+
let propName = this.node("identifier");
26+
const name = this.text();
27+
this.next();
28+
propName = propName(name);
2629
let propExtends = null;
27-
let propImplements = null;
28-
if (this.next().token == this.tok.T_EXTENDS) {
30+
if (this.token == this.tok.T_EXTENDS) {
2931
propExtends = this.next().read_namespace_name();
3032
}
33+
let propImplements = null;
3134
if (this.token == this.tok.T_IMPLEMENTS) {
3235
propImplements = this.next().read_name_list();
3336
}
@@ -269,23 +272,23 @@ module.exports = {
269272
*/
270273
read_interface: function() {
271274
const result = this.node("interface");
272-
let name = null;
273-
let body = null;
274-
let propExtends = null;
275-
if (this.expect(this.tok.T_INTERFACE)) {
276-
this.next();
277-
}
278-
if (this.expect(this.tok.T_STRING)) {
279-
name = this.text();
275+
if (this.token !== this.tok.T_INTERFACE) {
276+
this.error(this.tok.T_INTERFACE);
280277
this.next();
278+
return null;
281279
}
280+
this.next().expect(this.tok.T_STRING);
281+
let propName = this.node("identifier");
282+
const name = this.text();
283+
this.next();
284+
propName = propName(name);
285+
let propExtends = null;
282286
if (this.token === this.tok.T_EXTENDS) {
283287
propExtends = this.next().read_name_list();
284288
}
285-
if (this.expect("{")) {
286-
body = this.next().read_interface_body();
287-
}
288-
return result(name, propExtends, body);
289+
this.expect("{");
290+
const body = this.next().read_interface_body();
291+
return result(propName, propExtends, body);
289292
},
290293
/**
291294
* Reads an interface body
@@ -344,26 +347,20 @@ module.exports = {
344347
*/
345348
read_trait: function() {
346349
const result = this.node("trait");
347-
let propName = null;
348-
let propExtends = null;
349-
let propImplements = null;
350-
let body = null;
351-
if (this.expect(this.tok.T_TRAIT)) {
350+
// graceful mode : ignore token & go next
351+
if (this.token !== this.tok.T_TRAIT) {
352+
this.error(this.tok.T_TRAIT);
352353
this.next();
354+
return null;
353355
}
354-
if (this.expect(this.tok.T_STRING)) {
355-
propName = this.text();
356-
}
357-
if (this.next().token == this.tok.T_EXTENDS) {
358-
propExtends = this.next().read_namespace_name();
359-
}
360-
if (this.token == this.tok.T_IMPLEMENTS) {
361-
propImplements = this.next().read_name_list();
362-
}
363-
if (this.expect("{")) {
364-
body = this.next().read_class_body();
365-
}
366-
return result(propName, propExtends, propImplements, body);
356+
this.next().expect(this.tok.T_STRING);
357+
let propName = this.node("identifier");
358+
const name = this.text();
359+
this.next();
360+
propName = propName(name);
361+
this.expect("{");
362+
const body = this.next().read_class_body();
363+
return result(propName, body);
367364
},
368365
/**
369366
* reading a use statement

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

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1507,7 +1507,23 @@ Program {
15071507
"offset": 390,
15081508
},
15091509
},
1510-
"name": "fooBar",
1510+
"name": Identifier {
1511+
"kind": "identifier",
1512+
"loc": Location {
1513+
"end": Position {
1514+
"column": 23,
1515+
"line": 25,
1516+
"offset": 411,
1517+
},
1518+
"source": "fooBar",
1519+
"start": Position {
1520+
"column": 17,
1521+
"line": 25,
1522+
"offset": 405,
1523+
},
1524+
},
1525+
"name": "fooBar",
1526+
},
15111527
},
15121528
Interface {
15131529
"body": Array [
@@ -1727,7 +1743,23 @@ Program {
17271743
"offset": 942,
17281744
},
17291745
},
1730-
"name": "Miror",
1746+
"name": Identifier {
1747+
"kind": "identifier",
1748+
"loc": Location {
1749+
"end": Position {
1750+
"column": 17,
1751+
"line": 49,
1752+
"offset": 957,
1753+
},
1754+
"source": "Miror",
1755+
"start": Position {
1756+
"column": 12,
1757+
"line": 49,
1758+
"offset": 952,
1759+
},
1760+
},
1761+
"name": "Miror",
1762+
},
17311763
},
17321764
_Function {
17331765
"arguments": Array [],
@@ -2726,8 +2758,6 @@ Program {
27262758
"visibility": "private",
27272759
},
27282760
],
2729-
"extends": null,
2730-
"implements": null,
27312761
"kind": "trait",
27322762
"loc": Location {
27332763
"end": Position {
@@ -2758,7 +2788,23 @@ Program {
27582788
"offset": 1196,
27592789
},
27602790
},
2761-
"name": "Line",
2791+
"name": Identifier {
2792+
"kind": "identifier",
2793+
"loc": Location {
2794+
"end": Position {
2795+
"column": 12,
2796+
"line": 60,
2797+
"offset": 1206,
2798+
},
2799+
"source": "Line",
2800+
"start": Position {
2801+
"column": 8,
2802+
"line": 60,
2803+
"offset": 1202,
2804+
},
2805+
},
2806+
"name": "Line",
2807+
},
27622808
"trailingComments": Array [
27632809
CommentLine {
27642810
"kind": "commentline",

test/snapshot/__snapshots__/call.test.js.snap

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,10 @@ Program {
797797
"isAnonymous": false,
798798
"isFinal": false,
799799
"kind": "class",
800-
"name": "Foo",
800+
"name": Identifier {
801+
"kind": "identifier",
802+
"name": "Foo",
803+
},
801804
},
802805
],
803806
"errors": Array [],
@@ -917,7 +920,10 @@ Program {
917920
"isAnonymous": false,
918921
"isFinal": false,
919922
"kind": "class",
920-
"name": "Foo",
923+
"name": Identifier {
924+
"kind": "identifier",
925+
"name": "Foo",
926+
},
921927
},
922928
],
923929
"errors": Array [],
@@ -1018,7 +1024,10 @@ Program {
10181024
"isAnonymous": false,
10191025
"isFinal": false,
10201026
"kind": "class",
1021-
"name": "Foo",
1027+
"name": Identifier {
1028+
"kind": "identifier",
1029+
"name": "Foo",
1030+
},
10221031
},
10231032
],
10241033
"errors": Array [],

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

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,10 @@ Program {
117117
"isAnonymous": false,
118118
"isFinal": false,
119119
"kind": "class",
120-
"name": "foo",
120+
"name": Identifier {
121+
"kind": "identifier",
122+
"name": "foo",
123+
},
121124
},
122125
Interface {
123126
"body": Array [
@@ -173,7 +176,10 @@ Program {
173176
},
174177
],
175178
"kind": "interface",
176-
"name": "boo",
179+
"name": Identifier {
180+
"kind": "identifier",
181+
"name": "boo",
182+
},
177183
},
178184
Trait {
179185
"body": Array [
@@ -215,20 +221,11 @@ Program {
215221
"visibility": "protected",
216222
},
217223
],
218-
"extends": ClassReference {
219-
"kind": "classreference",
220-
"name": "foo",
221-
"resolution": "uqn",
222-
},
223-
"implements": Array [
224-
ClassReference {
225-
"kind": "classreference",
226-
"name": "boo",
227-
"resolution": "uqn",
228-
},
229-
],
230224
"kind": "trait",
231-
"name": "line",
225+
"name": Identifier {
226+
"kind": "identifier",
227+
"name": "line",
228+
},
232229
},
233230
],
234231
"comments": Array [
@@ -384,7 +381,10 @@ Program {
384381
"isAnonymous": false,
385382
"isFinal": false,
386383
"kind": "class",
387-
"name": "A",
384+
"name": Identifier {
385+
"kind": "identifier",
386+
"name": "A",
387+
},
388388
},
389389
],
390390
"errors": Array [],
@@ -505,7 +505,10 @@ Program {
505505
"isAnonymous": false,
506506
"isFinal": false,
507507
"kind": "class",
508-
"name": "__proto__",
508+
"name": Identifier {
509+
"kind": "identifier",
510+
"name": "__proto__",
511+
},
509512
},
510513
],
511514
"errors": Array [],
@@ -562,7 +565,10 @@ Program {
562565
"isAnonymous": false,
563566
"isFinal": false,
564567
"kind": "class",
565-
"name": "foo",
568+
"name": Identifier {
569+
"kind": "identifier",
570+
"name": "foo",
571+
},
566572
},
567573
],
568574
"errors": Array [
@@ -854,7 +860,10 @@ Program {
854860
"isAnonymous": false,
855861
"isFinal": true,
856862
"kind": "class",
857-
"name": "foo",
863+
"name": Identifier {
864+
"kind": "identifier",
865+
"name": "foo",
866+
},
858867
},
859868
Class {
860869
"body": Array [
@@ -990,7 +999,32 @@ Program {
990999
"isAnonymous": false,
9911000
"isFinal": false,
9921001
"kind": "class",
993-
"name": "bar",
1002+
"name": Identifier {
1003+
"kind": "identifier",
1004+
"name": "bar",
1005+
},
1006+
},
1007+
],
1008+
"errors": Array [],
1009+
"kind": "program",
1010+
}
1011+
`;
1012+
1013+
exports[`Test classes class name as identifier 1`] = `
1014+
Program {
1015+
"children": Array [
1016+
Class {
1017+
"body": Array [],
1018+
"extends": null,
1019+
"implements": null,
1020+
"isAbstract": false,
1021+
"isAnonymous": false,
1022+
"isFinal": false,
1023+
"kind": "class",
1024+
"name": Identifier {
1025+
"kind": "identifier",
1026+
"name": "A",
1027+
},
9941028
},
9951029
],
9961030
"errors": Array [],

0 commit comments

Comments
 (0)