|
| 1 | +import type { AST } from "svelte-eslint-parser" |
| 2 | +import { createRule } from "../utils" |
| 3 | + |
| 4 | +export default createRule("no-trailing-spaces", { |
| 5 | + meta: { |
| 6 | + type: "layout", |
| 7 | + docs: { |
| 8 | + description: "disallow trailing whitespace at the end of lines", |
| 9 | + category: "Extension Rules", |
| 10 | + recommended: false, |
| 11 | + extensionRule: "no-trailing-spaces", |
| 12 | + conflictWithPrettier: true, |
| 13 | + }, |
| 14 | + fixable: "whitespace", |
| 15 | + schema: [ |
| 16 | + { |
| 17 | + type: "object", |
| 18 | + properties: { |
| 19 | + skipBlankLines: { type: "boolean" }, |
| 20 | + ignoreComments: { type: "boolean" }, |
| 21 | + }, |
| 22 | + additionalProperties: false, |
| 23 | + }, |
| 24 | + ], |
| 25 | + messages: { |
| 26 | + trailingSpace: "Trailing spaces not allowed.", |
| 27 | + }, |
| 28 | + }, |
| 29 | + create(context) { |
| 30 | + const options: |
| 31 | + | { skipBlankLines?: boolean; ignoreComments?: boolean } |
| 32 | + | undefined = context.options[0] |
| 33 | + const skipBlankLines = options?.skipBlankLines || false |
| 34 | + const ignoreComments = options?.ignoreComments || false |
| 35 | + |
| 36 | + const sourceCode = context.getSourceCode() |
| 37 | + |
| 38 | + const ignoreLineNumbers = new Set<number>() |
| 39 | + if (ignoreComments) { |
| 40 | + for (const { type, loc } of sourceCode.getAllComments()) { |
| 41 | + const endLine = type === "Block" ? loc.end.line - 1 : loc.end.line |
| 42 | + for (let i = loc.start.line; i <= endLine; i++) { |
| 43 | + ignoreLineNumbers.add(i) |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Reports a given location. |
| 50 | + */ |
| 51 | + function report(loc: AST.SourceLocation) { |
| 52 | + context.report({ |
| 53 | + loc, |
| 54 | + messageId: "trailingSpace", |
| 55 | + fix(fixer) { |
| 56 | + return fixer.removeRange([ |
| 57 | + sourceCode.getIndexFromLoc(loc.start), |
| 58 | + sourceCode.getIndexFromLoc(loc.end), |
| 59 | + ]) |
| 60 | + }, |
| 61 | + }) |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Collects the location of the given node as the ignore line numbers. |
| 66 | + */ |
| 67 | + function collectIgnoreLineNumbers({ loc }: { loc: AST.SourceLocation }) { |
| 68 | + const endLine = loc.end.line - 1 |
| 69 | + for (let i = loc.start.line; i <= endLine; i++) { |
| 70 | + ignoreLineNumbers.add(i) |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + return { |
| 75 | + TemplateElement: collectIgnoreLineNumbers, |
| 76 | + ...(ignoreComments |
| 77 | + ? { |
| 78 | + SvelteHTMLComment: collectIgnoreLineNumbers, |
| 79 | + } |
| 80 | + : {}), |
| 81 | + "Program:exit"() { |
| 82 | + const lines = sourceCode.lines |
| 83 | + for (let lineIndex = 0; lineIndex < lines.length; lineIndex++) { |
| 84 | + const line = lines[lineIndex] |
| 85 | + if (skipBlankLines && !line.trim()) { |
| 86 | + continue |
| 87 | + } |
| 88 | + const lineNumber = lineIndex + 1 |
| 89 | + if (ignoreLineNumbers.has(lineNumber)) { |
| 90 | + continue |
| 91 | + } |
| 92 | + const trimmed = line.trimEnd() |
| 93 | + if (trimmed === line) { |
| 94 | + continue |
| 95 | + } |
| 96 | + report({ |
| 97 | + start: { line: lineNumber, column: trimmed.length }, |
| 98 | + end: { line: lineNumber, column: line.length }, |
| 99 | + }) |
| 100 | + } |
| 101 | + }, |
| 102 | + } |
| 103 | + }, |
| 104 | +}) |
0 commit comments