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
4 changes: 1 addition & 3 deletions blink/internal/fixer/rules/array_notation.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ func convertLongArray(s *tokens.Stream, name string) bool {
}
s.SetValue(closeIdx, "]")
s.SetValue(j, "[")
for k := j - 1; k >= i; k-- {
s.RemoveAt(k)
}
s.ReplaceRange(i, j-1, nil)
changed = true
}
return changed
Expand Down
4 changes: 1 addition & 3 deletions blink/internal/fixer/rules/control_structure_braces.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,7 @@ func csbIsAltSyntax(s *tokens.Stream, controlIndex int) bool {

// csbInsert inserts toks at pos, preserving their order.
func csbInsert(s *tokens.Stream, pos int, toks []token.Token) {
for i, t := range toks {
s.InsertAt(pos+i, t)
}
s.InsertSliceAt(pos, toks)
}

// csbFindParenthesisEnd returns the ")" index of the control's condition, or the
Expand Down
4 changes: 1 addition & 3 deletions blink/internal/fixer/rules/gen2_ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ func (AssignNullCoalescingToCoalesceEqual) Fix(s *tokens.Stream) bool {
// `= $a ??` becomes `??=`: retag the `=` and drop the duplicate operand
// and operator.
s.SetValue(i, "??=")
for k := opIdx; k >= i+1; k-- {
s.RemoveAt(k)
}
s.ReplaceRange(i+1, opIdx, nil)
changed = true
}
return changed
Expand Down
4 changes: 1 addition & 3 deletions blink/internal/fixer/rules/gen3_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,7 @@ func nullableTypeRemove(s *tokens.Stream, start, end int, hasNullable bool) bool
} else {
return false
}
for j := hi; j >= lo; j-- {
s.RemoveAt(j)
}
s.ReplaceRange(lo, hi, nil)
return true
}

Expand Down
4 changes: 1 addition & 3 deletions blink/internal/fixer/rules/gen5_lang.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,7 @@ func (EmptyLoopBody) Fix(s *tokens.Stream) bool {
// Replace "{" with ";" and drop the "}" plus the whitespace-only interior,
// so "{ }" collapses cleanly to ";".
s.SetValue(braceOpen, ";")
for k := braceClose; k > braceOpen; k-- {
s.RemoveAt(k)
}
s.ReplaceRange(braceOpen+1, braceClose, nil)
changed = true
}
return changed
Expand Down
4 changes: 1 addition & 3 deletions blink/internal/fixer/rules/gen_imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ func removeUnneededAliases(s *tokens.Stream, from, semi int) bool {
if seg > from && alias < end &&
s.At(seg).Kind == token.Ident && s.At(alias).Kind == token.Ident &&
s.At(seg).Value == s.At(alias).Value {
for r := alias; r >= seg+1; r-- {
s.RemoveAt(r)
}
s.ReplaceRange(seg+1, alias, nil)
end -= alias - seg
k = seg + 1
changed = true
Expand Down
12 changes: 3 additions & 9 deletions blink/internal/fixer/rules/gen_ops_inc.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,7 @@ func (StandardizeIncrement) Fix(s *tokens.Stream) bool {

// Drop `<op> 1` (operator..number), then the inline gap before it, then
// prepend the increment operator at the chain root.
for k := numIdx; k >= i; k-- {
s.RemoveAt(k)
}
s.ReplaceRange(i, numIdx, nil)
if lv+1 < s.Len() && s.At(lv+1).Kind == token.Whitespace && !hasNewline(s.At(lv+1).Value) {
s.RemoveAt(lv + 1)
}
Expand Down Expand Up @@ -322,9 +320,7 @@ func (IncrementStyle) Fix(s *tokens.Stream) bool {
}

// Move the operator in front of the variable, dropping the inline gap.
for k := i; k >= lv+1; k-- {
s.RemoveAt(k)
}
s.ReplaceRange(lv+1, i, nil)
s.InsertAt(lv, token.Token{Kind: token.Punct, Value: t.Value})
changed = true
i = lv
Expand Down Expand Up @@ -420,9 +416,7 @@ func (LongToShorthandOperator) Fix(s *tokens.Stream) bool {
// `= $a <op>` becomes `<op>=`: retag the `=` and drop the duplicate
// operand and operator.
s.SetValue(i, s.At(opIdx).Value+"=")
for k := opIdx; k >= i+1; k-- {
s.RemoveAt(k)
}
s.ReplaceRange(i+1, opIdx, nil)
changed = true
}
return changed
Expand Down
4 changes: 1 addition & 3 deletions blink/internal/fixer/rules/imports_unused.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,9 +360,7 @@ func matchCloseBrace(s *tokens.Stream, open int) int {
}

func removeImport(s *tokens.Stream, im importInfo) {
for r := im.semi; r >= im.start; r-- {
s.RemoveAt(r)
}
s.ReplaceRange(im.start, im.semi, nil)
// drop just the statement's own line break, keeping any blank line
if im.start < s.Len() && s.At(im.start).Kind == token.Whitespace {
if v := s.At(im.start).Value; strings.HasPrefix(v, "\n") {
Expand Down
4 changes: 1 addition & 3 deletions blink/internal/fixer/rules/modifier_keywords.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,7 @@ func orderModifierKeywords(s *tokens.Stream, m int) bool {
repl = append(repl, token.Token{Kind: token.Whitespace, Value: " "})
}
if after == m {
for _, tk := range slices.Backward(repl) {
s.InsertAt(m, tk)
}
s.InsertSliceAt(m, repl)
return true
}
s.ReplaceRange(m, after-1, repl)
Expand Down
5 changes: 1 addition & 4 deletions blink/internal/fixer/rules/no_alternative_syntax.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,7 @@ func nasAddBraces(s *tokens.Stream, keyword token.Token, index, colonIndex int)

// nasReplace replaces the single token at pos with items, preserving order.
func nasReplace(s *tokens.Stream, pos int, items []token.Token) {
s.RemoveAt(pos)
for i, t := range items {
s.InsertAt(pos+i, t)
}
s.ReplaceRange(pos, pos, items)
}

// nasNextParen returns the index of the first "(" after index, or -1.
Expand Down
7 changes: 2 additions & 5 deletions blink/internal/fixer/rules/operator_linebreak.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,8 @@ func moveOperatorToLineStart(s *tokens.Stream, opIndices []int, nextM int) {
s.SetValue(idx, "")
}

at := nextM
for _, c := range clones {
s.InsertAt(at, c)
at++
}
s.InsertSliceAt(nextM, clones)
at := nextM + len(clones)
if hadSpaceBefore {
s.InsertAt(at, token.Token{Kind: token.Whitespace, Value: " "})
}
Expand Down
6 changes: 6 additions & 0 deletions blink/internal/tokens/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ func (s *Stream) InsertAt(i int, t token.Token) {
s.toks = slices.Insert(s.toks, i, t)
}

// InsertSliceAt inserts ts at index i in one splice, so the tail shifts once
// instead of once per element.
func (s *Stream) InsertSliceAt(i int, ts []token.Token) {
s.toks = slices.Insert(s.toks, i, ts...)
}

// MatchForward returns the index of the delimiter matching the opener at i
// ("(", "{" or "["), or -1 if there is none. Strings and comments are single
// tokens, so scanning punctuation is safe.
Expand Down
Loading