|
| 1 | +import { RuleTester } from "@typescript-eslint/rule-tester" |
| 2 | +import { afterAll, describe, it } from "vitest" |
| 3 | + |
| 4 | +import { textRestrictions } from "./text-restrictions.js" |
| 5 | + |
| 6 | +RuleTester.afterAll = afterAll |
| 7 | +RuleTester.describe = describe |
| 8 | +RuleTester.it = it |
| 9 | + |
| 10 | +const ruleTester = new RuleTester({ |
| 11 | + languageOptions: { |
| 12 | + parserOptions: { |
| 13 | + ecmaVersion: 2022, |
| 14 | + sourceType: "module", |
| 15 | + ecmaFeatures: { |
| 16 | + jsx: true |
| 17 | + } |
| 18 | + } |
| 19 | + } |
| 20 | +}) |
| 21 | + |
| 22 | +ruleTester.run("text-restrictions", textRestrictions, { |
| 23 | + valid: [ |
| 24 | + // Default options (no restrictions) |
| 25 | + "t`Hello World`", |
| 26 | + "<Trans>Hello World</Trans>", |
| 27 | + |
| 28 | + // With minLength, messages meeting requirement |
| 29 | + { |
| 30 | + code: "t`Hello World`", |
| 31 | + options: [{ forbiddenPatterns: [], minLength: 5 }] |
| 32 | + }, |
| 33 | + { |
| 34 | + code: "<Trans>Hello World</Trans>", |
| 35 | + options: [{ forbiddenPatterns: [], minLength: 5 }] |
| 36 | + }, |
| 37 | + |
| 38 | + // With forbiddenPatterns, messages not matching |
| 39 | + { |
| 40 | + code: "t`Hello World`", |
| 41 | + options: [{ forbiddenPatterns: ["\\n", " "], minLength: null }] |
| 42 | + }, |
| 43 | + { |
| 44 | + code: "<Trans>Hello World</Trans>", |
| 45 | + options: [{ forbiddenPatterns: ["<br>", "TODO"], minLength: null }] |
| 46 | + }, |
| 47 | + |
| 48 | + // Empty messages don't trigger minLength (handled by other rules) |
| 49 | + { |
| 50 | + code: "t``", |
| 51 | + options: [{ forbiddenPatterns: [], minLength: 5 }] |
| 52 | + }, |
| 53 | + |
| 54 | + // Non-Lingui templates ignored |
| 55 | + { |
| 56 | + code: "css` `", |
| 57 | + options: [{ forbiddenPatterns: [" "], minLength: null }] |
| 58 | + }, |
| 59 | + |
| 60 | + // Non-Trans JSX ignored |
| 61 | + { |
| 62 | + code: "<div>X</div>", |
| 63 | + options: [{ forbiddenPatterns: [], minLength: 5 }] |
| 64 | + } |
| 65 | + ], |
| 66 | + invalid: [ |
| 67 | + // Forbidden pattern: HTML entity |
| 68 | + { |
| 69 | + code: "t`Hello World`", |
| 70 | + options: [{ forbiddenPatterns: [" "], minLength: null }], |
| 71 | + errors: [{ messageId: "forbiddenPattern" }] |
| 72 | + }, |
| 73 | + |
| 74 | + // Forbidden pattern in JSX |
| 75 | + { |
| 76 | + code: "<Trans>HelloTODOWorld</Trans>", |
| 77 | + options: [{ forbiddenPatterns: ["TODO"], minLength: null }], |
| 78 | + errors: [{ messageId: "forbiddenPattern" }] |
| 79 | + }, |
| 80 | + |
| 81 | + // Multiple forbidden patterns matched |
| 82 | + { |
| 83 | + code: "t`Hello &World`", |
| 84 | + options: [{ forbiddenPatterns: [" ", "&"], minLength: null }], |
| 85 | + errors: [ |
| 86 | + { messageId: "forbiddenPattern" }, |
| 87 | + { messageId: "forbiddenPattern" } |
| 88 | + ] |
| 89 | + }, |
| 90 | + |
| 91 | + // Too short message |
| 92 | + { |
| 93 | + code: "t`Hi`", |
| 94 | + options: [{ forbiddenPatterns: [], minLength: 5 }], |
| 95 | + errors: [{ messageId: "tooShort" }] |
| 96 | + }, |
| 97 | + { |
| 98 | + code: "t`X`", |
| 99 | + options: [{ forbiddenPatterns: [], minLength: 2 }], |
| 100 | + errors: [{ messageId: "tooShort" }] |
| 101 | + }, |
| 102 | + |
| 103 | + // Too short JSX message |
| 104 | + { |
| 105 | + code: "<Trans>Hi</Trans>", |
| 106 | + options: [{ forbiddenPatterns: [], minLength: 5 }], |
| 107 | + errors: [{ messageId: "tooShort" }] |
| 108 | + }, |
| 109 | + |
| 110 | + // Both forbidden pattern and too short |
| 111 | + { |
| 112 | + code: "t`X&`", |
| 113 | + options: [{ forbiddenPatterns: ["&"], minLength: 5 }], |
| 114 | + errors: [ |
| 115 | + { messageId: "forbiddenPattern" }, |
| 116 | + { messageId: "tooShort" } |
| 117 | + ] |
| 118 | + }, |
| 119 | + |
| 120 | + // Regex pattern |
| 121 | + { |
| 122 | + code: "t`Hello World!!!`", |
| 123 | + options: [{ forbiddenPatterns: ["!{2,}"], minLength: null }], |
| 124 | + errors: [{ messageId: "forbiddenPattern" }] |
| 125 | + } |
| 126 | + ] |
| 127 | +}) |
| 128 | + |
0 commit comments