Skip to content

Commit 1528bf2

Browse files
committed
added support for attributes on:
- interfaces
1 parent 257595c commit 1528bf2

12 files changed

+126
-9
lines changed

src/ast/interface.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,18 @@ const KIND = "interface";
1414
* @extends {Declaration}
1515
* @property {Identifier[]} extends
1616
* @property {Declaration[]} body
17+
* @property {AttrGroup[]} attrGroups
1718
*/
1819
module.exports = Declaration.extends(KIND, function Interface(
1920
name,
2021
ext,
2122
body,
23+
attrGroups,
2224
docs,
2325
location
2426
) {
2527
Declaration.apply(this, [KIND, name, docs, location]);
2628
this.extends = ext;
2729
this.body = body;
30+
this.attrGroups = attrGroups;
2831
});

src/parser/class.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ module.exports = {
351351
* interface ::= T_INTERFACE T_STRING (T_EXTENDS (NAMESPACE_NAME ',')* NAMESPACE_NAME)? '{' INTERFACE_BODY '}'
352352
* ```
353353
*/
354-
read_interface_declaration_statement: function () {
354+
read_interface_declaration_statement: function (attrs) {
355355
const result = this.node("interface");
356356
if (this.token !== this.tok.T_INTERFACE) {
357357
this.error(this.tok.T_INTERFACE);
@@ -366,7 +366,7 @@ module.exports = {
366366
const propExtends = this.read_interface_extends_list();
367367
this.expect("{");
368368
const body = this.next().read_interface_body();
369-
return result(propName, propExtends, body);
369+
return result(propName, propExtends, body, attrs || []);
370370
},
371371
/**
372372
* Reads an interface body
@@ -401,7 +401,7 @@ module.exports = {
401401
result = result.concat(constants);
402402
} else if (this.token === this.tok.T_FUNCTION) {
403403
// reads a function
404-
const method = this.read_function_declaration(2, flags);
404+
const method = this.read_function_declaration(2, flags, attrs);
405405
method.parseFlags(flags);
406406
result.push(method);
407407
if (this.expect(";")) {

src/parser/function.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ module.exports = {
3535
read_function: function (closure, flag, attrs) {
3636
const result = this.read_function_declaration(
3737
closure ? 1 : flag ? 2 : 0,
38-
flag && flag[1] === 1
38+
flag && flag[1] === 1,
39+
attrs || []
3940
);
4041
if (flag && flag[2] == 1) {
4142
// abstract function :
@@ -54,7 +55,6 @@ module.exports = {
5455
result.parseFlags(flag);
5556
}
5657
}
57-
if (attrs) result.attrGroups = attrs;
5858
return result;
5959
},
6060
/**
@@ -63,7 +63,7 @@ module.exports = {
6363
* function_declaration ::= T_FUNCTION '&'? T_STRING '(' parameter_list ')'
6464
* ```
6565
*/
66-
read_function_declaration: function (type, isStatic) {
66+
read_function_declaration: function (type, isStatic, attrs) {
6767
let nodeName = "function";
6868
if (type === 1) {
6969
nodeName = "closure";
@@ -133,9 +133,13 @@ module.exports = {
133133
}
134134
if (type === 1) {
135135
// closure
136-
return result(params, isRef, use, returnType, nullable, isStatic);
136+
const fnNode = result(params, isRef, use, returnType, nullable, isStatic);
137+
fnNode.attrGroups = attrs || [];
138+
return fnNode;
137139
}
138-
return result(name, params, isRef, returnType, nullable);
140+
const fnNode = result(name, params, isRef, returnType, nullable);
141+
fnNode.attrGroups = attrs || [];
142+
return fnNode;
139143
},
140144

141145
read_lexical_vars: function () {

src/parser/statement.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ module.exports = {
5050
case this.tok.T_CLASS:
5151
return this.read_class_declaration_statement(attrs);
5252
case this.tok.T_INTERFACE:
53-
return this.read_interface_declaration_statement();
53+
return this.read_interface_declaration_statement(attrs);
5454
case this.tok.T_TRAIT:
5555
return this.read_trait_declaration_statement();
5656
case this.tok.T_USE:

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1703,6 +1703,7 @@ Program {
17031703
},
17041704
},
17051705
Interface {
1706+
"attrGroups": Array [],
17061707
"body": Array [
17071708
Method {
17081709
"arguments": Array [],

test/snapshot/__snapshots__/attributes.test.js.snap

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,94 @@ Program {
354354
}
355355
`;
356356

357+
exports[`Parse Attributes can parse interface attributes 1`] = `
358+
Program {
359+
"children": Array [
360+
Interface {
361+
"attrGroups": Array [
362+
AttrGroup {
363+
"attrs": Array [
364+
Attribute {
365+
"args": Array [],
366+
"kind": "attribute",
367+
"name": "A",
368+
},
369+
],
370+
"kind": "attrgroup",
371+
},
372+
],
373+
"body": Array [
374+
ClassConstant {
375+
"attrGroups": Array [
376+
AttrGroup {
377+
"attrs": Array [
378+
Attribute {
379+
"args": Array [],
380+
"kind": "attribute",
381+
"name": "C",
382+
},
383+
],
384+
"kind": "attrgroup",
385+
},
386+
],
387+
"constants": Array [
388+
Constant {
389+
"kind": "constant",
390+
"name": Identifier {
391+
"kind": "identifier",
392+
"name": "D",
393+
},
394+
"value": Number {
395+
"kind": "number",
396+
"value": "0",
397+
},
398+
},
399+
],
400+
"kind": "classconstant",
401+
"visibility": "",
402+
},
403+
Method {
404+
"arguments": Array [],
405+
"attrGroups": Array [
406+
AttrGroup {
407+
"attrs": Array [
408+
Attribute {
409+
"args": Array [],
410+
"kind": "attribute",
411+
"name": "E",
412+
},
413+
],
414+
"kind": "attrgroup",
415+
},
416+
],
417+
"body": null,
418+
"byref": false,
419+
"isAbstract": false,
420+
"isFinal": false,
421+
"isStatic": false,
422+
"kind": "method",
423+
"name": Identifier {
424+
"kind": "identifier",
425+
"name": "f",
426+
},
427+
"nullable": false,
428+
"type": null,
429+
"visibility": "public",
430+
},
431+
],
432+
"extends": null,
433+
"kind": "interface",
434+
"name": Identifier {
435+
"kind": "identifier",
436+
"name": "b",
437+
},
438+
},
439+
],
440+
"errors": Array [],
441+
"kind": "program",
442+
}
443+
`;
444+
357445
exports[`Parse Attributes can parse method attributes 1`] = `
358446
Program {
359447
"children": Array [

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ Program {
141141
},
142142
},
143143
Interface {
144+
"attrGroups": Array [],
144145
"body": Array [
145146
ClassConstant {
146147
"attrGroups": Array [],

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ exports[`Test graceful mode to suppress errors interface 1`] = `
44
Program {
55
"children": Array [
66
Interface {
7+
"attrGroups": Array [],
78
"body": Array [],
89
"extends": null,
910
"kind": "interface",
@@ -576,6 +577,7 @@ exports[`Test graceful mode to suppress errors test flags 1`] = `
576577
Program {
577578
"children": Array [
578579
Interface {
580+
"attrGroups": Array [],
579581
"body": Array [
580582
Method {
581583
"arguments": Array [],

test/snapshot/__snapshots__/interface.test.js.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ exports[`interface extends 1`] = `
44
Program {
55
"children": Array [
66
Interface {
7+
"attrGroups": Array [],
78
"body": Array [],
89
"extends": Array [
910
Name {
@@ -28,6 +29,7 @@ exports[`interface interface name as identifier 1`] = `
2829
Program {
2930
"children": Array [
3031
Interface {
32+
"attrGroups": Array [],
3133
"body": Array [],
3234
"extends": null,
3335
"kind": "interface",
@@ -46,6 +48,7 @@ exports[`interface multiple extends 1`] = `
4648
Program {
4749
"children": Array [
4850
Interface {
51+
"attrGroups": Array [],
4952
"body": Array [],
5053
"extends": Array [
5154
Name {

test/snapshot/__snapshots__/location.test.js.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7536,6 +7536,7 @@ exports[`Test locations test interface 1`] = `
75367536
Program {
75377537
"children": Array [
75387538
Interface {
7539+
"attrGroups": Array [],
75397540
"body": Array [],
75407541
"extends": null,
75417542
"kind": "interface",

0 commit comments

Comments
 (0)