fix: no-unlocalized-strings rule to correctly handle as const assertions in property values with ignoreNames #92
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description:
This PR fixes an issue in the
no-unlocalized-strings
ESLint rule where string literals assigned to object properties are incorrectly reported as unlocalized strings when using TypeScript'sas const
assertions, even if the property names are specified in theignoreNames
option.Background:
When using the
ignoreNames
option with a regex pattern to ignore certain property names, the rule should not report string literals assigned to those properties. However, if the property value includes anas const
assertion, the rule fails to recognize it due to theTSAsExpression
node introduced by the TypeScript assertion.Example:
Given the ESLint configuration:
The following code incorrectly reports errors for the string literals
"circle"
and"rectangle"
:Cause of the Issue:
The
as const
assertion wraps the string literal in aTSAsExpression
node in the Abstract Syntax Tree (AST). The existing rule does not handleTSAsExpression
nodes when processing property values, so it fails to apply theignoreNames
logic to the unwrapped string literals.Solution:
The fix involves updating the rule to handle
TSAsExpression
nodes by unwrapping them to access the underlying string literals. Specifically:Implemented an Unwrapping Helper Function:
Added a helper function
unwrapTSAsExpression
to recursively unwrapTSAsExpression
nodes:Updated the
isStringLiteral
Function:Modified
isStringLiteral
to handleTSAsExpression
nodes:Modified the Visitor to Handle
TSAsExpression
Nodes:Updated the visitor function for
Property
nodes to unwrapTSAsExpression
nodes when processing property values:Testing:
ignoreNames
option configured, the linter no longer reports errors for string literals assigned to property names matching the regex pattern, even when usingas const
assertions.as const
assertions in property values.Notes:
TSAsExpression
nodes in property values, avoiding changes that could affect other parts of the rule.Conclusion:
This PR improves the
no-unlocalized-strings
rule by correctly handlingas const
assertions in property values when using theignoreNames
option. It ensures that string literals assigned to ignored property names are not incorrectly reported as unlocalized strings.