-
Notifications
You must be signed in to change notification settings - Fork 13
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
fix: no-unlocalized-strings
rule to ignore string literals in expressions assigned to variables specified in ignoreNames
#94
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #94 +/- ##
==========================================
+ Coverage 96.60% 97.18% +0.58%
==========================================
Files 10 10
Lines 383 427 +44
Branches 105 131 +26
==========================================
+ Hits 370 415 +45
+ Misses 13 12 -1 ☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks like there is a code duplication which could be extracted into a reusable function.
Also please add the same set of test where TemplateLiteral is used all these cases.
I have refactored the code to extract the logic and also added tests for template literals. This also enhanced code coverage. |
it seems that might also cover the cases where something more complex is assigned to variable {
code: 'const variant = myFunction({someProperty: "Hello World!"})',
options: [{ ignoreNames: ['variant'] }],
}, Could you test it locally, am I correct with that? PS. I could not say is it good or bad. If it's also covering this, for some users that might be confusing, because "why argument of the From other side it still might be good, i recently had such case, don't remember exactly what it was, but I wished that this behavior would be there. |
I guess this is indeed not good. I would expect to receive to get an error for |
I pushed another rework of the code to prevent these cases. I also added the check to the error section of the test suite. |
I made some adjustments to the handling of JSX strings. Based on my understanding, expressions like Additionally, I added |
I applied some more tests from issues I have seen in our code base. Looks good for existing test cases. |
LGTM |
@swernerx thanks for the contribution, and @timofei-iatsenko thanks for the review! |
Description:
This PR addresses an issue in the
no-unlocalized-strings
ESLint rule where string literals within expressions assigned to variables are incorrectly reported as unlocalized strings, even when the variable names are specified in theignoreNames
option.Background:
When using the
ignoreNames
option to ignore certain variable names, the rule should not report string literals assigned to those variables. However, if the string literal is part of an expression (e.g., using the nullish coalescing operator??
, logical OR||
, or ternary? :
), the rule currently fails to recognize it and reports an error.Example:
Given the ESLint configuration:
The following code incorrectly reports an error for the string literals
"body"
,"yes"
, and"no"
:Cause of the Issue:
The rule does not currently check whether a string literal is part of an expression assigned to a variable specified in
ignoreNames
. It only handles direct assignments of string literals.Solution:
To fix this issue, I modified the
'Literal:exit'
and'TemplateLiteral:exit'
handlers in the rule to traverse up the Abstract Syntax Tree (AST) and check if the literal is part of aVariableDeclarator
orAssignmentExpression
where the variable name matches an entry inignoreNames
. If it is, the rule will ignore the literal and not report an error.Explanation:
ignoreNames
.Testing:
Manual Testing:
ignoreNames
are no longer reported.??
), logical OR (||
), and ternary (? :
) operators.Automated Testing:
ignoreNames
.Conclusion:
This PR improves the
no-unlocalized-strings
rule by ensuring that string literals within expressions assigned to variables specified inignoreNames
are correctly ignored, aligning the rule's behavior with user expectations and the documentation.