Skip to content

refactor: byRef #388

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 31, 2019
Merged
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
2 changes: 2 additions & 0 deletions src/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const Position = require("./ast/position");
* - [Exit](#exit)
* - [Clone](#clone)
* - [Assign](#assign)
* - [AssignRef](#assignref)
* - [Array](#array)
* - [List](#list)
* - [Variable](#variable)
Expand Down Expand Up @@ -458,6 +459,7 @@ AST.prototype.checkNodes = function() {
[
require("./ast/array"),
require("./ast/assign"),
require("./ast/assignref"),
require("./ast/bin"),
require("./ast/block"),
require("./ast/boolean"),
Expand Down
2 changes: 1 addition & 1 deletion src/ast/assign.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module.exports = Expression.extends(KIND, function Assign(
location
) {
Expression.apply(this, [KIND, docs, location]);
this.operator = operator;
this.left = left;
this.right = right;
this.operator = operator;
});
28 changes: 28 additions & 0 deletions src/ast/assignref.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Copyright (C) 2018 Glayzzle (BSD3 License)
* @authors https://github.com/glayzzle/php-parser/graphs/contributors
* @url http://glayzzle.com
*/
"use strict";

const Expression = require("./expression");
const KIND = "assignref";

/**
* Assigns a value to the specified target
* @constructor Assign
* @extends {Expression}
* @property {Expression} left
* @property {Expression} right
* @property {String} operator
*/
module.exports = Expression.extends(KIND, function AssignRef(
left,
right,
docs,
location
) {
Expression.apply(this, [KIND, docs, location]);
this.left = left;
this.right = right;
});
26 changes: 12 additions & 14 deletions src/parser/expr.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,21 +354,19 @@ module.exports = {
if (isConst) this.error("VARIABLE");
let right;
if (this.next().token == "&") {
right = this.read_byref(
function() {
if (this.token === this.tok.T_NEW) {
if (this.php7) {
this.error();
}
return this.read_new_expr();
} else {
return this.read_variable(false, false);
}
}.bind(this)
);
} else {
right = this.read_expr();
this.next();
if (this.token === this.tok.T_NEW) {
if (this.php7) {
this.error();
}
right = this.read_new_expr();
} else {
right = this.read_variable(false, false);
}

return result("assignref", expr, right);
}
right = this.read_expr();
return result("assign", expr, right, "=");
}

Expand Down
6 changes: 2 additions & 4 deletions test/snapshot/__snapshots__/assign.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -402,16 +402,14 @@ exports[`assign with ref 1`] = `
Program {
"children": Array [
ExpressionStatement {
"expression": Assign {
"kind": "assign",
"expression": AssignRef {
"kind": "assignref",
"left": Variable {
"curly": false,
"kind": "variable",
"name": "bar",
},
"operator": "=",
"right": Variable {
"byref": true,
"curly": false,
"kind": "variable",
"name": "foo",
Expand Down
Loading