Skip to content
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
9 changes: 8 additions & 1 deletion blink/internal/fixer/rules/array_indentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,14 @@ func isArrayLiteralOpen(s *tokens.Stream, open int) bool {
return false
case token.Punct:
switch t.Value {
case ")", "]", "}":
case ")":
// a cast before "[" ("(object) [...]") opens an array literal, not an
// access on a call result
if o := s.MatchBackward(p); o >= 0 && isCastParen(s, o) {
return true
}
return false
case "]", "}":
return false
}
}
Expand Down
3 changes: 3 additions & 0 deletions blink/internal/fixer/rules/array_list_item_newline.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ func (ArrayListItemNewline) Fix(s *tokens.Stream) bool {
if !isArrayLiteralOpen(s, open) {
continue
}
if isDestructuringAssignOpen(s, open) {
continue // "[$a, $b] = ..." is a destructuring target, not an array literal
}
closeIdx := s.MatchForward(open)
if closeIdx < 0 || sigNext(s, open) == closeIdx {
continue // empty []
Expand Down
11 changes: 11 additions & 0 deletions blink/internal/fixer/rules/array_list_item_newline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,15 @@ func TestArrayListItemNewline(t *testing.T) {
if _, changed := apply(t, f, "<?php\n$a = [\n 'x' => 1,\n];"); changed {
t.Fatal("already-multiline array must be a no-op")
}

// a cast before "[" opens an array literal, so it still splits
got, changed = apply(t, f, "<?php\n$a = (object) ['x' => 1, 'y' => 2];")
if want := "<?php\n$a = (object) [\n 'x' => 1,\n 'y' => 2,\n];"; !changed || got != want {
t.Fatalf("cast: changed=%v got=%q", changed, got)
}

// an index access on a call result is not an array literal, so it is left alone
if _, changed := apply(t, f, "<?php\n$a = foo()['x'];"); changed {
t.Fatal("call-result index access must stay inline")
}
}
3 changes: 3 additions & 0 deletions blink/internal/fixer/rules/array_opener_and_closer_newline.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ func (ArrayOpenerAndCloserNewline) Fix(s *tokens.Stream) bool {
if !isArrayLiteralOpen(s, open) {
continue
}
if isDestructuringAssignOpen(s, open) {
continue // destructuring target, not an array literal
}
closeIdx := s.MatchForward(open)
if closeIdx < 0 {
continue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ func (StandaloneLineInMultilineArray) Fix(s *tokens.Stream) bool {
if !isArrayLiteralOpen(s, open) {
continue
}
if isDestructuringAssignOpen(s, open) {
continue // destructuring target, not an array literal
}
closeIdx := s.MatchForward(open)
if closeIdx < 0 || sigNext(s, open) == closeIdx {
continue // empty []
Expand Down
Loading