|
| 1 | +import { RuleTester } from "@typescript-eslint/rule-tester" |
| 2 | +import { afterAll, describe, it } from "vitest" |
| 3 | + |
| 4 | +import { noSingleVariableMessage } from "./no-single-variable-message.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-single-variable-message", noSingleVariableMessage, { |
| 23 | + valid: [ |
| 24 | + // Template with text and variable |
| 25 | + "t`Hello ${name}`", |
| 26 | + "t`Status: ${status}`", |
| 27 | + "t`${count} items`", |
| 28 | + |
| 29 | + // Template with only text |
| 30 | + "t`Hello World`", |
| 31 | + |
| 32 | + // Template with multiple variables |
| 33 | + "t`${first} and ${second}`", |
| 34 | + |
| 35 | + // JSX with text and variable |
| 36 | + "<Trans>Hello {name}</Trans>", |
| 37 | + "<Trans>Status: {status}</Trans>", |
| 38 | + "<Trans>{count} items</Trans>", |
| 39 | + |
| 40 | + // JSX with only text |
| 41 | + "<Trans>Hello World</Trans>", |
| 42 | + |
| 43 | + // JSX with multiple children |
| 44 | + "<Trans>{first} and {second}</Trans>", |
| 45 | + |
| 46 | + // Non-Lingui tagged templates (should be ignored) |
| 47 | + "css`${variable}`", |
| 48 | + "html`${content}`", |
| 49 | + |
| 50 | + // Non-Trans JSX elements (should be ignored) |
| 51 | + "<Plural>{count}</Plural>", |
| 52 | + "<div>{content}</div>" |
| 53 | + ], |
| 54 | + invalid: [ |
| 55 | + // Template with only variable |
| 56 | + { |
| 57 | + code: "t`${status}`", |
| 58 | + errors: [{ messageId: "singleVariable" }] |
| 59 | + }, |
| 60 | + { |
| 61 | + code: "t`${user.name}`", |
| 62 | + errors: [{ messageId: "singleVariable" }] |
| 63 | + }, |
| 64 | + // Template with variable and whitespace only |
| 65 | + { |
| 66 | + code: "t` ${status} `", |
| 67 | + errors: [{ messageId: "singleVariable" }] |
| 68 | + }, |
| 69 | + |
| 70 | + // JSX with only variable |
| 71 | + { |
| 72 | + code: "<Trans>{status}</Trans>", |
| 73 | + errors: [{ messageId: "singleVariable" }] |
| 74 | + }, |
| 75 | + { |
| 76 | + code: "<Trans>{user.name}</Trans>", |
| 77 | + errors: [{ messageId: "singleVariable" }] |
| 78 | + }, |
| 79 | + // JSX with variable and whitespace only |
| 80 | + { |
| 81 | + code: "<Trans> {status} </Trans>", |
| 82 | + errors: [{ messageId: "singleVariable" }] |
| 83 | + } |
| 84 | + ] |
| 85 | +}) |
0 commit comments