Skip to content

Commit

Permalink
fix(no-unlocalized-strings): ignore more cases
Browse files Browse the repository at this point in the history
- ignore when there is no text, and only variables
- apply `ignore` option to the JSX Text
- improve default Regexp in the documentation
  • Loading branch information
timofei-iatsenko committed Nov 24, 2024
1 parent 00bfc7c commit d9ca52b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
6 changes: 3 additions & 3 deletions docs/rules/no-unlocalized-strings.md
Original file line number Diff line number Diff line change
Expand Up @@ -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_-]+$"
Expand Down
6 changes: 3 additions & 3 deletions src/rules/no-unlocalized-strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ export const rule = createRule<Option[], string>({
visited.add(node)

const text = getText(node)
if (!text || isIgnoredSymbol(text)) {
if (!text || isIgnoredSymbol(text) || isTextWhiteListed(text)) {
return
}

Expand Down Expand Up @@ -481,9 +481,9 @@ export const rule = createRule<Option[], string>({
},
'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' })
},
Expand Down
41 changes: 41 additions & 0 deletions tests/src/rules/no-unlocalized-strings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ ruleTester.run<string, Option[]>(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'] }],
Expand Down Expand Up @@ -92,6 +100,15 @@ ruleTester.run<string, Option[]>(name, rule, {
// // JSX
{ code: '<div className="primary"></div>' },
{ code: '<div className={`primary`}></div>' },
{
name: 'Should ignore non-word strings in the JSX Text',
code: `<span>+</span>`,
},
{
name: 'Should JSX Text if it matches the ignore option',
code: `<span>foo</span>`,
options: [{ ignore: ['^foo'] }],
},
{ code: '<div className={a ? "active": "inactive"}></div>' },
{ code: '<div className={a ? `active`: `inactive`}></div>' },
{ code: '<div>{i18n._("foo")}</div>' },
Expand Down Expand Up @@ -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)
})
})

0 comments on commit d9ca52b

Please sign in to comment.