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
12 changes: 12 additions & 0 deletions src/needs-parens.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,18 @@ function needsParens(path, options) {
return node.kind === "yieldfrom" || !!(node.key || node.value);
}
}
case "match":
// `match (...) {...}()` and the like are syntax errors
switch (parent.kind) {
case "propertylookup":
case "nullsafepropertylookup":
case "staticlookup":
case "offsetlookup":
case "call":
return key === "what";
default:
return false;
}
case "assign": {
if (
parent.kind === "for" &&
Expand Down
114 changes: 114 additions & 0 deletions tests/match/__snapshots__/jsfmt.spec.mjs.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,119 @@
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing

exports[`dereference.php 1`] = `
====================================options=====================================
parsers: ["php"]
phpVersion: "8.0"
printWidth: 80
trailingCommaPHP: false
| printWidth
=====================================input======================================
<?php
$handler = (match ($kind) {
'a' => fn() => 1,
default => fn() => 2,
})();
$first = (match ($kind) { default => [1, 2] })[0];
$name = (match ($kind) { default => $object })->name;
$value = (match ($kind) { default => $object })?->name;
$result = (match ($kind) { default => $object })->handle();
$constant = (match ($kind) { default => Foo::class })::BAR;
$property = (match ($kind) { default => Foo::class })::$bar;
$instance = (match ($kind) { default => Foo::class })::create();
$plain = match ($kind) { default => 1 } + 1;

=====================================output=====================================
<?php
$handler = (match ($kind) {
"a" => fn() => 1,
default => fn() => 2
})();
$first = (match ($kind) {
default => [1, 2]
})[0];
$name = (match ($kind) {
default => $object
})->name;
$value = (match ($kind) {
default => $object
})?->name;
$result = (match ($kind) {
default => $object
})->handle();
$constant = (match ($kind) {
default => Foo::class
})::BAR;
$property = (match ($kind) {
default => Foo::class
})::$bar;
$instance = (match ($kind) {
default => Foo::class
})::create();
$plain =
match ($kind) {
default => 1
} + 1;

================================================================================
`;

exports[`dereference.php 2`] = `
====================================options=====================================
parsers: ["php"]
phpVersion: "8.0"
printWidth: 80
trailingCommaPHP: true
| printWidth
=====================================input======================================
<?php
$handler = (match ($kind) {
'a' => fn() => 1,
default => fn() => 2,
})();
$first = (match ($kind) { default => [1, 2] })[0];
$name = (match ($kind) { default => $object })->name;
$value = (match ($kind) { default => $object })?->name;
$result = (match ($kind) { default => $object })->handle();
$constant = (match ($kind) { default => Foo::class })::BAR;
$property = (match ($kind) { default => Foo::class })::$bar;
$instance = (match ($kind) { default => Foo::class })::create();
$plain = match ($kind) { default => 1 } + 1;

=====================================output=====================================
<?php
$handler = (match ($kind) {
"a" => fn() => 1,
default => fn() => 2,
})();
$first = (match ($kind) {
default => [1, 2],
})[0];
$name = (match ($kind) {
default => $object,
})->name;
$value = (match ($kind) {
default => $object,
})?->name;
$result = (match ($kind) {
default => $object,
})->handle();
$constant = (match ($kind) {
default => Foo::class,
})::BAR;
$property = (match ($kind) {
default => Foo::class,
})::$bar;
$instance = (match ($kind) {
default => Foo::class,
})::create();
$plain =
match ($kind) {
default => 1,
} + 1;

================================================================================
`;

exports[`match.php 1`] = `
====================================options=====================================
parsers: ["php"]
Expand Down
13 changes: 13 additions & 0 deletions tests/match/dereference.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
$handler = (match ($kind) {
'a' => fn() => 1,
default => fn() => 2,
})();
$first = (match ($kind) { default => [1, 2] })[0];
$name = (match ($kind) { default => $object })->name;
$value = (match ($kind) { default => $object })?->name;
$result = (match ($kind) { default => $object })->handle();
$constant = (match ($kind) { default => Foo::class })::BAR;
$property = (match ($kind) { default => Foo::class })::$bar;
$instance = (match ($kind) { default => Foo::class })::create();
$plain = match ($kind) { default => 1 } + 1;