|
| 1 | +import { TSESLint } from "@typescript-eslint/experimental-utils" |
| 2 | +import rule from "../rules/string-unions.js" |
| 3 | +import { createTsRuleTester } from "../test-utils.js" |
| 4 | + |
| 5 | +const ruleTester = createTsRuleTester() |
| 6 | + |
| 7 | +const createValidCodeVariants = ( |
| 8 | + code: string |
| 9 | +): TSESLint.RunTests< |
| 10 | + "unsorted", |
| 11 | + [{ caseSensitive?: boolean; natural?: boolean }] |
| 12 | +>["valid"] => [ |
| 13 | + { code, options: [{ caseSensitive: false, natural: false }] }, |
| 14 | + { code, options: [{ caseSensitive: true, natural: false }] }, |
| 15 | + { code, options: [{ caseSensitive: false, natural: true }] }, |
| 16 | + { code, options: [{ caseSensitive: true, natural: true }] }, |
| 17 | +] |
| 18 | + |
| 19 | +ruleTester.run("sort/string-unions", rule, { |
| 20 | + valid: [ |
| 21 | + ...createValidCodeVariants("type A = 'a'"), |
| 22 | + ...createValidCodeVariants("type A = 'a' | 'b'"), |
| 23 | + ...createValidCodeVariants("type A = '_' | 'a' | 'b'"), |
| 24 | + |
| 25 | + // Ignores mixed types |
| 26 | + ...createValidCodeVariants("type A = 'b' | 'a' | boolean"), |
| 27 | + ...createValidCodeVariants("type A = 'b' | {type:string} | 'a'"), |
| 28 | + |
| 29 | + // Options |
| 30 | + { |
| 31 | + code: "type A = 'a1' | 'A1' | 'a12' | 'a2' | 'B2'", |
| 32 | + options: [{ caseSensitive: false, natural: false }], |
| 33 | + }, |
| 34 | + { |
| 35 | + code: "type A = 'A1' | 'B1' | 'a1' | 'a12' | 'a2'", |
| 36 | + options: [{ caseSensitive: true, natural: false }], |
| 37 | + }, |
| 38 | + { |
| 39 | + code: "type A = 'a1' | 'A1' | 'a2' | 'a12' | 'B2'", |
| 40 | + options: [{ caseSensitive: false, natural: true }], |
| 41 | + }, |
| 42 | + { |
| 43 | + code: "type A = 'A1' | 'B2' | 'a1' | 'a2' | 'a12'", |
| 44 | + options: [{ caseSensitive: true, natural: true }], |
| 45 | + }, |
| 46 | + ], |
| 47 | + invalid: [ |
| 48 | + { |
| 49 | + code: "type A = 'b' | 'a'", |
| 50 | + output: "type A = 'a' | 'b'", |
| 51 | + errors: [{ messageId: "unsorted" }], |
| 52 | + }, |
| 53 | + { |
| 54 | + code: "type A = 'b' | 'a' | 'c'", |
| 55 | + output: "type A = 'a' | 'b' | 'c'", |
| 56 | + errors: [{ messageId: "unsorted" }], |
| 57 | + }, |
| 58 | + { |
| 59 | + code: "type A = 'b' | '_' | 'c'", |
| 60 | + output: "type A = '_' | 'b' | 'c'", |
| 61 | + errors: [{ messageId: "unsorted" }], |
| 62 | + }, |
| 63 | + |
| 64 | + // Options |
| 65 | + { |
| 66 | + code: "type A = 'a12' | 'B2' | 'a1' | 'a2'", |
| 67 | + output: "type A = 'a1' | 'a12' | 'a2' | 'B2'", |
| 68 | + options: [{ caseSensitive: false, natural: false }], |
| 69 | + errors: [{ messageId: "unsorted" }], |
| 70 | + }, |
| 71 | + { |
| 72 | + code: "type A = 'a1' | 'B2' | 'a2' | 'a12'", |
| 73 | + output: "type A = 'B2' | 'a1' | 'a12' | 'a2'", |
| 74 | + options: [{ caseSensitive: true, natural: false }], |
| 75 | + errors: [{ messageId: "unsorted" }], |
| 76 | + }, |
| 77 | + { |
| 78 | + code: "type A = 'a2' | 'a1' | 'a12' | 'B2'", |
| 79 | + output: "type A = 'a1' | 'a2' | 'a12' | 'B2'", |
| 80 | + options: [{ caseSensitive: false, natural: true }], |
| 81 | + errors: [{ messageId: "unsorted" }], |
| 82 | + }, |
| 83 | + { |
| 84 | + code: "type A = 'a12' | 'a2' | 'B2' | 'a1'", |
| 85 | + output: "type A = 'B2' | 'a1' | 'a2' | 'a12'", |
| 86 | + options: [{ caseSensitive: true, natural: true }], |
| 87 | + errors: [{ messageId: "unsorted" }], |
| 88 | + }, |
| 89 | + ], |
| 90 | +}) |
0 commit comments