Skip to content

Commit

Permalink
refactor: improve display of linting errors (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
schoero authored Oct 1, 2024
1 parent 396263b commit e993cb7
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
17 changes: 12 additions & 5 deletions src/rules/tailwind-multiline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ import { getAttributesByJSXElement, getLiteralsByJSXClassAttribute } from "reada
import { getAttributesBySvelteTag, getLiteralsBySvelteClassAttribute } from "readable-tailwind:parsers:svelte.js";
import { getAttributesByVueStartTag, getLiteralsByVueClassAttribute } from "readable-tailwind:parsers:vue.js";
import { escapeNestedQuotes } from "readable-tailwind:utils:quotes.js";
import { findLineStartPosition, findLiteralStartPosition, splitClasses } from "readable-tailwind:utils:utils.js";
import {
display,
findLineStartPosition,
findLiteralStartPosition,
splitClasses
} from "readable-tailwind:utils:utils.js";

import type { TagNode } from "es-html-parser";
import type { Rule } from "eslint";
Expand Down Expand Up @@ -502,13 +507,14 @@ function lintLiterals(ctx: Rule.RuleContext, literals: Literal[]) {

ctx.report({
data: {
notReadable: literal.content
notReadable: display(literal.raw),
readable: display(fixedClasses)
},
fix(fixer) {
return fixer.replaceTextRange(literal.range, fixedClasses);
},
loc: literal.loc,
message: "Unnecessary line wrapping: \"{{ notReadable }}\"."
message: "Unnecessary line wrapping. Expected\n\n{{ notReadable }}\n\nto be\n\n{{ readable }}"
});

return;
Expand Down Expand Up @@ -580,15 +586,16 @@ function lintLiterals(ctx: Rule.RuleContext, literals: Literal[]) {

ctx.report({
data: {
notReadable: literal.content
notReadable: display(literal.raw),
readable: display(fixedClasses)
},
fix(fixer) {
return literal.parent.type === "JSXAttribute"
? fixer.replaceTextRange(literal.range, `{${fixedClasses}}`)
: fixer.replaceTextRange(literal.range, fixedClasses);
},
loc: literal.loc,
message: "Incorrect line wrapping: \"{{ notReadable }}\"."
message: "Incorrect line wrapping. Expected\n\n{{ notReadable }}\n\nto be\n\n{{ readable }}"
});

}
Expand Down
7 changes: 4 additions & 3 deletions src/rules/tailwind-no-unnecessary-whitespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { getAttributesByJSXElement, getLiteralsByJSXClassAttribute } from "reada
import { getAttributesBySvelteTag, getLiteralsBySvelteClassAttribute } from "readable-tailwind:parsers:svelte.js";
import { getAttributesByVueStartTag, getLiteralsByVueClassAttribute } from "readable-tailwind:parsers:vue.js";
import { escapeNestedQuotes } from "readable-tailwind:utils:quotes.js";
import { splitClasses, splitWhitespaces } from "readable-tailwind:utils:utils.js";
import { display, splitClasses, splitWhitespaces } from "readable-tailwind:utils:utils.js";

import type { TagNode } from "es-html-parser";
import type { Rule } from "eslint";
Expand Down Expand Up @@ -191,13 +191,14 @@ function lintLiterals(ctx: Rule.RuleContext, literals: Literal[]) {

ctx.report({
data: {
unnecessaryWhitespace: literal.content
fixedClasses: display(fixedClasses),
unnecessaryWhitespace: display(literal.raw)
},
fix(fixer) {
return fixer.replaceTextRange(literal.range, fixedClasses);
},
loc: literal.loc,
message: "Unnecessary whitespace: \"{{ unnecessaryWhitespace }}\"."
message: "Unnecessary whitespace. Expected\n\n{{ unnecessaryWhitespace }}\n\nto be\n\n{{ fixedClasses }}"
});

}
Expand Down
7 changes: 4 additions & 3 deletions src/rules/tailwind-sort-classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { getAttributesByJSXElement, getLiteralsByJSXClassAttribute } from "reada
import { getAttributesBySvelteTag, getLiteralsBySvelteClassAttribute } from "readable-tailwind:parsers:svelte.js";
import { getAttributesByVueStartTag, getLiteralsByVueClassAttribute } from "readable-tailwind:parsers:vue.js";
import { escapeNestedQuotes } from "readable-tailwind:utils:quotes.js";
import { splitClasses, splitWhitespaces } from "readable-tailwind:utils:utils.js";
import { display, splitClasses, splitWhitespaces } from "readable-tailwind:utils:utils.js";

import type { TagNode } from "es-html-parser";
import type { Rule } from "eslint";
Expand Down Expand Up @@ -112,13 +112,14 @@ export const tailwindSortClasses: ESLintRule<Options> = {

ctx.report({
data: {
notSorted: literal.content
notSorted: display(literal.raw),
sorted: display(fixedClasses)
},
fix(fixer) {
return fixer.replaceTextRange(literal.range, fixedClasses);
},
loc: literal.loc,
message: "Incorrect class order: \"{{ notSorted }}\"."
message: "Incorrect class order. Expected\n\n{{ notSorted }}\n\nto be\n\n{{ sorted }}"
});

}
Expand Down
8 changes: 8 additions & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ export function splitClasses(classes: string): string[] {

}

export function display(classes: string): string {
return classes
.replaceAll(" ", "·")
.replaceAll("\n", "↵\n")
.replaceAll("\r", "↩\r")
.replaceAll("\t", "→");
}

export function splitWhitespaces(classes: string): string[] {
return classes.split(/\S+/);
}
Expand Down

0 comments on commit e993cb7

Please sign in to comment.