|
| 1 | +import * as Parser from "@typescript-eslint/parser"; |
| 2 | +import { RuleTester, TestCaseError } from "@typescript-eslint/rule-tester"; |
| 3 | + |
| 4 | +import { |
| 5 | + generalSettings, |
| 6 | + withAngularParser, |
| 7 | +} from "../utils/parser/test-helpers"; |
| 8 | +import { enforcesShorthand, MessageIds, RULE_NAME } from "./enforces-shorthand"; |
| 9 | + |
| 10 | +const generateError = ( |
| 11 | + obsoleteClassnames: Array<string>, |
| 12 | + shorthand: string, |
| 13 | +): TestCaseError<MessageIds> => { |
| 14 | + return { |
| 15 | + messageId: "fix:use-shorthand", |
| 16 | + data: { |
| 17 | + classnames: obsoleteClassnames.map((cls) => `'${cls}'`).join(", "), |
| 18 | + shorthand: shorthand, |
| 19 | + }, |
| 20 | + }; |
| 21 | +}; |
| 22 | + |
| 23 | +const ruleTester = new RuleTester({ |
| 24 | + languageOptions: { |
| 25 | + parser: Parser, |
| 26 | + parserOptions: { |
| 27 | + ecmaFeatures: { |
| 28 | + jsx: true, |
| 29 | + }, |
| 30 | + }, |
| 31 | + }, |
| 32 | + settings: { |
| 33 | + tailwindcss: { |
| 34 | + ...generalSettings, |
| 35 | + }, |
| 36 | + }, |
| 37 | +}); |
| 38 | + |
| 39 | +ruleTester.run(RULE_NAME, enforcesShorthand, { |
| 40 | + valid: [ |
| 41 | + // Angular / Native HTML + static text |
| 42 | + `<h1 class="-mt- h-100 md:h-full">valid</h1>`, |
| 43 | + `<h1 class="w-full md:h-full">modifiers</h1>`, |
| 44 | + ].map((testedNgCode) => ({ |
| 45 | + code: testedNgCode, |
| 46 | + languageOptions: withAngularParser, |
| 47 | + })), |
| 48 | + invalid: [ |
| 49 | + { |
| 50 | + code: `ctl("overflow-x-hidden overflow-y-hidden")`, |
| 51 | + errors: [ |
| 52 | + generateError( |
| 53 | + ["overflow-x-hidden", "overflow-y-hidden"], |
| 54 | + "overflow-hidden", |
| 55 | + ), |
| 56 | + ], |
| 57 | + output: `ctl("overflow-hidden")`, |
| 58 | + }, |
| 59 | + { |
| 60 | + code: `ctl("overscroll-x-none overscroll-y-none")`, |
| 61 | + errors: [ |
| 62 | + generateError( |
| 63 | + ["overscroll-x-none", "overscroll-y-none"], |
| 64 | + "overscroll-none", |
| 65 | + ), |
| 66 | + ], |
| 67 | + output: `ctl("overscroll-none")`, |
| 68 | + }, |
| 69 | + { |
| 70 | + code: `ctl("top-0 right-0 bottom-0")`, |
| 71 | + errors: [generateError(["top-0", "bottom-0"], "inset-y-0")], |
| 72 | + output: `ctl("right-0 inset-y-0")`, |
| 73 | + }, |
| 74 | + { |
| 75 | + code: `ctl("top-0 right-0 bottom-0 left-0")`, |
| 76 | + errors: [ |
| 77 | + generateError(["right-0", "left-0"], "inset-x-0"), |
| 78 | + generateError(["top-0", "bottom-0"], "inset-y-0"), |
| 79 | + ], |
| 80 | + output: [ |
| 81 | + `ctl("top-0 bottom-0 inset-x-0")`, |
| 82 | + `ctl("inset-x-0 inset-y-0")`, |
| 83 | + `ctl("inset-0")`, |
| 84 | + ], |
| 85 | + }, |
| 86 | + { |
| 87 | + code: `ctl("inset-y-0 right-0 left-0")`, |
| 88 | + errors: [generateError(["right-0", "left-0"], "inset-x-0")], |
| 89 | + output: [`ctl("inset-y-0 inset-x-0")`, `ctl("inset-0")`], |
| 90 | + }, |
| 91 | + { |
| 92 | + code: `ctl("-inset-y-10 -inset-x-10")`, |
| 93 | + errors: [generateError(["-inset-x-10", "-inset-y-10"], "-inset-10")], |
| 94 | + output: [`ctl("-inset-10")`], |
| 95 | + }, |
| 96 | + { |
| 97 | + code: `ctl("-my-10 -mx-10")`, |
| 98 | + errors: [generateError(["-mx-10", "-my-10"], "-m-10")], |
| 99 | + output: [`ctl("-m-10")`], |
| 100 | + }, |
| 101 | + { |
| 102 | + code: `ctl("dark:-my-10 dark:-mx-10")`, |
| 103 | + errors: [generateError(["dark:-mx-10", "dark:-my-10"], "dark:-m-10")], |
| 104 | + output: [`ctl("dark:-m-10")`], |
| 105 | + }, |
| 106 | + { |
| 107 | + code: `ctl("gap-x-10 gap-y-10")`, |
| 108 | + errors: [generateError(["gap-x-10", "gap-y-10"], "gap-10")], |
| 109 | + output: [`ctl("gap-10")`], |
| 110 | + }, |
| 111 | + { |
| 112 | + code: `<h1 class="block md:-mx-10 md:-my-10">margin</h1>`, |
| 113 | + errors: [generateError(["md:-mx-10", "md:-my-10"], "md:-m-10")], |
| 114 | + output: `<h1 class="block md:-m-10">margin</h1>`, |
| 115 | + languageOptions: withAngularParser, |
| 116 | + }, |
| 117 | + { |
| 118 | + code: `<h1 class="md:pl-10 md:pr-10">padding</h1>`, |
| 119 | + errors: [generateError(["md:pl-10", "md:pr-10"], "md:px-10")], |
| 120 | + output: `<h1 class="md:px-10">padding</h1>`, |
| 121 | + languageOptions: withAngularParser, |
| 122 | + }, |
| 123 | + { |
| 124 | + code: `ctl("w-1/2 h-1/2")`, |
| 125 | + errors: [generateError(["w-1/2", "h-1/2"], "size-1/2")], |
| 126 | + output: [`ctl("size-1/2")`], |
| 127 | + }, |
| 128 | + { |
| 129 | + code: `ctl("debug overflow-hidden text-ellipsis whitespace-nowrap")`, |
| 130 | + errors: [ |
| 131 | + generateError( |
| 132 | + ["overflow-hidden", "text-ellipsis", "whitespace-nowrap"], |
| 133 | + "truncate", |
| 134 | + ), |
| 135 | + ], |
| 136 | + output: [`ctl("debug truncate")`], |
| 137 | + }, |
| 138 | + { |
| 139 | + code: `ctl("rounded-bl-2xl rounded-br-2xl")`, |
| 140 | + errors: [ |
| 141 | + generateError(["rounded-bl-2xl", "rounded-br-2xl"], "rounded-b-2xl"), |
| 142 | + ], |
| 143 | + output: [`ctl("rounded-b-2xl")`], |
| 144 | + }, |
| 145 | + { |
| 146 | + code: `ctl("rounded-ee-2xl rounded-es-2xl")`, |
| 147 | + errors: [ |
| 148 | + generateError(["rounded-ee-2xl", "rounded-es-2xl"], "rounded-b-2xl"), |
| 149 | + ], |
| 150 | + output: [`ctl("rounded-b-2xl")`], |
| 151 | + }, |
| 152 | + { |
| 153 | + code: `ctl("border-t-1 border-b-1")`, |
| 154 | + errors: [generateError(["border-t-1", "border-b-1"], "border-y-1")], |
| 155 | + output: [`ctl("border-y-1")`], |
| 156 | + }, |
| 157 | + { |
| 158 | + code: `ctl("border-t border-b")`, |
| 159 | + errors: [generateError(["border-t", "border-b"], "border-y")], |
| 160 | + output: [`ctl("border-y")`], |
| 161 | + }, |
| 162 | + { |
| 163 | + code: `ctl("border-t border-b border-x")`, |
| 164 | + errors: [generateError(["border-t", "border-b"], "border-y")], |
| 165 | + output: [`ctl("border-x border-y")`, `ctl("border")`], |
| 166 | + }, |
| 167 | + { |
| 168 | + code: `ctl("border-spacing-x-20 border-spacing-y-20")`, |
| 169 | + errors: [ |
| 170 | + generateError( |
| 171 | + ["border-spacing-x-20", "border-spacing-y-20"], |
| 172 | + "border-spacing-20", |
| 173 | + ), |
| 174 | + ], |
| 175 | + output: [`ctl("border-spacing-20")`], |
| 176 | + }, |
| 177 | + { |
| 178 | + code: `ctl("scale-x-150 scale-y-150")`, |
| 179 | + errors: [generateError(["scale-x-150", "scale-y-150"], "scale-150")], |
| 180 | + output: [`ctl("scale-150")`], |
| 181 | + }, |
| 182 | + { |
| 183 | + code: `ctl("skew-x-150 dark:-skew-x-100 dark:skew-y-100 skew-y-150")`, |
| 184 | + errors: [generateError(["skew-x-150", "skew-y-150"], "skew-150")], |
| 185 | + output: [`ctl("dark:-skew-x-100 dark:skew-y-100 skew-150")`], |
| 186 | + }, |
| 187 | + { |
| 188 | + code: `ctl("-translate-y-10 -translate-x-10")`, |
| 189 | + errors: [ |
| 190 | + generateError(["-translate-x-10", "-translate-y-10"], "-translate-10"), |
| 191 | + ], |
| 192 | + output: [`ctl("-translate-10")`], |
| 193 | + }, |
| 194 | + ], |
| 195 | +}); |
0 commit comments