MCPcopy Index your code
hub / github.com/github/gh-aw / removeJavaScriptCommentsFromLine

Function removeJavaScriptCommentsFromLine

pkg/workflow/js.go:100–206  ·  view source on GitHub ↗

removeJavaScriptCommentsFromLine removes JavaScript comments from a single line while preserving comments that appear within string literals and regex literals

(line string, inBlockComment *bool)

Source from the content-addressed store, hash-verified

98// removeJavaScriptCommentsFromLine removes JavaScript comments from a single line
99// while preserving comments that appear within string literals and regex literals
100func removeJavaScriptCommentsFromLine(line string, inBlockComment *bool) string {
101 var result strings.Builder
102 runes := []rune(line)
103 i := 0
104
105 for i < len(runes) {
106 if *inBlockComment {
107 // Look for end of block comment
108 if i < len(runes)-1 && runes[i] == '*' && runes[i+1] == '/' {
109 *inBlockComment = false
110 i += 2 // Skip '*/'
111 } else {
112 i++
113 }
114 continue
115 }
116
117 // Check for start of comments
118 if i < len(runes)-1 {
119 // Block comment start
120 if runes[i] == '/' && runes[i+1] == '*' {
121 *inBlockComment = true
122 i += 2 // Skip '/*'
123 continue
124 }
125 // Line comment start
126 if runes[i] == '/' && runes[i+1] == '/' {
127 // Check if we're inside a string literal or regex literal
128 beforeSlash := string(runes[:i])
129 if !isInsideStringLiteral(beforeSlash) && !isInsideRegexLiteral(beforeSlash) {
130 // Rest of line is a comment, stop processing
131 break
132 }
133 }
134 }
135
136 // Check for regex literals
137 if runes[i] == '/' {
138 beforeSlash := string(runes[:i])
139 if !isInsideStringLiteral(beforeSlash) && !isInsideRegexLiteral(beforeSlash) && canStartRegexLiteral(beforeSlash) {
140 // This is likely a regex literal
141 result.WriteRune(runes[i]) // Write the opening /
142 i++
143
144 // Process inside regex literal
145 for i < len(runes) {
146 if runes[i] == '/' {
147 // Check if it's escaped
148 escapeCount := 0
149 j := i - 1
150 for j >= 0 && runes[j] == '\\' {
151 escapeCount++
152 j--
153 }
154 if escapeCount%2 == 0 {
155 // Not escaped, end of regex
156 result.WriteRune(runes[i]) // Write the closing /
157 i++

Callers 2

removeJavaScriptCommentsFunction · 0.85

Calls 4

isInsideStringLiteralFunction · 0.85
isInsideRegexLiteralFunction · 0.85
canStartRegexLiteralFunction · 0.85
StringMethod · 0.45

Tested by 1