Skip to content

Commit ecb99bc

Browse files
authored
fix: handle yieldfrom parenthesization in needsParens (#2466)
1 parent 6ce4e8a commit ecb99bc

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

src/needs-parens.mjs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ function needsParens(path, options) {
142142
return false;
143143
}
144144
}
145-
case "yield": {
145+
case "yield":
146+
case "yieldfrom": {
146147
switch (parent.kind) {
147148
case "propertylookup":
148149
case "nullsafepropertylookup":
@@ -154,8 +155,11 @@ function needsParens(path, options) {
154155
case "retif":
155156
return key === "test";
156157

158+
case "assign":
159+
return node.kind === "yield" && !!(node.key || node.value);
160+
157161
default:
158-
return !!(node.key || node.value);
162+
return node.kind === "yieldfrom" || !!(node.key || node.value);
159163
}
160164
}
161165
case "assign": {

tests/yield/__snapshots__/jsfmt.spec.mjs.snap

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Jest Snapshot v1, https://goo.gl/fbAQLP
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
22

33
exports[`yield.php 1`] = `
44
====================================options=====================================
@@ -64,6 +64,13 @@ function foo() {
6464
$test = 123;
6565
yield "bar {$test}" => 123;
6666
}
67+
function bar() {
68+
$x = yield from $gen;
69+
$cond ? yield from $gen : $fallback;
70+
foo(yield from $gen);
71+
(yield from $gen)->method();
72+
$a && yield from $gen;
73+
}
6774
6875
=====================================output=====================================
6976
<?php
@@ -131,6 +138,14 @@ function foo()
131138
$test = 123;
132139
yield "bar {$test}" => 123;
133140
}
141+
function bar()
142+
{
143+
$x = yield from $gen;
144+
$cond ? yield from $gen : $fallback;
145+
foo(yield from $gen);
146+
(yield from $gen)->method();
147+
$a && (yield from $gen);
148+
}
134149
135150
================================================================================
136151
`;

tests/yield/yield.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,10 @@ function foo() {
5656
$test = 123;
5757
yield "bar {$test}" => 123;
5858
}
59+
function bar() {
60+
$x = yield from $gen;
61+
$cond ? yield from $gen : $fallback;
62+
foo(yield from $gen);
63+
(yield from $gen)->method();
64+
$a && yield from $gen;
65+
}

0 commit comments

Comments
 (0)