Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(no-unlocalized-strings): ignore more cases #84

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 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 Expand Up @@ -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",
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
55 changes: 55 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,18 @@ 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 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'] }],
Expand Down Expand Up @@ -92,6 +104,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 @@ -125,6 +146,10 @@ ruleTester.run<string, Option[]>(name, rule, {
{ code: '<img src={`./image.png`} />' },
{ code: '<button type="button" for="form-id" />' },
{ code: '<button type={`button`} for={`form-id`} />' },
{
name: 'JSX Space should not be flagged',
code: `<button>{' '}</button>`,
},
{ code: '<DIV foo="bar" />', options: [{ ignoreNames: ['foo'] }] },
{ code: '<DIV foo={`Bar`} />', options: [{ ignoreNames: ['foo'] }] },
{
Expand Down Expand Up @@ -362,6 +387,12 @@ jsxTester.run('no-unlocalized-strings', rule, {
valid: [
{ code: '<Component>{ i18n._("abc") }</Component>' },
{ code: '<Component>{ i18n._(`abc`) }</Component>' },
{
name: 'Should not flag the JSX text with only spaces and interpolations',
code: `<Component>
{variable}
</Component>`,
},
{ code: '<Trans>Hello</Trans>' },
{ code: '<Trans><Component>Hello</Component></Trans>' },
{
Expand Down Expand Up @@ -415,3 +446,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)
})
})