From d9ca52ba75f7ddc5ffa3762ca2c34c134de2ac89 Mon Sep 17 00:00:00 2001 From: Timofei Iatsenko Date: Fri, 22 Nov 2024 12:27:50 +0100 Subject: [PATCH 1/4] fix(no-unlocalized-strings): ignore more cases - ignore when there is no text, and only variables - apply `ignore` option to the JSX Text - improve default Regexp in the documentation --- docs/rules/no-unlocalized-strings.md | 6 +-- src/rules/no-unlocalized-strings.ts | 6 +-- .../src/rules/no-unlocalized-strings.test.ts | 41 +++++++++++++++++++ 3 files changed, 47 insertions(+), 6 deletions(-) diff --git a/docs/rules/no-unlocalized-strings.md b/docs/rules/no-unlocalized-strings.md index 836fcd0..2df86bc 100644 --- a/docs/rules/no-unlocalized-strings.md +++ b/docs/rules/no-unlocalized-strings.md @@ -16,9 +16,9 @@ The rule doesn’t come with built-in ignore settings because each project is un "error", { "ignore": [ - // Ignore strings that don’t start with an uppercase letter - // or don't contain two words separated by whitespace - "^(?![A-Z].*|\\w+\\s\\w+).+$", + // Ignore strings which are a single "word" (no spaces) + // and doesn't start with an uppercase letter + "^(?![A-Z])\\S+$", // Ignore UPPERCASE literals // Example: const test = "FOO" "^[A-Z0-9_-]+$" diff --git a/src/rules/no-unlocalized-strings.ts b/src/rules/no-unlocalized-strings.ts index 24d244c..490015d 100644 --- a/src/rules/no-unlocalized-strings.ts +++ b/src/rules/no-unlocalized-strings.ts @@ -243,7 +243,7 @@ export const rule = createRule({ visited.add(node) const text = getText(node) - if (!text || isIgnoredSymbol(text)) { + if (!text || isIgnoredSymbol(text) || isTextWhiteListed(text)) { return } @@ -481,9 +481,9 @@ export const rule = createRule({ }, 'TemplateLiteral:exit'(node: TSESTree.TemplateLiteral) { if (visited.has(node)) return - const quasisValue = getText(node) + const text = getText(node) - if (isTextWhiteListed(quasisValue)) return + if (!text || isTextWhiteListed(text)) return context.report({ node, messageId: 'default' }) }, diff --git a/tests/src/rules/no-unlocalized-strings.test.ts b/tests/src/rules/no-unlocalized-strings.test.ts index bdaa34f..096b795 100644 --- a/tests/src/rules/no-unlocalized-strings.test.ts +++ b/tests/src/rules/no-unlocalized-strings.test.ts @@ -35,6 +35,14 @@ ruleTester.run(name, rule, { name: 'should ignore non word strings 2', code: 'const a = `0123456789!@#$%^&*()_+|~-=\\`[]{};\':",./<>?`;', }, + { + name: 'should ignore strings containing only variables', + code: 'const t = `${BRAND_NAME}`', + }, + { + name: 'should ignore strings containing only variables 2', + code: 'const t = `${BRAND_NAME}${BRAND_NAME}`', + }, { code: 'hello("Hello")', options: [{ ignoreFunctions: ['hello'] }], @@ -92,6 +100,15 @@ ruleTester.run(name, rule, { // // JSX { code: '
' }, { code: '
' }, + { + name: 'Should ignore non-word strings in the JSX Text', + code: `+`, + }, + { + name: 'Should JSX Text if it matches the ignore option', + code: `foo`, + options: [{ ignore: ['^foo'] }], + }, { code: '
' }, { code: '
' }, { code: '
{i18n._("foo")}
' }, @@ -415,3 +432,27 @@ jsxTester.run('no-unlocalized-strings', rule, { }, ], }) + +/** + * This test is covering the ignore regex proposed in the documentation + * This regex doesn't used directly in the code. + */ +describe('Default ignore regex', () => { + const regex = '^(?![A-Z])\\S+$' + + test.each([ + ['hello', true], + ['helloMyVar', true], + ['package.json', true], + ['./src/**/*.test*', true], + ['camel_case', true], + + // Start from capital letter + ['Hello', false], + // Multiword string (has space) + ['hello world', false], + ['Hello World', false], + ])('validate %s', (str, pass) => { + expect(new RegExp(regex).test(str)).toBe(pass) + }) +}) From 8b536b7bd5a576c86d23709478600c8dceeb6c07 Mon Sep 17 00:00:00 2001 From: Timofei Iatsenko Date: Sun, 24 Nov 2024 11:08:47 +0100 Subject: [PATCH 2/4] docs(no-unlocalized-strings): fix typo in default configuration --- docs/rules/no-unlocalized-strings.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/rules/no-unlocalized-strings.md b/docs/rules/no-unlocalized-strings.md index 2df86bc..402e0ae 100644 --- a/docs/rules/no-unlocalized-strings.md +++ b/docs/rules/no-unlocalized-strings.md @@ -60,7 +60,7 @@ The rule doesn’t come with built-in ignore settings because each project is un ], // Following settings require typed linting https://typescript-eslint.io/getting-started/typed-linting/ "useTsTypes": true, - "ignoreMethodsOnType": [ + "ignoreMethodsOnTypes": [ // Ignore specified methods on Map and Set types "Map.get", "Map.has", From 8392cacdd08325b2aa5cc914be016f4a7f94bad1 Mon Sep 17 00:00:00 2001 From: Timofei Iatsenko Date: Sun, 24 Nov 2024 11:18:29 +0100 Subject: [PATCH 3/4] test(no-unlocalized-strings): add more test cases --- tests/src/rules/no-unlocalized-strings.test.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/tests/src/rules/no-unlocalized-strings.test.ts b/tests/src/rules/no-unlocalized-strings.test.ts index 096b795..e72472e 100644 --- a/tests/src/rules/no-unlocalized-strings.test.ts +++ b/tests/src/rules/no-unlocalized-strings.test.ts @@ -40,9 +40,13 @@ ruleTester.run(name, rule, { code: 'const t = `${BRAND_NAME}`', }, { - name: 'should ignore strings containing only variables 2', + name: 'should ignore strings containing few variables', code: 'const t = `${BRAND_NAME}${BRAND_NAME}`', }, + { + name: 'should ignore strings containing few variables with spaces', + code: 'const t = ` ${BRAND_NAME} ${BRAND_NAME} `', + }, { code: 'hello("Hello")', options: [{ ignoreFunctions: ['hello'] }], @@ -142,6 +146,10 @@ ruleTester.run(name, rule, { { code: '' }, { code: '`, }, { code: '
', options: [{ ignoreNames: ['foo'] }] }, { code: '
', options: [{ ignoreNames: ['foo'] }] },