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 rule to ignore string literals in expressions assigned to variables specified in ignoreNames #94

Merged
merged 16 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
51 changes: 39 additions & 12 deletions src/rules/no-unlocalized-strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,24 +471,31 @@
processTextNode(node)
},
'Literal:exit'(node: TSESTree.Literal) {
// visited and passed linting
if (visited.has(node)) return
const trimmed = `${node.value}`.trim()
if (!trimmed) return

if (isTextWhiteListed(trimmed)) return

//
// TYPESCRIPT
// todo: that doesn't work
// if (tsService) {
// const typeObj = tsService.getTypeAtLocation(node.parent)
//
// // var a: 'abc' = 'abc'
// if (typeObj.isStringLiteral() && typeObj.symbol) {
// return
// }
// }
// Check if this literal is part of a VariableDeclarator or AssignmentExpression with id/name in ignoreNames
let parent = node.parent
while (parent) {
if (parent.type === TSESTree.AST_NODE_TYPES.VariableDeclarator) {
const variableDeclarator = parent as TSESTree.VariableDeclarator
if (isIdentifier(variableDeclarator.id) && isIgnoredName(variableDeclarator.id.name)) {
return // Do not report this literal
}
} else if (parent.type === TSESTree.AST_NODE_TYPES.AssignmentExpression) {
const assignmentExpression = parent as TSESTree.AssignmentExpression
if (
isIdentifier(assignmentExpression.left) &&
isIgnoredName(assignmentExpression.left.name)
) {
return // Do not report this literal
}
}
parent = parent.parent
}

context.report({ node, messageId: 'default' })
},
Expand All @@ -498,6 +505,26 @@

if (!text || isTextWhiteListed(text)) return

// Check if this template literal is part of a VariableDeclarator or AssignmentExpression with id/name in ignoreNames
let parent = node.parent
while (parent) {
if (parent.type === TSESTree.AST_NODE_TYPES.VariableDeclarator) {
const variableDeclarator = parent as TSESTree.VariableDeclarator
if (isIdentifier(variableDeclarator.id) && isIgnoredName(variableDeclarator.id.name)) {
return // Do not report this template literal

Check warning on line 514 in src/rules/no-unlocalized-strings.ts

View check run for this annotation

Codecov / codecov/patch

src/rules/no-unlocalized-strings.ts#L514

Added line #L514 was not covered by tests
}
} else if (parent.type === TSESTree.AST_NODE_TYPES.AssignmentExpression) {
const assignmentExpression = parent as TSESTree.AssignmentExpression

Check warning on line 517 in src/rules/no-unlocalized-strings.ts

View check run for this annotation

Codecov / codecov/patch

src/rules/no-unlocalized-strings.ts#L517

Added line #L517 was not covered by tests
if (
isIdentifier(assignmentExpression.left) &&
isIgnoredName(assignmentExpression.left.name)

Check warning on line 520 in src/rules/no-unlocalized-strings.ts

View check run for this annotation

Codecov / codecov/patch

src/rules/no-unlocalized-strings.ts#L520

Added line #L520 was not covered by tests
) {
return // Do not report this template literal

Check warning on line 522 in src/rules/no-unlocalized-strings.ts

View check run for this annotation

Codecov / codecov/patch

src/rules/no-unlocalized-strings.ts#L522

Added line #L522 was not covered by tests
}
}
parent = parent.parent
}

context.report({ node, messageId: 'default' })
},

Expand Down
31 changes: 31 additions & 0 deletions tests/src/rules/no-unlocalized-strings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,31 @@ ruleTester.run<string, Option[]>(name, rule, {
name: 'computed keys should be ignored by default with TplLiteral',
code: `obj[\`key with space\`] = 5`,
},
{
name: 'Supports default value assignment',
code: 'const variant = input || "body"',
options: [{ ignoreNames: ['variant'] }],
},
{
name: 'Supports nullish coalescing operator',
code: 'const variant = input ?? "body"',
options: [{ ignoreNames: ['variant'] }],
},
{
name: 'Supports ternary operator',
code: 'const value = condition ? "yes" : "no"',
options: [{ ignoreNames: ['value'] }],
},
{
name: 'Ignores literals in assignment expression after variable declaration',
code: `let variant; variant = input ?? "body";`,
options: [{ ignoreNames: ['variant'] }],
},
{
name: 'Ignores literals in assignment expression to variable in ignoreNames',
code: `let variant; variant = "body";`,
options: [{ ignoreNames: ['variant'] }],
},
{
code: `const test = "Hello!"`,
options: [{ ignoreNames: ['test'] }],
Expand Down Expand Up @@ -296,6 +321,12 @@ ruleTester.run<string, Option[]>(name, rule, {
{ code: 'export const a = `hello string`;', errors },
{ code: "const ge = 'Select tax code'", errors },
{ code: 'const a = `Foo`;', errors },
{
name: 'Reports error when variable name is not in ignoreNames',
code: 'const other = input ?? "body";',
options: [{ ignoreNames: ['variant'] }],
errors: [{ messageId: 'default', line: 1, column: 24 }],
},
{ code: 'const a = call(`Ffo`);', errors },
{ code: 'var a = {foo: `Bar`};', errors },
{
Expand Down
Loading