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 corrected support for interface prop names #95

Merged

Conversation

swernerx
Copy link
Contributor

@swernerx swernerx commented Dec 9, 2024

Problem

The no-unlocalized-strings ESLint rule was incorrectly flagging string literals used in TypeScript type definitions as untranslated strings:

interface Props {
  "aria-label": string;    // ❌ Was incorrectly flagged
  "data-testid": string;   // ❌ Was incorrectly flagged
}

type ButtonVariant = 'primary' | 'secondary';  // ❌ Was incorrectly flagged

Solution

Modified the rule to properly ignore string literals when they appear in type contexts. All strings in TypeScript type definitions are now correctly ignored as they represent type information rather than actual text content:

interface Props {
  "aria-label": string;    // ✅ Now correctly ignored
  "data-testid": string;   // ✅ Now correctly ignored
}

type ButtonVariant = 'primary' | 'secondary';  // ✅ Now correctly ignored
interface Config {
  variant: 'primary';      // ✅ Now correctly ignored
}

Implementation

  • Simplified type context detection to ignore all string literals in TypeScript type definitions
  • Added comprehensive handling of various TypeScript type contexts (interfaces, type aliases, literal types)
  • Removed unnecessary property value checks as all type contexts should be ignored

Testing

  • Added test cases for string literals in various TypeScript type contexts
  • Verified proper handling of string literals in interfaces and type definitions
  • Maintained existing functionality for non-TypeScript contexts

Breaking Changes

None. This is a bug fix that improves TypeScript support without affecting the core functionality of the rule.

Copy link

codecov bot commented Dec 9, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.45%. Comparing base (df19c1c) to head (e3cfbac).
Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #95      +/-   ##
==========================================
+ Coverage   97.18%   97.45%   +0.26%     
==========================================
  Files          10       10              
  Lines         427      433       +6     
  Branches      127      133       +6     
==========================================
+ Hits          415      422       +7     
+ Misses         12       11       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@timofei-iatsenko
Copy link
Collaborator

Thanks for the contribution.

Have a question regarding:

interface Props {
  message: 'This should be translated';  // ❌ Was not being caught
}

How it's going to be translated? What the real use case? In my opinion typescript interfaces should be completely ignored

@swernerx
Copy link
Contributor Author

swernerx commented Dec 9, 2024

Was quite tough to keep matching the previous coverage, but now with some code cleanups and an extended test suite it's green again :)

@swernerx
Copy link
Contributor Author

swernerx commented Dec 9, 2024

Thanks for the contribution.

Have a question regarding:

interface Props {
  message: 'This should be translated';  // ❌ Was not being caught
}

How it's going to be translated? What the real use case? In my opinion typescript interfaces should be completely ignored

That was indeed not right I guess. Valid comment!

Comment on lines 638 to 653
{
name: 'object literal properties should still be checked',
code: `
const props = {
label: 'This should be translated'
};
`,
errors: [{ messageId: 'default' }],
},
{
name: 'regular string assignments should still be checked',
code: `
let message = 'This should be translated';
`,
errors: [{ messageId: 'default' }],
},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possible the duplication of the cases, we already have these:

    { code: 'const a = `Hello ${nice}`', errors },
    { code: 'export const a = `hello string`;', errors },
    { code: "const ge = 'Select tax code'", errors },
    { code: 'const a = `Foo`;', errors },

    { code: 'var a = {foo: "Bar"};', errors },
    { code: `const test = { text: 'This is not localized' }`, errors },

I know the test suite for this rule is a bit mess, let's not make it even more messier ) Either delete old duplicating cases, or not add a new ones.

Also i tried to somehow group / sort them, so it's easier to spot group of functionality. Like all variants of prop assignments together. All variants of JSX text together, etc.

Thanks!

@swernerx
Copy link
Contributor Author

Done. Reworked the test suite. Improved structure and maintained coverage. Tell me if that's looking better. :)

@swernerx
Copy link
Contributor Author

I’ve identified three additional issues that I plan to address next week. In the meantime, is there anything else that needs to be adjusted, or can we proceed with merging this? I’d appreciate any feedback or remarks!

@andrii-bodnar andrii-bodnar merged commit 56eb2ee into lingui:main Dec 16, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants