Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions src/parser/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ module.exports = {
continue;
}

const locStart = this.position();

if (this.token === this.tok.T_ATTRIBUTE) {
attrs = this.read_attr_list();
}
Expand All @@ -108,14 +110,12 @@ module.exports = {
continue;
}

const locStart = this.position();

// read member flags
const flags = this.read_member_flags(false);

// check constant
if (this.token === this.tok.T_CONST) {
const constants = this.read_constant_list(flags, attrs);
const constants = this.read_constant_list(flags, attrs, locStart);
if (this.expect(";")) {
this.next();
}
Expand Down Expand Up @@ -151,7 +151,7 @@ module.exports = {
this.token === this.tok.T_STRING)))
) {
// reads a variable
const variables = this.read_variable_list(flags, attrs);
const variables = this.read_variable_list(flags, attrs, locStart);
attrs = [];
result = result.concat(variables);
} else {
Expand All @@ -176,7 +176,7 @@ module.exports = {
* variable_list ::= (variable_declaration ',')* variable_declaration
* ```
*/
read_variable_list(flags, attrs) {
read_variable_list(flags, attrs, locStart) {
let property_statement = this.node("propertystatement");

const properties = this.read_list(
Expand Down Expand Up @@ -233,6 +233,16 @@ module.exports = {
);

property_statement = property_statement(null, properties, flags);
if (locStart && property_statement.loc) {
property_statement.loc.start = locStart;
if (property_statement.loc.source) {
property_statement.loc.source = this.lexer._input.substr(
property_statement.loc.start.offset,
property_statement.loc.end.offset -
property_statement.loc.start.offset,
);
}
}

// semicolons are found only for regular properties definitions.
// Property hooks are terminated by a closing curly brace, }.
Expand Down Expand Up @@ -335,7 +345,7 @@ module.exports = {
* constant_list ::= T_CONST [type] (constant_declaration ',')* constant_declaration
* ```
*/
read_constant_list(flags, attrs) {
read_constant_list(flags, attrs, locStart) {
const result = this.node("classconstant");
if (this.expect(this.tok.T_CONST)) {
this.next();
Expand Down Expand Up @@ -383,7 +393,17 @@ module.exports = {
",",
);

return result(null, items, flags, nullable, type, attrs || []);
const node = result(null, items, flags, nullable, type, attrs || []);
if (locStart && node.loc) {
node.loc.start = locStart;
if (node.loc.source) {
node.loc.source = this.lexer._input.substr(
node.loc.start.offset,
node.loc.end.offset - node.loc.start.offset,
);
}
}
return node;
},
/*
* Read member flags
Expand Down Expand Up @@ -581,7 +601,7 @@ module.exports = {

// check constant
if (this.token === this.tok.T_CONST) {
const constants = this.read_constant_list(flags, attrs);
const constants = this.read_constant_list(flags, attrs, locStart);
if (this.expect(";")) {
this.next();
}
Expand All @@ -600,7 +620,7 @@ module.exports = {
this.next();
}
} else if (this.token === this.tok.T_STRING) {
result.push(this.read_variable_list(flags, attrs));
result.push(this.read_variable_list(flags, attrs, locStart));
} else {
// raise an error
this.error([this.tok.T_CONST, this.tok.T_FUNCTION, this.tok.T_STRING]);
Expand Down
6 changes: 3 additions & 3 deletions test/snapshot/__snapshots__/acid.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -841,14 +841,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": [
Expand Down
Loading