Skip to content

Commit ebefa6a

Browse files
committed
fix: instanceof precedence + new tests
1 parent d7180f5 commit ebefa6a

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

src/ast.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,11 @@ AST.precedence = {};
147147
["==", "!=", "===", "!==", /* '<>', */ "<=>"],
148148
["<", "<=", ">", ">="],
149149
["<<", ">>"],
150-
["instanceof"],
151150
["+", "-", "."],
152151
["*", "/", "%"],
153-
["!", "u-", "u+", "u~"], // u- etc. are unary variants; higher than * so -20*5 parses as (-20)*5
152+
["!"],
153+
["instanceof"],
154+
["u-", "u+", "u~"],
154155
["cast", "silent"],
155156
["**"],
156157
// TODO: [ (array)

test/precedence.test.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ describe("Test precedence", function () {
6161
});
6262
it("test instanceof", function () {
6363
shouldBeSame("$a instanceof $b && $c", "($a instanceof $b) && $c");
64+
shouldBeSame("$a + $b instanceof $c", "$a + ($b instanceof $c)");
65+
shouldBeSame("$a * $b instanceof $c", "$a * ($b instanceof $c)");
66+
shouldBeSame("-$a instanceof $b", "(-$a) instanceof $b");
67+
shouldBeSame("+$a instanceof $b", "(+$a) instanceof $b");
68+
shouldBeSame("~$a instanceof $b", "(~$a) instanceof $b");
6469
});
6570
it("test <<", function () {
6671
shouldBeSame("1 + 3 << 5", "(1 + 3) << 5");
@@ -119,11 +124,15 @@ describe("Test precedence", function () {
119124
shouldBeSame("5 AND 4 + 3", "5 AND (4 + 3)");
120125
});
121126
it("test unary : !", function () {
122-
shouldBeSame("!$a instanceof $b", "(!$a) instanceof $b");
123-
shouldBeSame("!$a + $b instanceof $c", "((!$a) + $b) instanceof $c");
127+
shouldBeSame("!$a instanceof $b", "!($a instanceof $b)");
128+
shouldBeSame("!$a + $b instanceof $c", "(!$a) + ($b instanceof $c)");
124129
shouldBeSame("6 + !4 + 5", "6 + (!4) + 5");
125130
shouldBeSame("if($a && !$b) {}", "if($a && (!$b)) {}");
126131
});
132+
it("test unary : - (prettier/plugin-php#2501)", function () {
133+
shouldBeSame("5 * -1 + 2", "(5 * (-1)) + 2");
134+
shouldBeSame('5 * -1 . "foo"', '(5 * (-1)) . "foo"');
135+
});
127136
it("test concat", function () {
128137
shouldBeSame('"a"."b"."c"."d"', '((("a"."b")."c")."d")');
129138
});

0 commit comments

Comments
 (0)