Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: improve display of linting errors #60

Merged
merged 1 commit into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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