feat(processor): route mutation paths through StripComments
This commit is contained in:
@@ -133,7 +133,7 @@ func StripComments(src []byte, filename string, cfg *config.Config, opts Options
|
||||
|
||||
for _, t := range toks {
|
||||
startLine := line
|
||||
endLine := startLine + strings.Count(t.Value, "\n")
|
||||
endLine := startLine + strings.Count(strings.TrimSuffix(t.Value, "\n"), "\n")
|
||||
drop := shouldDrop(t.Type, opts)
|
||||
if drop && cfg != nil && cfg.ShouldPreserveComment(t.Value) {
|
||||
drop = false
|
||||
|
||||
@@ -118,36 +118,11 @@ func (p *Processor) processGitChanges() error {
|
||||
}
|
||||
|
||||
func (p *Processor) processFileWithLineRanges(filename string, lineRanges []git.LineRange, cfg *config.Config) error {
|
||||
language, err := DetectLanguage(filename)
|
||||
src, err := os.ReadFile(filename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if p.cli.Verbose {
|
||||
fmt.Printf("Processing %s (language: %s)\n", filename, GetLanguageName(filename))
|
||||
if len(lineRanges) == 0 {
|
||||
fmt.Printf("Processing entire file (untracked)\n")
|
||||
} else {
|
||||
fmt.Printf("Processing %d line ranges\n", len(lineRanges))
|
||||
}
|
||||
}
|
||||
|
||||
file, err := os.Open(filename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
var lines []string
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
lines = append(lines, scanner.Text())
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if p.cli.Backup {
|
||||
if err := p.createBackup(filename); err != nil {
|
||||
return fmt.Errorf("failed to create backup: %v", err)
|
||||
@@ -157,44 +132,25 @@ func (p *Processor) processFileWithLineRanges(filename string, lineRanges []git.
|
||||
}
|
||||
}
|
||||
|
||||
processEntireFile := len(lineRanges) == 0
|
||||
modified := false
|
||||
var processedLines []string
|
||||
|
||||
for i, line := range lines {
|
||||
lineNum := i + 1
|
||||
shouldProcess := processEntireFile || git.IsInLineRanges(lineNum, lineRanges)
|
||||
|
||||
if shouldProcess {
|
||||
newLine := p.removeCommentsFromLine(line, language, cfg)
|
||||
if newLine != line {
|
||||
modified = true
|
||||
|
||||
if newLine != "" {
|
||||
processedLines = append(processedLines, newLine)
|
||||
var inRange func(int) bool
|
||||
if len(lineRanges) > 0 {
|
||||
inRange = func(n int) bool { return git.IsInLineRanges(n, lineRanges) }
|
||||
}
|
||||
|
||||
} else {
|
||||
processedLines = append(processedLines, line)
|
||||
}
|
||||
} else {
|
||||
processedLines = append(processedLines, line)
|
||||
}
|
||||
}
|
||||
|
||||
if modified {
|
||||
outFile, err := os.Create(filename)
|
||||
out, changes, err := StripComments(src, filename, cfg, Options{
|
||||
Inline: p.cli.Inline,
|
||||
Block: p.cli.Block,
|
||||
PreserveLines: p.cli.PreserveLines,
|
||||
InRange: inRange,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer outFile.Close()
|
||||
|
||||
for _, line := range processedLines {
|
||||
if _, err := fmt.Fprintln(outFile, line); err != nil {
|
||||
if changed(changes) {
|
||||
if err := os.WriteFile(filename, out, 0644); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if p.cli.Verbose {
|
||||
fmt.Printf("✓ Comments removed from %s\n", filename)
|
||||
}
|
||||
|
||||
@@ -135,22 +135,10 @@ func (p *Processor) processFile(filename string, cfg *config.Config) error {
|
||||
}
|
||||
|
||||
func (p *Processor) processFileInMemory(filename string, language types.Language, cfg *config.Config) error {
|
||||
|
||||
file, err := os.Open(filename)
|
||||
src, err := os.ReadFile(filename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
var lines []string
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
lines = append(lines, scanner.Text())
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if p.cli.Backup {
|
||||
if err := p.createBackup(filename); err != nil {
|
||||
@@ -161,36 +149,19 @@ func (p *Processor) processFileInMemory(filename string, language types.Language
|
||||
}
|
||||
}
|
||||
|
||||
modified := false
|
||||
var processedLines []string
|
||||
|
||||
for _, line := range lines {
|
||||
newLine := p.removeCommentsFromLine(line, language, cfg)
|
||||
if newLine != line {
|
||||
modified = true
|
||||
|
||||
if newLine != "" {
|
||||
processedLines = append(processedLines, newLine)
|
||||
}
|
||||
|
||||
} else {
|
||||
processedLines = append(processedLines, line)
|
||||
}
|
||||
}
|
||||
|
||||
if modified {
|
||||
outFile, err := os.Create(filename)
|
||||
out, changes, err := StripComments(src, filename, cfg, Options{
|
||||
Inline: p.cli.Inline,
|
||||
Block: p.cli.Block,
|
||||
PreserveLines: p.cli.PreserveLines,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer outFile.Close()
|
||||
|
||||
for _, line := range processedLines {
|
||||
if _, err := fmt.Fprintln(outFile, line); err != nil {
|
||||
if changed(changes) {
|
||||
if err := os.WriteFile(filename, out, 0644); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if p.cli.Verbose {
|
||||
fmt.Printf("✓ Comments removed from %s\n", filename)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user