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 {
|
for _, t := range toks {
|
||||||
startLine := line
|
startLine := line
|
||||||
endLine := startLine + strings.Count(t.Value, "\n")
|
endLine := startLine + strings.Count(strings.TrimSuffix(t.Value, "\n"), "\n")
|
||||||
drop := shouldDrop(t.Type, opts)
|
drop := shouldDrop(t.Type, opts)
|
||||||
if drop && cfg != nil && cfg.ShouldPreserveComment(t.Value) {
|
if drop && cfg != nil && cfg.ShouldPreserveComment(t.Value) {
|
||||||
drop = false
|
drop = false
|
||||||
|
|||||||
@@ -118,36 +118,11 @@ func (p *Processor) processGitChanges() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *Processor) processFileWithLineRanges(filename string, lineRanges []git.LineRange, cfg *config.Config) 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 {
|
if err != nil {
|
||||||
return err
|
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 p.cli.Backup {
|
||||||
if err := p.createBackup(filename); err != nil {
|
if err := p.createBackup(filename); err != nil {
|
||||||
return fmt.Errorf("failed to create backup: %v", err)
|
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
|
var inRange func(int) bool
|
||||||
modified := false
|
if len(lineRanges) > 0 {
|
||||||
var processedLines []string
|
inRange = func(n int) bool { return git.IsInLineRanges(n, lineRanges) }
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
out, changes, err := StripComments(src, filename, cfg, Options{
|
||||||
processedLines = append(processedLines, line)
|
Inline: p.cli.Inline,
|
||||||
}
|
Block: p.cli.Block,
|
||||||
} else {
|
PreserveLines: p.cli.PreserveLines,
|
||||||
processedLines = append(processedLines, line)
|
InRange: inRange,
|
||||||
}
|
})
|
||||||
}
|
|
||||||
|
|
||||||
if modified {
|
|
||||||
outFile, err := os.Create(filename)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer outFile.Close()
|
|
||||||
|
|
||||||
for _, line := range processedLines {
|
if changed(changes) {
|
||||||
if _, err := fmt.Fprintln(outFile, line); err != nil {
|
if err := os.WriteFile(filename, out, 0644); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if p.cli.Verbose {
|
if p.cli.Verbose {
|
||||||
fmt.Printf("✓ Comments removed from %s\n", filename)
|
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 {
|
func (p *Processor) processFileInMemory(filename string, language types.Language, cfg *config.Config) error {
|
||||||
|
src, err := os.ReadFile(filename)
|
||||||
file, err := os.Open(filename)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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 p.cli.Backup {
|
||||||
if err := p.createBackup(filename); err != nil {
|
if err := p.createBackup(filename); err != nil {
|
||||||
@@ -161,36 +149,19 @@ func (p *Processor) processFileInMemory(filename string, language types.Language
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
modified := false
|
out, changes, err := StripComments(src, filename, cfg, Options{
|
||||||
var processedLines []string
|
Inline: p.cli.Inline,
|
||||||
|
Block: p.cli.Block,
|
||||||
for _, line := range lines {
|
PreserveLines: p.cli.PreserveLines,
|
||||||
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)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer outFile.Close()
|
|
||||||
|
|
||||||
for _, line := range processedLines {
|
if changed(changes) {
|
||||||
if _, err := fmt.Fprintln(outFile, line); err != nil {
|
if err := os.WriteFile(filename, out, 0644); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if p.cli.Verbose {
|
if p.cli.Verbose {
|
||||||
fmt.Printf("✓ Comments removed from %s\n", filename)
|
fmt.Printf("✓ Comments removed from %s\n", filename)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user