|
| 1 | +import { RuleTester } from "@typescript-eslint/rule-tester" |
| 2 | +import { afterAll, describe, it } from "vitest" |
| 3 | + |
| 4 | +import { validTCallLocation } from "./valid-t-call-location.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("valid-t-call-location", validTCallLocation, { |
| 23 | + valid: [ |
| 24 | + // Inside function declaration |
| 25 | + `function Component() { |
| 26 | + const msg = t\`Hello\` |
| 27 | + return msg |
| 28 | + }`, |
| 29 | + |
| 30 | + // Inside arrow function |
| 31 | + `const Component = () => { |
| 32 | + const msg = t\`Hello\` |
| 33 | + return msg |
| 34 | + }`, |
| 35 | + |
| 36 | + // Inside function expression |
| 37 | + `const getMsg = function() { |
| 38 | + return t\`Hello\` |
| 39 | + }`, |
| 40 | + |
| 41 | + // Inside method |
| 42 | + `class MyClass { |
| 43 | + getMessage() { |
| 44 | + return t\`Hello\` |
| 45 | + } |
| 46 | + }`, |
| 47 | + |
| 48 | + // Inside React component |
| 49 | + `function App() { |
| 50 | + return <div>{t\`Welcome\`}</div> |
| 51 | + }`, |
| 52 | + |
| 53 | + // Inside callback |
| 54 | + `items.map(() => t\`Item\`)`, |
| 55 | + |
| 56 | + // Inside event handler |
| 57 | + `function Component() { |
| 58 | + const handleClick = () => { |
| 59 | + alert(t\`Clicked\`) |
| 60 | + } |
| 61 | + }`, |
| 62 | + |
| 63 | + // Inside hook |
| 64 | + `function useTranslation() { |
| 65 | + return { message: t\`Hello\` } |
| 66 | + }`, |
| 67 | + |
| 68 | + // Non-t tagged templates at top level are fine |
| 69 | + `const styles = css\`color: red\``, |
| 70 | + `const html = html\`<div></div>\``, |
| 71 | + |
| 72 | + // Allow top level when configured |
| 73 | + { |
| 74 | + code: `const msg = t\`Hello\``, |
| 75 | + options: [{ allowTopLevel: true }] |
| 76 | + } |
| 77 | + ], |
| 78 | + invalid: [ |
| 79 | + // Top-level variable declaration |
| 80 | + { |
| 81 | + code: `const msg = t\`Hello\``, |
| 82 | + errors: [{ messageId: "topLevelNotAllowed" }] |
| 83 | + }, |
| 84 | + |
| 85 | + // Top-level export |
| 86 | + { |
| 87 | + code: `export const msg = t\`Welcome\``, |
| 88 | + errors: [{ messageId: "topLevelNotAllowed" }] |
| 89 | + }, |
| 90 | + |
| 91 | + // Multiple top-level usages |
| 92 | + { |
| 93 | + code: ` |
| 94 | + const greeting = t\`Hello\` |
| 95 | + const farewell = t\`Goodbye\` |
| 96 | + `, |
| 97 | + errors: [ |
| 98 | + { messageId: "topLevelNotAllowed" }, |
| 99 | + { messageId: "topLevelNotAllowed" } |
| 100 | + ] |
| 101 | + }, |
| 102 | + |
| 103 | + // Inside class property (not method) |
| 104 | + { |
| 105 | + code: ` |
| 106 | + class MyClass { |
| 107 | + message = t\`Hello\` |
| 108 | + } |
| 109 | + `, |
| 110 | + errors: [{ messageId: "topLevelNotAllowed" }] |
| 111 | + }, |
| 112 | + |
| 113 | + // Inside object at top level |
| 114 | + { |
| 115 | + code: ` |
| 116 | + const config = { |
| 117 | + message: t\`Hello\` |
| 118 | + } |
| 119 | + `, |
| 120 | + errors: [{ messageId: "topLevelNotAllowed" }] |
| 121 | + } |
| 122 | + ] |
| 123 | +}) |
| 124 | + |
0 commit comments