fix(processor): implement universal string-aware comment parsing
- Fix block comment start detection to use string-aware parsing - Fix block comment end detection to use string-aware parsing - Fix lineHasComment to use string-aware parsing instead of strings.Contains - Prevents removal of comment markers inside string literals - Protects all supported languages: Go, JS, Python, Lua, etc.
This commit is contained in:
@@ -58,7 +58,6 @@ func (p *Processor) processGitChanges() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
supportedChanges := make([]git.FileChange, 0, len(changes))
|
supportedChanges := make([]git.FileChange, 0, len(changes))
|
||||||
for _, change := range changes {
|
for _, change := range changes {
|
||||||
if IsSupportedFile(change.Path) {
|
if IsSupportedFile(change.Path) {
|
||||||
@@ -112,7 +111,6 @@ func (p *Processor) processFileWithLineRanges(filename string, lineRanges []git.
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
cfg, _, err := config.Load()
|
cfg, _, err := config.Load()
|
||||||
if err != nil && p.cli.Verbose {
|
if err != nil && p.cli.Verbose {
|
||||||
fmt.Printf("Warning: failed to load config, using defaults: %v\n", err)
|
fmt.Printf("Warning: failed to load config, using defaults: %v\n", err)
|
||||||
@@ -211,12 +209,11 @@ func (p *Processor) removeCommentsFromLine(line string, language types.Language,
|
|||||||
if idx := p.findCommentIndex(result, language.LineComment); idx != -1 {
|
if idx := p.findCommentIndex(result, language.LineComment); idx != -1 {
|
||||||
|
|
||||||
comment := strings.TrimSpace(result[idx:])
|
comment := strings.TrimSpace(result[idx:])
|
||||||
|
|
||||||
|
|
||||||
if cfg.ShouldPreserveComment(comment) {
|
if cfg.ShouldPreserveComment(comment) {
|
||||||
return originalLine
|
return originalLine
|
||||||
}
|
}
|
||||||
|
|
||||||
result = result[:idx]
|
result = result[:idx]
|
||||||
hasChanges = true
|
hasChanges = true
|
||||||
}
|
}
|
||||||
@@ -228,26 +225,25 @@ func (p *Processor) removeCommentsFromLine(line string, language types.Language,
|
|||||||
endComment := language.BlockComment.End
|
endComment := language.BlockComment.End
|
||||||
|
|
||||||
for {
|
for {
|
||||||
startIdx := strings.Index(result, startComment)
|
startIdx := p.findCommentIndex(result, startComment)
|
||||||
if startIdx == -1 {
|
if startIdx == -1 {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
endIdx := strings.Index(result[startIdx:], endComment)
|
endIdx := p.findCommentIndex(result[startIdx:], endComment)
|
||||||
if endIdx == -1 {
|
if endIdx == -1 {
|
||||||
|
|
||||||
comment := strings.TrimSpace(result[startIdx:])
|
comment := strings.TrimSpace(result[startIdx:])
|
||||||
if cfg.ShouldPreserveComment(comment) {
|
if cfg.ShouldPreserveComment(comment) {
|
||||||
return originalLine
|
return originalLine
|
||||||
}
|
}
|
||||||
|
|
||||||
result = result[:startIdx]
|
result = result[:startIdx]
|
||||||
hasChanges = true
|
hasChanges = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
|
blockComment := strings.TrimSpace(result[startIdx : startIdx+endIdx+len(endComment)])
|
||||||
blockComment := strings.TrimSpace(result[startIdx:startIdx+endIdx+len(endComment)])
|
|
||||||
if cfg.ShouldPreserveComment(blockComment) {
|
if cfg.ShouldPreserveComment(blockComment) {
|
||||||
return originalLine
|
return originalLine
|
||||||
}
|
}
|
||||||
@@ -260,7 +256,7 @@ func (p *Processor) removeCommentsFromLine(line string, language types.Language,
|
|||||||
|
|
||||||
if hasChanges {
|
if hasChanges {
|
||||||
result = strings.TrimRight(result, " \t")
|
result = strings.TrimRight(result, " \t")
|
||||||
|
|
||||||
if strings.TrimSpace(result) == "" {
|
if strings.TrimSpace(result) == "" {
|
||||||
if p.cli.PreserveLines {
|
if p.cli.PreserveLines {
|
||||||
leadingWhitespace := ""
|
leadingWhitespace := ""
|
||||||
@@ -287,7 +283,6 @@ func (p *Processor) showGitPreviewWithTotals(filename string, lineRanges []git.L
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
cfg, _, err := config.Load()
|
cfg, _, err := config.Load()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cfg = config.Default()
|
cfg = config.Default()
|
||||||
@@ -374,21 +369,19 @@ func (p *Processor) showGitPreviewWithTotals(filename string, lineRanges []git.L
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (p *Processor) findCommentIndex(line, commentMarker string) int {
|
func (p *Processor) findCommentIndex(line, commentMarker string) int {
|
||||||
inString := false
|
inString := false
|
||||||
var stringChar byte
|
var stringChar byte
|
||||||
|
|
||||||
for i := 0; i < len(line); i++ {
|
for i := 0; i < len(line); i++ {
|
||||||
char := line[i]
|
char := line[i]
|
||||||
|
|
||||||
|
|
||||||
if !inString && (char == '"' || char == '\'' || char == '`') {
|
if !inString && (char == '"' || char == '\'' || char == '`') {
|
||||||
inString = true
|
inString = true
|
||||||
stringChar = char
|
stringChar = char
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if inString && char == stringChar {
|
if inString && char == stringChar {
|
||||||
|
|
||||||
if i > 0 && line[i-1] != '\\' {
|
if i > 0 && line[i-1] != '\\' {
|
||||||
@@ -396,7 +389,6 @@ func (p *Processor) findCommentIndex(line, commentMarker string) int {
|
|||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if !inString {
|
if !inString {
|
||||||
if i+len(commentMarker) <= len(line) && line[i:i+len(commentMarker)] == commentMarker {
|
if i+len(commentMarker) <= len(line) && line[i:i+len(commentMarker)] == commentMarker {
|
||||||
@@ -404,7 +396,7 @@ func (p *Processor) findCommentIndex(line, commentMarker string) int {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -414,14 +406,13 @@ func (p *Processor) lineHasComment(line string, language types.Language) bool {
|
|||||||
if language.LineComment != "" && p.findCommentIndex(line, language.LineComment) != -1 {
|
if language.LineComment != "" && p.findCommentIndex(line, language.LineComment) != -1 {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if language.BlockComment != nil {
|
if language.BlockComment != nil {
|
||||||
if strings.Contains(line, language.BlockComment.Start) || strings.Contains(line, language.BlockComment.End) {
|
if p.findCommentIndex(line, language.BlockComment.Start) != -1 || p.findCommentIndex(line, language.BlockComment.End) != -1 {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user