Skip to content

Commit

Permalink
fix: don't wrap empty attributes (#8)
Browse files Browse the repository at this point in the history
* fix: don't wrap empty attributes

* fix: collapse empty multiline strings

* chore: fix typo
  • Loading branch information
schoero authored Mar 10, 2024
1 parent cc7169f commit bf4426b
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/parsers/svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ export function getLiteralsBySvelteClassAttribute(ctx: Rule.RuleContext, attribu

const value = attribute.value[0];

// eslint-disable-next-line eslint-plugin-typescript/no-unnecessary-condition
if(!value){ // empty attribute
return [];
}

if(isSvelteStringLiteral(value)){
const stringLiteral = getStringLiteralBySvelteStringLiteral(ctx, value);

Expand Down
35 changes: 35 additions & 0 deletions src/rules/tailwind-multiline.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,41 @@ import { tailwindMultiline } from "readable-tailwind:rules:tailwind-multiline.js

describe(tailwindMultiline.name, () => {

it("should not wrap empty strings", () => {
expect(void lint(
tailwindMultiline,
TEST_SYNTAXES,
{
valid: [
{
html: "<div class=\"\" />",
jsx: "const Test = () => <div class=\"\" />;",
svelte: "<div class=\"\" />",
vue: "<template><div class=\"\" /></template>"
},
{
html: "<div class='' />",
jsx: "const Test = () => <div class='' />;",
svelte: "<div class='' />",
vue: "<template><div class='' /></template>"
},
{
jsx: "const Test = () => <div class={\"\"} />;",
svelte: "<div class={\"\"} />"
},
{
jsx: "const Test = () => <div class={''} />;",
svelte: "<div class={''} />"
},
{
jsx: "const Test = () => <div class={``} />;",
svelte: "<div class={``} />"
}
]
}
)).toBeUndefined();
});

it("should not wrap short lines", () => {
expect(void lint(
tailwindMultiline,
Expand Down
9 changes: 8 additions & 1 deletion src/rules/tailwind-multiline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,14 @@ function lintLiterals(ctx: Rule.RuleContext, literals: Literal[]) {

}

// Skip line wrapping if it is not necessary
// skip if class string was empty
if(lines.length === 2){
if(!literal.openingBraces && !literal.closingBraces && literal.content.trim() === ""){
continue;
}
}

// skip line wrapping if it is not necessary
skip: if(lines.length === 3){

// disallow skipping for template literals with braces
Expand Down
25 changes: 25 additions & 0 deletions src/rules/tailwind-no-unnecessary-whitespace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,31 @@ describe(tailwindNoUnnecessaryWhitespace.name, () => {
})
).toBeUndefined());

it("should collapse empty multiline strings", () => {
const dirtyEmptyMultilineString = `
`;
const cleanEmptyMultilineString = "";

expect(
void lint(tailwindNoUnnecessaryWhitespace, TEST_SYNTAXES, {
invalid: [
{
errors: 1,
html: `<div class="${dirtyEmptyMultilineString}" />`,
htmlOutput: `<div class="${cleanEmptyMultilineString}" />`,
jsx: `const Test = () => <div class="${dirtyEmptyMultilineString}" />;`,
jsxOutput: `const Test = () => <div class="${cleanEmptyMultilineString}" />;`,
svelte: `<div class="${dirtyEmptyMultilineString}" />`,
svelteOutput: `<div class="${cleanEmptyMultilineString}" />`,
vue: `<template><div class="${dirtyEmptyMultilineString}" /></template>`,
vueOutput: `<template><div class="${cleanEmptyMultilineString}" /></template>`
}
]
})
).toBeUndefined();
});

it("should keep the quotes as they are", () => expect(
void lint(tailwindNoUnnecessaryWhitespace, TEST_SYNTAXES, {
invalid: [
Expand Down
4 changes: 4 additions & 0 deletions src/rules/tailwind-no-unnecessary-whitespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,10 @@ function splitClassesKeepWhitespace(literal: Literal, allowMultiline: boolean):

const mixedChunks: string[] = [];

if(classChunks.length === 0){
return [];
}

while(whitespaceChunks.length > 0 || classChunks.length > 0){

const whitespaceChunk = whitespaceChunks.shift();
Expand Down

0 comments on commit bf4426b

Please sign in to comment.