Skip to content

Commit

Permalink
#511 - incl tokens on methods, properties, const
Browse files Browse the repository at this point in the history
  • Loading branch information
ichiriac committed Mar 26, 2020
1 parent f02fea4 commit d14af18
Show file tree
Hide file tree
Showing 7 changed files with 449 additions and 32 deletions.
30 changes: 23 additions & 7 deletions src/parser/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ module.exports = {
* ```
*/
read_variable_list: function(flags) {
const result = this.node("propertystatement");
let result = this.node("propertystatement");

const properties = this.read_list(
/**
Expand Down Expand Up @@ -168,8 +168,12 @@ module.exports = {
},
","
);

return result(null, properties, flags);
result = result(null, properties, flags);
// including visibility modifiers tokens
if (flags[3]) {
this.ast.swapLocations(result, flags[3], result, this);
}
return result;
},
/**
* Reads constant list
Expand All @@ -178,7 +182,7 @@ module.exports = {
* ```
*/
read_constant_list: function(flags) {
const result = this.node("classconstant");
let result = this.node("classconstant");
if (this.expect(this.tok.T_CONST)) {
this.next();
}
Expand Down Expand Up @@ -213,18 +217,30 @@ module.exports = {
},
","
);

return result(null, items, flags);
result = result(null, items, flags);
// including visibility modifiers tokens
if (flags[3]) {
this.ast.swapLocations(result, flags[3], result, this);
}
return result;
},
/**
* Read member flags
* @return array
* 1st index : 0 => public, 1 => protected, 2 => private
* 2nd index : 0 => instance member, 1 => static member
* 3rd index : 0 => normal, 1 => abstract member, 2 => final member
* 4th index : location of tokens (to be included into resulting node)
*/
read_member_flags: function(asInterface) {
const result = [-1, -1, -1];
const result = [
-1,
-1,
-1,
{
loc: { start: this.ast.position(this) }
}
];
if (this.is("T_MEMBER_FLAGS")) {
let idx = 0,
val = 0;
Expand Down
4 changes: 4 additions & 0 deletions src/parser/function.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ module.exports = {
closure ? 1 : flag ? 2 : 0,
flag && flag[1] === 1
);
if (flag && flag[3]) {
// include function modifiers tokens
this.ast.swapLocations(result, flag[3], result, this);
}
if (flag && flag[2] == 1) {
// abstract function :
result.parseFlags(flag);
Expand Down
2 changes: 1 addition & 1 deletion test/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const parser = require("../src/index");
const ast = parser.parseCode(
`
<?php
declare(tick=1): /* 1 */ ENDDECLARE;
class foo { public function bar() { } }
`,
{
parser: {
Expand Down
24 changes: 12 additions & 12 deletions test/snapshot/__snapshots__/acid.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -834,14 +834,14 @@ Program {
"line": 35,
"offset": 648,
},
"source": "$dwarf = [
"source": "protected $dwarf = [
'sneezy' => 'achoum',
'bashful' => 'tadah'
]",
"start": Position {
"column": 14,
"column": 4,
"line": 32,
"offset": 577,
"offset": 567,
},
},
"properties": Array [
Expand Down Expand Up @@ -1567,11 +1567,11 @@ Program {
"line": 46,
"offset": 934,
},
"source": "function doSomething()",
"source": "final public function doSomething()",
"start": Position {
"column": 17,
"column": 4,
"line": 39,
"offset": 713,
"offset": 700,
},
},
"name": Identifier {
Expand Down Expand Up @@ -2817,11 +2817,11 @@ Program {
"line": 72,
"offset": 1498,
},
"source": "function draw(bool $arrow = false) : string",
"source": "public function draw(bool $arrow = false) : string",
"start": Position {
"column": 11,
"column": 4,
"line": 61,
"offset": 1220,
"offset": 1213,
},
},
"name": Identifier {
Expand Down Expand Up @@ -2929,11 +2929,11 @@ Program {
"line": 75,
"offset": 1563,
},
"source": "function shuut()",
"source": "private function shuut()",
"start": Position {
"column": 12,
"column": 4,
"line": 73,
"offset": 1511,
"offset": 1503,
},
},
"name": Identifier {
Expand Down
Loading

0 comments on commit d14af18

Please sign in to comment.