Skip to content

Commit

Permalink
[ESLint I18n Rule] Cleanup (#181959)
Browse files Browse the repository at this point in the history
## Summary

Just a tiny cleanup PR that was leftover.
  • Loading branch information
CoenWarmer committed Apr 29, 2024
1 parent 694acf7 commit e28cb6e
Showing 1 changed file with 19 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,25 +78,35 @@ export const I18nTranslateShouldStartWithTheRightId: Rule.RuleModule = {
}

if (identifier && !identifier.startsWith(`${i18nAppId}.`)) {
const oldI18nIdentifierArray = identifier.split('.');
const i18nIdentifierRange = node.arguments[0].range;

const newI18nIdentifier =
const oldI18nIdentifierArray = identifier.split('.');
const correctI18nIdentifier =
oldI18nIdentifierArray[0] === 'xpack'
? `${i18nAppId}.${oldI18nIdentifierArray.slice(2).join('.')}`
: `${i18nAppId}.${oldI18nIdentifierArray.slice(1).join('.')}`;

const opts = node.arguments[1]
? sourceCode.getText().slice(node.arguments[1].range[0], node.arguments[1].range[1])
: "{ defaultMessage: '' }";
const hasExistingOpts = node.arguments.length > 1;

report({
node: node as any,
message: RULE_WARNING_MESSAGE,
fix(fixer) {
return fixer.replaceTextRange(
node.range,
`i18n.translate('${newI18nIdentifier}', ${opts})`
);
return [
hasExistingOpts
? // if there are existing options, only replace the i18n identifier and keep the options
fixer.replaceTextRange(i18nIdentifierRange, `\'${correctI18nIdentifier}\'`)
: // if there are no existing options, add an options object with an empty default message
fixer.replaceTextRange(
i18nIdentifierRange,
`\'${correctI18nIdentifier}\', { defaultMessage: '' }`
),
!hasI18nImportLine && rangeToAddI18nImportLine
? replaceMode === 'replace'
? fixer.replaceTextRange(rangeToAddI18nImportLine, i18nImportLine)
: fixer.insertTextAfterRange(rangeToAddI18nImportLine, `\n${i18nImportLine}`)
: null,
].filter(isTruthy);
},
});
}
Expand Down

0 comments on commit e28cb6e

Please sign in to comment.