|
| 1 | +import { RuleTester } from "@typescript-eslint/rule-tester" |
| 2 | +import { afterAll, describe, it } from "vitest" |
| 3 | + |
| 4 | +import { noUnlocalizedStrings } from "./no-unlocalized-strings.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("no-unlocalized-strings", noUnlocalizedStrings, { |
| 23 | + valid: [ |
| 24 | + // Inside t`` |
| 25 | + "t`Hello World`", |
| 26 | + "t`Save changes`", |
| 27 | + |
| 28 | + // Inside <Trans> |
| 29 | + "<Trans>Hello World</Trans>", |
| 30 | + "<Trans>Save changes</Trans>", |
| 31 | + |
| 32 | + // Inside msg/defineMessage |
| 33 | + 'msg({ message: "Hello World" })', |
| 34 | + 'defineMessage({ message: "Save changes" })', |
| 35 | + |
| 36 | + // Console/debug (default ignored functions) |
| 37 | + 'console.log("Hello World")', |
| 38 | + 'console.error("Something went wrong")', |
| 39 | + |
| 40 | + // Ignored properties (className, type, etc.) |
| 41 | + '<div className="my-class" />', |
| 42 | + '<input type="text" />', |
| 43 | + '<div id="my-id" />', |
| 44 | + '<div data-testid="test-button" />', |
| 45 | + '<a href="/path/to/page" />', |
| 46 | + |
| 47 | + // Object properties with ignored keys |
| 48 | + '({ type: "button" })', |
| 49 | + '({ className: "my-class" })', |
| 50 | + |
| 51 | + // Technical strings (no spaces, identifiers) |
| 52 | + 'const x = "myIdentifier"', |
| 53 | + 'const x = "my-css-class"', |
| 54 | + 'const x = "CONSTANT_VALUE"', |
| 55 | + |
| 56 | + // URLs and paths |
| 57 | + 'const url = "https://example.com"', |
| 58 | + 'const path = "/api/users"', |
| 59 | + 'const mailto = "mailto:test@example.com"', |
| 60 | + |
| 61 | + // Single characters |
| 62 | + 'const sep = "-"', |
| 63 | + 'const x = "."', |
| 64 | + |
| 65 | + // Empty strings |
| 66 | + 'const x = ""', |
| 67 | + 'const x = " "', |
| 68 | + |
| 69 | + // Type contexts |
| 70 | + 'type Status = "loading" | "error"', |
| 71 | + "interface Props { variant: 'primary' | 'secondary' }", |
| 72 | + |
| 73 | + // Ignore pattern |
| 74 | + { |
| 75 | + code: 'const x = "test_id_123"', |
| 76 | + options: [{ ignoreFunctions: [], ignoreProperties: [], ignoreNames: [], ignorePattern: "^test_" }] |
| 77 | + }, |
| 78 | + |
| 79 | + // Non-UI looking text |
| 80 | + 'const x = "myFunction"', |
| 81 | + 'const x = "onClick"' |
| 82 | + ], |
| 83 | + invalid: [ |
| 84 | + // Plain string that looks like UI text |
| 85 | + { |
| 86 | + code: 'const label = "Save changes"', |
| 87 | + errors: [{ messageId: "unlocalizedString" }] |
| 88 | + }, |
| 89 | + { |
| 90 | + code: 'const msg = "Something went wrong!"', |
| 91 | + errors: [{ messageId: "unlocalizedString" }] |
| 92 | + }, |
| 93 | + { |
| 94 | + code: 'const title = "Welcome to the app"', |
| 95 | + errors: [{ messageId: "unlocalizedString" }] |
| 96 | + }, |
| 97 | + |
| 98 | + // JSX text |
| 99 | + { |
| 100 | + code: "<button>Save changes</button>", |
| 101 | + errors: [{ messageId: "unlocalizedString" }] |
| 102 | + }, |
| 103 | + { |
| 104 | + code: "<div>Something went wrong!</div>", |
| 105 | + errors: [{ messageId: "unlocalizedString" }] |
| 106 | + }, |
| 107 | + { |
| 108 | + code: "<p>Please try again.</p>", |
| 109 | + errors: [{ messageId: "unlocalizedString" }] |
| 110 | + }, |
| 111 | + |
| 112 | + // JSX with title/aria-label that's not in default ignore list |
| 113 | + { |
| 114 | + code: '<button title="Click here">X</button>', |
| 115 | + errors: [{ messageId: "unlocalizedString" }] |
| 116 | + }, |
| 117 | + |
| 118 | + // Multiple violations |
| 119 | + { |
| 120 | + code: ` |
| 121 | + const a = "Hello World" |
| 122 | + const b = "Goodbye World" |
| 123 | + `, |
| 124 | + errors: [ |
| 125 | + { messageId: "unlocalizedString" }, |
| 126 | + { messageId: "unlocalizedString" } |
| 127 | + ] |
| 128 | + }, |
| 129 | + |
| 130 | + // Function return value |
| 131 | + { |
| 132 | + code: 'function getLabel() { return "Click me" }', |
| 133 | + errors: [{ messageId: "unlocalizedString" }] |
| 134 | + }, |
| 135 | + |
| 136 | + // Object property (non-ignored key) |
| 137 | + { |
| 138 | + code: '({ label: "Save changes" })', |
| 139 | + errors: [{ messageId: "unlocalizedString" }] |
| 140 | + }, |
| 141 | + { |
| 142 | + code: '({ message: "Error occurred!" })', |
| 143 | + errors: [{ messageId: "unlocalizedString" }] |
| 144 | + } |
| 145 | + ] |
| 146 | +}) |
| 147 | + |
0 commit comments