|
| 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("gap-x-10 gap-y-10")`, |
| 98 | + errors: [generateError(["gap-x-10", "gap-y-10"], "gap-10")], |
| 99 | + output: [`ctl("gap-10")`], |
| 100 | + }, |
| 101 | + { |
| 102 | + code: `<h1 class="block md:-mx-10 md:-my-10">margin</h1>`, |
| 103 | + errors: [generateError(["md:-mx-10", "md:-my-10"], "md:-m-10")], |
| 104 | + output: `<h1 class="block md:-m-10">margin</h1>`, |
| 105 | + languageOptions: withAngularParser, |
| 106 | + }, |
| 107 | + { |
| 108 | + code: `<h1 class="md:pl-10 md:pr-10">padding</h1>`, |
| 109 | + errors: [generateError(["md:pl-10", "md:pr-10"], "md:px-10")], |
| 110 | + output: `<h1 class="md:px-10">padding</h1>`, |
| 111 | + languageOptions: withAngularParser, |
| 112 | + }, |
| 113 | + { |
| 114 | + code: `ctl("w-1/2 h-1/2")`, |
| 115 | + errors: [generateError(["w-1/2", "h-1/2"], "size-1/2")], |
| 116 | + output: [`ctl("size-1/2")`], |
| 117 | + }, |
| 118 | + { |
| 119 | + code: `ctl("debug overflow-hidden text-ellipsis whitespace-nowrap")`, |
| 120 | + errors: [ |
| 121 | + generateError( |
| 122 | + ["overflow-hidden", "text-ellipsis", "whitespace-nowrap"], |
| 123 | + "truncate", |
| 124 | + ), |
| 125 | + ], |
| 126 | + output: [`ctl("debug truncate")`], |
| 127 | + }, |
| 128 | + ], |
| 129 | +}); |
0 commit comments