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
2 changes: 1 addition & 1 deletion src/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ AST.precedence = {};
["cast", "silent"],
["**"],
// TODO: [ (array)
// TODO: clone, new
// TODO: new
].forEach(function (list, index) {
list.forEach(function (operator) {
AST.precedence[operator] = index + 1;
Expand Down
7 changes: 5 additions & 2 deletions src/parser/expr.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,15 +363,18 @@ module.exports = {
this.next();
if (this.version >= 805 && this.token === "(") {
this.next();
const what = this.read_expr();
let what = this.read_variable(false, false);
what = this.handleDereferencable(what);
let properties = null;
if (this.token === ",") {
properties = this.next().read_expr();
}
this.expect(")") && this.next();
return node(what, properties);
}
return node(this.read_expr(), null);
let what = this.read_variable(false, false);
what = this.handleDereferencable(what);
return node(what, null);
}

switch (this.token) {
Expand Down
57 changes: 57 additions & 0 deletions test/snapshot/clone.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
const parser = require("../main");

function filterKey(fn, obj) {
if (Array.isArray(obj)) {
return obj.map((e) => filterKey(fn, e));
}

if (typeof obj === "object" && obj !== null) {
return Object.keys(obj)
.filter(fn)
.reduce(
(result, key) => ({
...result,
[key]: filterKey(fn, obj[key]),
}),
{},
);
}

return obj;
}

function shouldBeSame(a, b) {
const fn = (key) => key !== "parenthesizedExpression";
expect(filterKey(fn, parser.parseEval(a))).toEqual(
filterKey(fn, parser.parseEval(b)),
);
}

describe("clone", function () {
it("simple", function () {
expect(parser.parseEval("clone $obj;")).toMatchSnapshot();
Expand All @@ -20,3 +47,33 @@ describe("clone", function () {
).toMatchSnapshot();
});
});

describe("clone precedence comparison", function () {
it("clone $obj + 1 should be same as (clone $obj) + 1", function () {
shouldBeSame("clone $obj + 1", "(clone $obj) + 1");
});
it("clone $obj * 2 should be same as (clone $obj) * 2", function () {
shouldBeSame("clone $obj * 2", "(clone $obj) * 2");
});
it("clone $obj - 1 should be same as (clone $obj) - 1", function () {
shouldBeSame("clone $obj - 1", "(clone $obj) - 1");
});
it("clone $obj / 2 should be same as (clone $obj) / 2", function () {
shouldBeSame("clone $obj / 2", "(clone $obj) / 2");
});
it("clone $obj->prop + 1 should be same as (clone $obj->prop) + 1", function () {
shouldBeSame("clone $obj->prop + 1", "(clone $obj->prop) + 1");
});
it("clone $obj->method() * 2 should be same as (clone $obj->method()) * 2", function () {
shouldBeSame("clone $obj->method() * 2", "(clone $obj->method()) * 2");
});
it("clone $obj[0] + 1 should be same as (clone $obj[0]) + 1", function () {
shouldBeSame("clone $obj[0] + 1", "(clone $obj[0]) + 1");
});
it("-clone $obj should be same as -(clone $obj)", function () {
shouldBeSame("-clone $obj", "-(clone $obj)");
});
it("!clone $obj should be same as !(clone $obj)", function () {
shouldBeSame("!clone $obj", "!(clone $obj)");
});
});