|
| 1 | +package no_nonoctal_decimal_escape |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/microsoft/typescript-go/shim/ast" |
| 7 | + "github.com/microsoft/typescript-go/shim/core" |
| 8 | + "github.com/web-infra-dev/rslint/internal/rule" |
| 9 | + "github.com/web-infra-dev/rslint/internal/utils" |
| 10 | +) |
| 11 | + |
| 12 | +// https://eslint.org/docs/latest/rules/no-nonoctal-decimal-escape |
| 13 | +var NoNonoctalDecimalEscapeRule = rule.Rule{ |
| 14 | + Name: "no-nonoctal-decimal-escape", |
| 15 | + Run: func(ctx rule.RuleContext, options any) rule.RuleListeners { |
| 16 | + return rule.RuleListeners{ |
| 17 | + ast.KindStringLiteral: func(node *ast.Node) { |
| 18 | + trimmedRange := utils.TrimNodeTextRange(ctx.SourceFile, node) |
| 19 | + rawStart := trimmedRange.Pos() |
| 20 | + raw := ctx.SourceFile.Text()[rawStart:trimmedRange.End()] |
| 21 | + |
| 22 | + if !containsBackslashDigit89(raw) { |
| 23 | + return |
| 24 | + } |
| 25 | + |
| 26 | + for _, hit := range scanDecimalEscapes(raw) { |
| 27 | + reportDecimalEscape(ctx, rawStart, hit) |
| 28 | + } |
| 29 | + }, |
| 30 | + } |
| 31 | + }, |
| 32 | +} |
| 33 | + |
| 34 | +// decimalEscapeHit describes a single \8 or \9 occurrence found in the raw |
| 35 | +// source text of a string literal, plus the immediately preceding `\X` escape |
| 36 | +// pair (used to detect the special "\0\8" / "\0\9" case). |
| 37 | +type decimalEscapeHit struct { |
| 38 | + previousEscape string |
| 39 | + previousEscapeStart int |
| 40 | + decimalEscapeStart int |
| 41 | + decimalEscapeEnd int |
| 42 | + decimalEscape string |
| 43 | +} |
| 44 | + |
| 45 | +func containsBackslashDigit89(raw string) bool { |
| 46 | + for i := 0; i+1 < len(raw); i++ { |
| 47 | + if raw[i] == '\\' && (raw[i+1] == '8' || raw[i+1] == '9') { |
| 48 | + return true |
| 49 | + } |
| 50 | + } |
| 51 | + return false |
| 52 | +} |
| 53 | + |
| 54 | +// scanDecimalEscapes walks raw source text and returns every \8 / \9 hit. |
| 55 | +// Mirrors ESLint's regex |
| 56 | +// |
| 57 | +// (?:[^\\]|(?<previousEscape>\\.))*?(?<decimalEscape>\\[89]) |
| 58 | +// |
| 59 | +// where previousEscape captures the LAST `\X` pair before the decimal escape. |
| 60 | +// In ESLint's lazy match, that capture only survives when the preceding `\X` |
| 61 | +// is immediately adjacent to the decimal escape — any unescaped character |
| 62 | +// between them clears it. We replicate that by resetting `previousEscape` |
| 63 | +// whenever a non-backslash byte is consumed. |
| 64 | +func scanDecimalEscapes(raw string) []decimalEscapeHit { |
| 65 | + var hits []decimalEscapeHit |
| 66 | + previousEscape := "" |
| 67 | + previousEscapeStart := -1 |
| 68 | + n := len(raw) |
| 69 | + for i := 0; i < n; { |
| 70 | + if raw[i] != '\\' { |
| 71 | + i++ |
| 72 | + previousEscape = "" |
| 73 | + previousEscapeStart = -1 |
| 74 | + continue |
| 75 | + } |
| 76 | + if i+1 >= n { |
| 77 | + break |
| 78 | + } |
| 79 | + next := raw[i+1] |
| 80 | + if next == '8' || next == '9' { |
| 81 | + hits = append(hits, decimalEscapeHit{ |
| 82 | + previousEscape: previousEscape, |
| 83 | + previousEscapeStart: previousEscapeStart, |
| 84 | + decimalEscapeStart: i, |
| 85 | + decimalEscapeEnd: i + 2, |
| 86 | + decimalEscape: raw[i : i+2], |
| 87 | + }) |
| 88 | + i += 2 |
| 89 | + previousEscape = "" |
| 90 | + previousEscapeStart = -1 |
| 91 | + continue |
| 92 | + } |
| 93 | + previousEscape = raw[i : i+2] |
| 94 | + previousEscapeStart = i |
| 95 | + i += 2 |
| 96 | + } |
| 97 | + return hits |
| 98 | +} |
| 99 | + |
| 100 | +func reportDecimalEscape(ctx rule.RuleContext, rawStart int, hit decimalEscapeHit) { |
| 101 | + decimalEscapeStartAbs := rawStart + hit.decimalEscapeStart |
| 102 | + decimalEscapeEndAbs := rawStart + hit.decimalEscapeEnd |
| 103 | + digit := hit.decimalEscape[1:] |
| 104 | + |
| 105 | + suggestions := make([]rule.RuleSuggestion, 0, 3) |
| 106 | + |
| 107 | + if hit.previousEscape == "\\0" { |
| 108 | + // "\0\X" — replacing with "\0X" would create a legacy octal escape, so |
| 109 | + // the rule offers two alternative refactors instead of the single one. |
| 110 | + previousEscapeStartAbs := rawStart + hit.previousEscapeStart |
| 111 | + nullEscape := unicodeEscape(0) |
| 112 | + combined := nullEscape + digit |
| 113 | + suggestions = append(suggestions, rule.RuleSuggestion{ |
| 114 | + Message: rule.RuleMessage{ |
| 115 | + Id: "refactor", |
| 116 | + Description: refactorMessage("\\0"+hit.decimalEscape, combined), |
| 117 | + }, |
| 118 | + FixesArr: []rule.RuleFix{ |
| 119 | + rule.RuleFixReplaceRange( |
| 120 | + core.NewTextRange(previousEscapeStartAbs, decimalEscapeEndAbs), |
| 121 | + combined, |
| 122 | + ), |
| 123 | + }, |
| 124 | + }) |
| 125 | + digitUnicode := unicodeEscape(rune(digit[0])) |
| 126 | + suggestions = append(suggestions, rule.RuleSuggestion{ |
| 127 | + Message: rule.RuleMessage{ |
| 128 | + Id: "refactor", |
| 129 | + Description: refactorMessage(hit.decimalEscape, digitUnicode), |
| 130 | + }, |
| 131 | + FixesArr: []rule.RuleFix{ |
| 132 | + rule.RuleFixReplaceRange( |
| 133 | + core.NewTextRange(decimalEscapeStartAbs, decimalEscapeEndAbs), |
| 134 | + digitUnicode, |
| 135 | + ), |
| 136 | + }, |
| 137 | + }) |
| 138 | + } else { |
| 139 | + suggestions = append(suggestions, rule.RuleSuggestion{ |
| 140 | + Message: rule.RuleMessage{ |
| 141 | + Id: "refactor", |
| 142 | + Description: refactorMessage(hit.decimalEscape, digit), |
| 143 | + }, |
| 144 | + FixesArr: []rule.RuleFix{ |
| 145 | + rule.RuleFixReplaceRange( |
| 146 | + core.NewTextRange(decimalEscapeStartAbs, decimalEscapeEndAbs), |
| 147 | + digit, |
| 148 | + ), |
| 149 | + }, |
| 150 | + }) |
| 151 | + } |
| 152 | + |
| 153 | + escaped := "\\" + hit.decimalEscape |
| 154 | + suggestions = append(suggestions, rule.RuleSuggestion{ |
| 155 | + Message: rule.RuleMessage{ |
| 156 | + Id: "escapeBackslash", |
| 157 | + Description: escapeBackslashMessage(hit.decimalEscape, escaped), |
| 158 | + }, |
| 159 | + FixesArr: []rule.RuleFix{ |
| 160 | + rule.RuleFixReplaceRange( |
| 161 | + core.NewTextRange(decimalEscapeStartAbs, decimalEscapeEndAbs), |
| 162 | + escaped, |
| 163 | + ), |
| 164 | + }, |
| 165 | + }) |
| 166 | + |
| 167 | + ctx.ReportRangeWithSuggestions( |
| 168 | + core.NewTextRange(decimalEscapeStartAbs, decimalEscapeEndAbs), |
| 169 | + rule.RuleMessage{ |
| 170 | + Id: "decimalEscape", |
| 171 | + Description: fmt.Sprintf("Don't use '%s' escape sequence.", hit.decimalEscape), |
| 172 | + }, |
| 173 | + suggestions..., |
| 174 | + ) |
| 175 | +} |
| 176 | + |
| 177 | +func unicodeEscape(ch rune) string { |
| 178 | + return fmt.Sprintf("\\u%04x", ch) |
| 179 | +} |
| 180 | + |
| 181 | +func refactorMessage(original, replacement string) string { |
| 182 | + return fmt.Sprintf("Replace '%s' with '%s'. This maintains the current functionality.", original, replacement) |
| 183 | +} |
| 184 | + |
| 185 | +func escapeBackslashMessage(original, replacement string) string { |
| 186 | + return fmt.Sprintf("Replace '%s' with '%s' to include the actual backslash character.", original, replacement) |
| 187 | +} |
0 commit comments