Skip to content

Commit d14af18

Browse files
committed
glayzzle#511 - incl tokens on methods, properties, const
1 parent f02fea4 commit d14af18

File tree

7 files changed

+449
-32
lines changed

7 files changed

+449
-32
lines changed

src/parser/class.js

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ module.exports = {
138138
* ```
139139
*/
140140
read_variable_list: function(flags) {
141-
const result = this.node("propertystatement");
141+
let result = this.node("propertystatement");
142142

143143
const properties = this.read_list(
144144
/**
@@ -168,8 +168,12 @@ module.exports = {
168168
},
169169
","
170170
);
171-
172-
return result(null, properties, flags);
171+
result = result(null, properties, flags);
172+
// including visibility modifiers tokens
173+
if (flags[3]) {
174+
this.ast.swapLocations(result, flags[3], result, this);
175+
}
176+
return result;
173177
},
174178
/**
175179
* Reads constant list
@@ -178,7 +182,7 @@ module.exports = {
178182
* ```
179183
*/
180184
read_constant_list: function(flags) {
181-
const result = this.node("classconstant");
185+
let result = this.node("classconstant");
182186
if (this.expect(this.tok.T_CONST)) {
183187
this.next();
184188
}
@@ -213,18 +217,30 @@ module.exports = {
213217
},
214218
","
215219
);
216-
217-
return result(null, items, flags);
220+
result = result(null, items, flags);
221+
// including visibility modifiers tokens
222+
if (flags[3]) {
223+
this.ast.swapLocations(result, flags[3], result, this);
224+
}
225+
return result;
218226
},
219227
/**
220228
* Read member flags
221229
* @return array
222230
* 1st index : 0 => public, 1 => protected, 2 => private
223231
* 2nd index : 0 => instance member, 1 => static member
224232
* 3rd index : 0 => normal, 1 => abstract member, 2 => final member
233+
* 4th index : location of tokens (to be included into resulting node)
225234
*/
226235
read_member_flags: function(asInterface) {
227-
const result = [-1, -1, -1];
236+
const result = [
237+
-1,
238+
-1,
239+
-1,
240+
{
241+
loc: { start: this.ast.position(this) }
242+
}
243+
];
228244
if (this.is("T_MEMBER_FLAGS")) {
229245
let idx = 0,
230246
val = 0;

src/parser/function.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ module.exports = {
3737
closure ? 1 : flag ? 2 : 0,
3838
flag && flag[1] === 1
3939
);
40+
if (flag && flag[3]) {
41+
// include function modifiers tokens
42+
this.ast.swapLocations(result, flag[3], result, this);
43+
}
4044
if (flag && flag[2] == 1) {
4145
// abstract function :
4246
result.parseFlags(flag);

test/debug.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const parser = require("../src/index");
1919
const ast = parser.parseCode(
2020
`
2121
<?php
22-
declare(tick=1): /* 1 */ ENDDECLARE;
22+
class foo { public function bar() { } }
2323
`,
2424
{
2525
parser: {

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -834,14 +834,14 @@ Program {
834834
"line": 35,
835835
"offset": 648,
836836
},
837-
"source": "$dwarf = [
837+
"source": "protected $dwarf = [
838838
'sneezy' => 'achoum',
839839
'bashful' => 'tadah'
840840
]",
841841
"start": Position {
842-
"column": 14,
842+
"column": 4,
843843
"line": 32,
844-
"offset": 577,
844+
"offset": 567,
845845
},
846846
},
847847
"properties": Array [
@@ -1567,11 +1567,11 @@ Program {
15671567
"line": 46,
15681568
"offset": 934,
15691569
},
1570-
"source": "function doSomething()",
1570+
"source": "final public function doSomething()",
15711571
"start": Position {
1572-
"column": 17,
1572+
"column": 4,
15731573
"line": 39,
1574-
"offset": 713,
1574+
"offset": 700,
15751575
},
15761576
},
15771577
"name": Identifier {
@@ -2817,11 +2817,11 @@ Program {
28172817
"line": 72,
28182818
"offset": 1498,
28192819
},
2820-
"source": "function draw(bool $arrow = false) : string",
2820+
"source": "public function draw(bool $arrow = false) : string",
28212821
"start": Position {
2822-
"column": 11,
2822+
"column": 4,
28232823
"line": 61,
2824-
"offset": 1220,
2824+
"offset": 1213,
28252825
},
28262826
},
28272827
"name": Identifier {
@@ -2929,11 +2929,11 @@ Program {
29292929
"line": 75,
29302930
"offset": 1563,
29312931
},
2932-
"source": "function shuut()",
2932+
"source": "private function shuut()",
29332933
"start": Position {
2934-
"column": 12,
2934+
"column": 4,
29352935
"line": 73,
2936-
"offset": 1511,
2936+
"offset": 1503,
29372937
},
29382938
},
29392939
"name": Identifier {

0 commit comments

Comments
 (0)