Skip to content

Commit d7b8b4c

Browse files
authored
Blink parity: preserve import indent in braced namespace; gate to 0.8% (#134)
1 parent e9abfc2 commit d7b8b4c

3 files changed

Lines changed: 24 additions & 5 deletions

File tree

‎.github/workflows/blink_parity_mautic.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ jobs:
9595
- name: Compare the two trees
9696
run: |
9797
# ratchet gate: fail once the differing share crosses this; lower it as parity improves
98-
MAX_DIFF_PERCENT=1.0
98+
MAX_DIFF_PERCENT=0.8
9999
total=0
100100
differ=0
101101
differing_files=""

‎blink/internal/fixer/rules/imports.go‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,18 @@ func (SingleLineAfterImports) Fix(s *tokens.Stream) bool {
215215
if next.Kind == token.Punct && next.Value == "}" {
216216
continue
217217
}
218-
if s.At(semi+1).Kind == token.Whitespace &&
219-
hasNewline(s.At(semi+1).Value) && s.At(semi+1).Value != "\n\n" {
220-
s.SetValue(semi+1, "\n\n")
221-
changed = true
218+
ws := s.At(semi + 1)
219+
if ws.Kind == token.Whitespace && hasNewline(ws.Value) {
220+
// preserve the import line's own indentation after the blank line, so a
221+
// braced-namespace body keeps its level; a whitespace that already holds
222+
// exactly one blank line (two newlines) is left untouched, as php-cs-fixer
223+
if strings.Count(ws.Value, "\n") != 2 {
224+
target := "\n\n" + indentBefore(s, i)
225+
if ws.Value != target {
226+
s.SetValue(semi+1, target)
227+
changed = true
228+
}
229+
}
222230
}
223231
}
224232
return changed

‎blink/internal/fixer/rules/rules_test.go‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,17 @@ func TestSingleLineAfterImports(t *testing.T) {
346346
if want := "<?php\nuse A;\nuse B;\n\nclass C {}"; !changed || got != want {
347347
t.Fatalf("changed=%v got=%q want=%q", changed, got, want)
348348
}
349+
350+
// inside a braced namespace the following line keeps its indentation
351+
got, changed = apply(t, SingleLineAfterImports{}, "<?php\nnamespace N {\n use A;\n class C {}\n}")
352+
if want := "<?php\nnamespace N {\n use A;\n\n class C {}\n}"; !changed || got != want {
353+
t.Fatalf("braced: changed=%v got=%q want=%q", changed, got, want)
354+
}
355+
356+
// a blank line that already exists is left untouched
357+
if _, changed := apply(t, SingleLineAfterImports{}, "<?php\nnamespace N {\n use A;\n\n class C {}\n}"); changed {
358+
t.Fatal("existing blank line after import must be a no-op")
359+
}
349360
}
350361

351362
func TestNoTrailingWhitespace(t *testing.T) {

0 commit comments

Comments
 (0)