Skip to content

Commit

Permalink
fix: use PropertyStatement node for multiple properties
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Jan 25, 2019
1 parent 813caea commit 6668746
Show file tree
Hide file tree
Showing 12 changed files with 1,649 additions and 204 deletions.
12 changes: 9 additions & 3 deletions src/ast.js
Expand Up @@ -99,14 +99,15 @@ const Position = require("./ast/position");
* - [Block](#block)
* - [Program](#program)
* - [Namespace](#namespace)
* - [PropertyStatement](#propertystatement)
* - [Property](#property)
* - [Declaration](#declaration)
* - [Class](#class)
* - [Interface](#interface)
* - [Trait](#trait)
* - [Function](#function)
* - [Method](#method)
* - [Parameter](#parameter)
* - [Property](#property)
* ---
*/

Expand Down Expand Up @@ -296,8 +297,12 @@ AST.prototype.resolvePrecedence = function(result, parser) {
result = buffer;
}
}
} else if (result.kind === "silent" && result.expr.right && !result.expr.parenthesizedExpression) {
// overall least precedence
} else if (
result.kind === "silent" &&
result.expr.right &&
!result.expr.parenthesizedExpression
) {
// overall least precedence
buffer = result.expr;
result.expr = buffer.left;
buffer.left = result;
Expand Down Expand Up @@ -505,6 +510,7 @@ AST.prototype.checkNodes = function() {
require("./ast/program"),
require("./ast/property"),
require("./ast/propertylookup"),
require("./ast/propertystatement"),
require("./ast/reference"),
require("./ast/retif"),
require("./ast/return"),
Expand Down
15 changes: 6 additions & 9 deletions src/ast/property.js
Expand Up @@ -5,26 +5,23 @@
*/
"use strict";

const Declaration = require("./declaration");
const Statement = require("./statement");
const KIND = "property";

/**
* Defines a class property
* @constructor Property
* @extends {Declaration}
* @property {boolean} isFinal
* @property {boolean} isStatic
* @property {string} visibility
* @extends {Statement}
* @property {string} name
* @property {Node|null} value
*/
module.exports = Declaration.extends(KIND, function Property(
module.exports = Statement.extends(KIND, function Property(
name,
value,
flags,
docs,
location
) {
Declaration.apply(this, [KIND, name, docs, location]);
Statement.apply(this, [KIND, docs, location]);
this.name = name;
this.value = value;
this.parseFlags(flags);
});
45 changes: 45 additions & 0 deletions src/ast/propertystatement.js
@@ -0,0 +1,45 @@
/**
* Copyright (C) 2018 Glayzzle (BSD3 License)
* @authors https://github.com/glayzzle/php-parser/graphs/contributors
* @url http://glayzzle.com
*/
"use strict";

const Statement = require("./statement");
const KIND = "propertystatement";

const IS_UNDEFINED = "";
const IS_PUBLIC = "public";
const IS_PROTECTED = "protected";
const IS_PRIVATE = "private";

/**
* Declares a properties into the current scope
* @constructor PropertyStatement
* @extends {Statement}
* @property {Property[]} properties
*/
module.exports = Statement.extends(KIND, function PropertyStatement(
kind,
properties,
flags,
docs,
location
) {
Statement.apply(this, [KIND, docs, location]);
this.properties = properties;

if (flags[0] === -1) {
this.visibility = IS_UNDEFINED;
} else if (flags[0] === null) {
this.visibility = null;
} else if (flags[0] === 0) {
this.visibility = IS_PUBLIC;
} else if (flags[0] === 1) {
this.visibility = IS_PROTECTED;
} else if (flags[0] === 2) {
this.visibility = IS_PRIVATE;
}

this.isStatic = flags[1] === 1;
});
14 changes: 10 additions & 4 deletions src/parser/class.js
Expand Up @@ -132,7 +132,9 @@ module.exports = {
* ```
*/
read_variable_list: function(flags) {
return this.read_list(
const result = this.node("propertystatement");

const properties = this.read_list(
/**
* Reads a variable declaration
*
Expand All @@ -143,20 +145,24 @@ module.exports = {
function read_variable_declaration() {
const result = this.node("property");
this.expect(this.tok.T_VARIABLE);
let propName = this.node("identifier");
const name = this.text().substring(1); // ignore $
this.next();
propName = propName(name);
if (this.token === ";" || this.token === ",") {
return result(name, null, flags);
return result(propName, null);
} else if (this.token === "=") {
// https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L815
return result(name, this.next().read_expr(), flags);
return result(propName, this.next().read_expr());
} else {
this.expect([",", ";", "="]);
return result(name, null, flags);
return result(propName, null);
}
},
","
);

return result(null, properties, flags);
},
/**
* Reads constant list
Expand Down

0 comments on commit 6668746

Please sign in to comment.