Skip to content

Commit

Permalink
fix: reworked string literal check to correctly support TS as const
Browse files Browse the repository at this point in the history
  • Loading branch information
swernerx committed Dec 4, 2024
1 parent 5c64f84 commit 011de11
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
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
4 changes: 4 additions & 0 deletions tests/src/rules/no-unlocalized-strings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ ruleTester.run<string, Option[]>(name, rule, {
code: 'class Form extends Component { displayName = "FormContainer" };',
options: [{ ignoreNames: ['displayName'] }],
},
{
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

0 comments on commit 011de11

Please sign in to comment.