|
| 1 | +import { Parser } from "htmlparser2"; |
| 2 | +import type { HyperframeLintFinding } from "./types"; |
| 3 | +import { parseHtmlStructure, readDecodedAttr } from "./utils"; |
| 4 | + |
| 5 | +type Location = Pick<HyperframeLintFinding, "line" | "column">; |
| 6 | + |
| 7 | +function normalized(source: string): { text: string; offsets: number[] } { |
| 8 | + let text = ""; |
| 9 | + const offsets: number[] = []; |
| 10 | + for (let i = 0; i < source.length; i++) { |
| 11 | + const char = source[i]!; |
| 12 | + if (/\s/.test(char)) { |
| 13 | + if (text.endsWith(" ")) continue; |
| 14 | + text += " "; |
| 15 | + } else text += char; |
| 16 | + offsets.push(i); |
| 17 | + } |
| 18 | + return { text, offsets }; |
| 19 | +} |
| 20 | + |
| 21 | +/** Resolve against original bytes, not the comment-stripped/template-unwrapped rule input. |
| 22 | + * Ambiguous snippets/IDs deliberately have no coordinates rather than pointing at the wrong copy. */ |
| 23 | +export function createSourceLocator(source: string): (finding: HyperframeLintFinding) => Location { |
| 24 | + const tags = parseHtmlStructure(source).tags; |
| 25 | + const searchable = source.split(""); |
| 26 | + const parser = new Parser({ |
| 27 | + oncomment() { |
| 28 | + searchable.fill("\0", parser.startIndex, parser.endIndex + 1); |
| 29 | + }, |
| 30 | + }); |
| 31 | + parser.write(source); |
| 32 | + parser.end(); |
| 33 | + const normalizedSource = normalized(searchable.join("")); |
| 34 | + const at = (offset: number): Location => { |
| 35 | + const before = source.slice(0, offset); |
| 36 | + return { |
| 37 | + line: before.split(/\r\n|\r|\n/).length, |
| 38 | + column: offset - Math.max(before.lastIndexOf("\n"), before.lastIndexOf("\r")), |
| 39 | + }; |
| 40 | + }; |
| 41 | + return (finding) => { |
| 42 | + if (finding.line !== undefined) return { line: finding.line, column: finding.column }; |
| 43 | + if (finding.snippet) { |
| 44 | + const offset = snippetOffset(normalizedSource, finding.snippet); |
| 45 | + return offset === undefined ? {} : at(offset); |
| 46 | + } |
| 47 | + const id = finding.elementId ?? /^#([\w-]+)$/.exec(finding.selector ?? "")?.[1]; |
| 48 | + if (!id) return {}; |
| 49 | + const matches = tags.filter((tag) => readDecodedAttr(tag.raw, "id") === id); |
| 50 | + return matches.length === 1 ? at(matches[0]!.index) : {}; |
| 51 | + }; |
| 52 | +} |
| 53 | + |
| 54 | +export function sourceLocationFor(source: string, finding: HyperframeLintFinding): Location { |
| 55 | + return createSourceLocator(source)(finding); |
| 56 | +} |
| 57 | + |
| 58 | +function snippetOffset(source: ReturnType<typeof normalized>, snippet: string): number | undefined { |
| 59 | + const needle = snippet |
| 60 | + .replace(/\s+/g, " ") |
| 61 | + .trim() |
| 62 | + .replace(/\.\.\.$/, ""); |
| 63 | + if (!needle) return undefined; |
| 64 | + const index = source.text.indexOf(needle); |
| 65 | + if (index < 0 || source.text.indexOf(needle, index + 1) >= 0) return undefined; |
| 66 | + return source.offsets[index]; |
| 67 | +} |
0 commit comments