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 correctly handle as const assertions in property values with ignoreNames #92

Merged
merged 2 commits into from
Dec 5, 2024
Merged
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
41 changes: 27 additions & 14 deletions src/rules/no-unlocalized-strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ function createMatcher(patterns: MatcherDef[]) {
}
}

function unwrapTSAsExpression(
node: TSESTree.Expression,
): TSESTree.Literal | TSESTree.TemplateLiteral | TSESTree.Expression {
while (node.type === TSESTree.AST_NODE_TYPES.TSAsExpression) {
node = node.expression
}
return node
}

export const name = 'no-unlocalized-strings'
export const rule = createRule<Option[], string>({
name,
Expand Down Expand Up @@ -217,7 +226,13 @@ export const rule = createRule<Option[], string>({

const isIgnoredName = createMatcher(option?.ignoreNames || [])

function isStringLiteral(node: TSESTree.Literal | TSESTree.TemplateLiteral | TSESTree.JSXText) {
function isStringLiteral(node: TSESTree.Node | null | undefined): boolean {
if (!node) return false

if (node.type === TSESTree.AST_NODE_TYPES.TSAsExpression) {
return isStringLiteral(node.expression)
}

switch (node.type) {
case TSESTree.AST_NODE_TYPES.Literal:
return typeof node.value === 'string'
Expand Down Expand Up @@ -355,24 +370,22 @@ export const rule = createRule<Option[], string>({
visited.add(node)
}
},
'Property > :matches(Literal,TemplateLiteral)'(
node: TSESTree.Literal | TSESTree.TemplateLiteral,
'Property > :matches(Literal,TemplateLiteral,TSAsExpression)'(
node: TSESTree.Literal | TSESTree.TemplateLiteral | TSESTree.TSAsExpression,
) {
const parent = node.parent as TSESTree.Property

// {A_B: "hello world"};
// ^^^^
if (isIdentifier(parent.key) && isIgnoredName(parent.key.name)) {
visited.add(node)
}

// {["A_B"]: "hello world"};
// ^^^^
if (
(isLiteral(parent.key) || isTemplateLiteral(parent.key)) &&
isIgnoredName(getText(parent.key))
(isIdentifier(parent.key) && isIgnoredName(parent.key.name)) ||
((isLiteral(parent.key) || isTemplateLiteral(parent.key)) &&
isIgnoredName(getText(parent.key)))
) {
visited.add(node)
// Unwrap TSAsExpression nodes
const unwrappedNode = unwrapTSAsExpression(node)

if (isLiteral(unwrappedNode) || isTemplateLiteral(unwrappedNode)) {
visited.add(unwrappedNode)
}
}
},
'MemberExpression[computed=true] > :matches(Literal,TemplateLiteral)'(
Expand Down
9 changes: 7 additions & 2 deletions tests/src/rules/no-unlocalized-strings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,11 @@ ruleTester.run<string, Option[]>(name, rule, {
code: 'function Input({ intent = "none"}) {}',
options: [{ ignoreNames: ['intent'] }],
},
{
name: "Should support ignoreNames when applied the 'as const' assertion",
code: 'const Shape = { CIRCLE: "circle" as const };',
options: [{ ignoreNames: [{ regex: { pattern: '^[A-Z0-9_-]+$' } }] }],
},
{
name: 'computed keys should be ignored by default, StringLiteral',
code: `obj["key with space"] = 5`,
Expand Down Expand Up @@ -267,13 +272,13 @@ ruleTester.run<string, Option[]>(name, rule, {
},

{
code: `const myMap = new Map();
code: `const myMap = new Map();
myMap.get("string with a spaces")
myMap.has("string with a spaces")`,
options: [{ useTsTypes: true, ignoreMethodsOnTypes: ['Map.get', 'Map.has'] }],
},
{
code: `interface Foo {get: (key: string) => string};
code: `interface Foo {get: (key: string) => string};
(foo as Foo).get("string with a spaces")`,
options: [{ useTsTypes: true, ignoreMethodsOnTypes: ['Foo.get'] }],
},
Expand Down
Loading