๐Ÿš€ Grammar-Aware Code Formatter: Structure through separation (supports Go, JavaScript, TypeScript, JSX, and TSX)
go formatter code-formatter javascript typescript jsx tsx

fix(formatter): Handle backticks inside strings and character literals

fuwn.net be453569 edc1037b

verified
+58 -1
+58 -1
formatter.go
··· 34 34 return len(trimmed) > 8 && trimmed[:8] == "package " 35 35 } 36 36 37 + func countRawStringDelimiters(line string) int { 38 + count := 0 39 + inString := false 40 + inCharacter := false 41 + 42 + for index := 0; index < len(line); index++ { 43 + character := line[index] 44 + 45 + if inCharacter { 46 + if character == '\\' && index+1 < len(line) { 47 + index++ 48 + 49 + continue 50 + } 51 + 52 + if character == '\'' { 53 + inCharacter = false 54 + } 55 + 56 + continue 57 + } 58 + 59 + if inString { 60 + if character == '\\' && index+1 < len(line) { 61 + index++ 62 + 63 + continue 64 + } 65 + 66 + if character == '"' { 67 + inString = false 68 + } 69 + 70 + continue 71 + } 72 + 73 + if character == '\'' { 74 + inCharacter = true 75 + 76 + continue 77 + } 78 + 79 + if character == '"' { 80 + inString = true 81 + 82 + continue 83 + } 84 + 85 + if character == '`' { 86 + count++ 87 + } 88 + } 89 + 90 + return count 91 + } 92 + 37 93 type CommentMode int 38 94 39 95 const ( ··· 205 261 insideRawString := false 206 262 207 263 for index, line := range lines { 208 - backtickCount := strings.Count(line, "`") 264 + backtickCount := countRawStringDelimiters(line) 209 265 wasInsideRawString := insideRawString 210 266 211 267 if backtickCount%2 == 1 { ··· 214 270 215 271 if wasInsideRawString { 216 272 result = append(result, line) 273 + 217 274 continue 218 275 } 219 276