|
| 1 | +import { Herb } from "@herb-tools/node-wasm" |
| 2 | +import { Location } from "@herb-tools/core" |
| 3 | +import { Formatter } from "@herb-tools/formatter" |
| 4 | + |
| 5 | +import { SourceRule } from "../types.js" |
| 6 | +import { positionFromOffset } from "./rule-utils.js" |
| 7 | + |
| 8 | +import type { UnboundLintOffense, LintContext, FullRuleConfig } from "../types.js" |
| 9 | + |
| 10 | +interface DiffRange { |
| 11 | + startOffset: number |
| 12 | + endOffset: number |
| 13 | + originalContent: string |
| 14 | + formattedContent: string |
| 15 | +} |
| 16 | + |
| 17 | +export class HerbFormatterWellFormattedRule extends SourceRule { |
| 18 | + static autocorrectable = false |
| 19 | + static ruleName = "herb-formatter-well-formatted" |
| 20 | + |
| 21 | + get defaultConfig(): FullRuleConfig { |
| 22 | + return { |
| 23 | + enabled: false, |
| 24 | + severity: "warning" |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + isEnabled(_source: string, context?: Partial<LintContext>): boolean { |
| 29 | + const config = context?.config |
| 30 | + |
| 31 | + if (!config) return true |
| 32 | + if (!config.isFormatterEnabled) return false |
| 33 | + if (context?.fileName && !config.isFormatterEnabledForPath(context.fileName)) return false |
| 34 | + |
| 35 | + return true |
| 36 | + } |
| 37 | + |
| 38 | + check(source: string, context?: Partial<LintContext>): UnboundLintOffense[] { |
| 39 | + const offenses: UnboundLintOffense[] = [] |
| 40 | + |
| 41 | + if (source.length === 0) return offenses |
| 42 | + |
| 43 | + try { |
| 44 | + const config = context?.config |
| 45 | + const filePath = context?.fileName |
| 46 | + const formatter = config ? Formatter.from(Herb, config) : new Formatter(Herb) |
| 47 | + const formatted = formatter.format(source, {}, filePath) |
| 48 | + |
| 49 | + if (source === formatted) return offenses |
| 50 | + |
| 51 | + const diffs = this.computeDiffs(source, formatted) |
| 52 | + |
| 53 | + for (const diff of diffs) { |
| 54 | + const startPosition = positionFromOffset(source, diff.startOffset) |
| 55 | + const endPosition = positionFromOffset(source, diff.endOffset) |
| 56 | + const location = Location.from(startPosition.line, startPosition.column, endPosition.line, endPosition.column) |
| 57 | + const message = this.generateMessage(diff) |
| 58 | + |
| 59 | + offenses.push({ |
| 60 | + rule: this.ruleName, |
| 61 | + code: this.ruleName, |
| 62 | + source: "Herb Linter", |
| 63 | + message, |
| 64 | + location |
| 65 | + }) |
| 66 | + } |
| 67 | + } catch { |
| 68 | + // Skip silently on formatting errors |
| 69 | + } |
| 70 | + |
| 71 | + return offenses |
| 72 | + } |
| 73 | + |
| 74 | + private computeDiffs(original: string, formatted: string): DiffRange[] { |
| 75 | + const diffs: DiffRange[] = [] |
| 76 | + const originalLength = original.length |
| 77 | + const formattedLength = formatted.length |
| 78 | + |
| 79 | + let originalIndex = 0 |
| 80 | + let formattedIndex = 0 |
| 81 | + |
| 82 | + while (originalIndex < originalLength || formattedIndex < formattedLength) { |
| 83 | + if (originalIndex < originalLength && formattedIndex < formattedLength && original[originalIndex] === formatted[formattedIndex]) { |
| 84 | + originalIndex++ |
| 85 | + formattedIndex++ |
| 86 | + continue |
| 87 | + } |
| 88 | + |
| 89 | + const diffStartOriginal = originalIndex |
| 90 | + const diffStartFormatted = formattedIndex |
| 91 | + const syncPoint = this.findSyncPoint(original, formatted, originalIndex, formattedIndex) |
| 92 | + |
| 93 | + if (syncPoint) { |
| 94 | + const originalEnd = syncPoint.originalIndex |
| 95 | + const formattedEnd = syncPoint.formattedIndex |
| 96 | + |
| 97 | + if (originalEnd > diffStartOriginal || formattedEnd > diffStartFormatted) { |
| 98 | + diffs.push({ |
| 99 | + startOffset: diffStartOriginal, |
| 100 | + endOffset: Math.max(diffStartOriginal + 1, originalEnd), |
| 101 | + originalContent: original.slice(diffStartOriginal, originalEnd), |
| 102 | + formattedContent: formatted.slice(diffStartFormatted, formattedEnd) |
| 103 | + }) |
| 104 | + } |
| 105 | + |
| 106 | + originalIndex = originalEnd |
| 107 | + formattedIndex = formattedEnd |
| 108 | + } else { |
| 109 | + diffs.push({ |
| 110 | + startOffset: diffStartOriginal, |
| 111 | + endOffset: originalLength, |
| 112 | + originalContent: original.slice(diffStartOriginal), |
| 113 | + formattedContent: formatted.slice(diffStartFormatted) |
| 114 | + }) |
| 115 | + |
| 116 | + break |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + return this.coalesceDiffs(diffs) |
| 121 | + } |
| 122 | + |
| 123 | + private findSyncPoint(original: string, formatted: string, originalStart: number, formattedStart: number): { originalIndex: number; formattedIndex: number } | null { |
| 124 | + const originalLength = original.length |
| 125 | + const formattedLength = formatted.length |
| 126 | + const maxLookahead = 100 |
| 127 | + const minMatchLength = 3 |
| 128 | + |
| 129 | + for (let originalOffset = 1; originalOffset <= maxLookahead && originalStart + originalOffset <= originalLength; originalOffset++) { |
| 130 | + for (let formattedOffset = 1; formattedOffset <= maxLookahead && formattedStart + formattedOffset <= formattedLength; formattedOffset++) { |
| 131 | + const originalPosition = originalStart + originalOffset |
| 132 | + const formattedPosition = formattedStart + formattedOffset |
| 133 | + |
| 134 | + let matchLength = 0 |
| 135 | + |
| 136 | + while ( |
| 137 | + originalPosition + matchLength < originalLength && |
| 138 | + formattedPosition + matchLength < formattedLength && |
| 139 | + original[originalPosition + matchLength] === formatted[formattedPosition + matchLength] |
| 140 | + ) { |
| 141 | + matchLength++ |
| 142 | + if (matchLength >= minMatchLength) { |
| 143 | + return { originalIndex: originalPosition, formattedIndex: formattedPosition } |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + if (originalPosition === originalLength && formattedPosition === formattedLength) { |
| 148 | + return { originalIndex: originalPosition, formattedIndex: formattedPosition } |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + if (originalStart >= originalLength && formattedStart < formattedLength) { |
| 154 | + return { originalIndex: originalLength, formattedIndex: formattedLength } |
| 155 | + } |
| 156 | + |
| 157 | + if (formattedStart >= formattedLength && originalStart < originalLength) { |
| 158 | + return { originalIndex: originalLength, formattedIndex: formattedLength } |
| 159 | + } |
| 160 | + |
| 161 | + return null |
| 162 | + } |
| 163 | + |
| 164 | + private coalesceDiffs(diffs: DiffRange[]): DiffRange[] { |
| 165 | + if (diffs.length <= 1) return diffs |
| 166 | + |
| 167 | + const coalesced: DiffRange[] = [] |
| 168 | + let current = diffs[0] |
| 169 | + |
| 170 | + for (let i = 1; i < diffs.length; i++) { |
| 171 | + const next = diffs[i] |
| 172 | + |
| 173 | + if (next.startOffset <= current.endOffset + 1) { |
| 174 | + current = { |
| 175 | + startOffset: current.startOffset, |
| 176 | + endOffset: Math.max(current.endOffset, next.endOffset), |
| 177 | + originalContent: current.originalContent + next.originalContent, |
| 178 | + formattedContent: current.formattedContent + next.formattedContent |
| 179 | + } |
| 180 | + } else { |
| 181 | + coalesced.push(current) |
| 182 | + current = next |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + coalesced.push(current) |
| 187 | + |
| 188 | + return coalesced |
| 189 | + } |
| 190 | + |
| 191 | + private generateMessage(diff: DiffRange): string { |
| 192 | + const { originalContent, formattedContent } = diff |
| 193 | + |
| 194 | + const originalIndentMatch = originalContent.match(/^[ \t]+/) |
| 195 | + const formattedIndentMatch = formattedContent.match(/^[ \t]+/) |
| 196 | + |
| 197 | + if (originalIndentMatch || formattedIndentMatch) { |
| 198 | + const originalIndent = originalIndentMatch?.[0] || "" |
| 199 | + const formattedIndent = formattedIndentMatch?.[0] || "" |
| 200 | + |
| 201 | + if (originalIndent !== formattedIndent) { |
| 202 | + const originalSpaces = this.countIndentation(originalIndent) |
| 203 | + const formattedSpaces = this.countIndentation(formattedIndent) |
| 204 | + |
| 205 | + if (originalSpaces !== formattedSpaces) { |
| 206 | + return `Incorrect indentation: expected ${formattedSpaces} spaces, found ${originalSpaces}` |
| 207 | + } |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + const originalIsWhitespace = /^[\s]+$/.test(originalContent) |
| 212 | + const formattedIsWhitespace = /^[\s]+$/.test(formattedContent) |
| 213 | + |
| 214 | + if (originalIsWhitespace && formattedContent === "") { |
| 215 | + return "Unexpected whitespace" |
| 216 | + } |
| 217 | + |
| 218 | + if (originalContent === "" && formattedIsWhitespace) { |
| 219 | + return "Missing whitespace" |
| 220 | + } |
| 221 | + |
| 222 | + const originalNewlines = (originalContent.match(/\n/g) || []).length |
| 223 | + const formattedNewlines = (formattedContent.match(/\n/g) || []).length |
| 224 | + |
| 225 | + if (originalNewlines !== formattedNewlines) { |
| 226 | + return "Incorrect line breaks" |
| 227 | + } |
| 228 | + |
| 229 | + return "Formatting differs from expected" |
| 230 | + } |
| 231 | + |
| 232 | + private countIndentation(indent: string): number { |
| 233 | + let count = 0 |
| 234 | + |
| 235 | + for (const char of indent) { |
| 236 | + if (char === "\t") { |
| 237 | + count += 2 |
| 238 | + } else { |
| 239 | + count++ |
| 240 | + } |
| 241 | + } |
| 242 | + |
| 243 | + return count |
| 244 | + } |
| 245 | +} |
0 commit comments