diff --git a/.eslint-plugin-local/code-no-unexternalized-strings.ts b/.eslint-plugin-local/code-no-unexternalized-strings.ts index 74c712e56fd..7cf7b2f38ee 100644 --- a/.eslint-plugin-local/code-no-unexternalized-strings.ts +++ b/.eslint-plugin-local/code-no-unexternalized-strings.ts @@ -15,6 +15,15 @@ function isDoubleQuoted(node: TSESTree.StringLiteral): boolean { return node.raw[0] === '"' && node.raw[node.raw.length - 1] === '"'; } +/** + * Enable bulk fixing double-quoted strings to single-quoted strings with the --fix eslint flag + * + * Disabled by default as this is often not the desired fix. Instead the string should be localized. However it is + * useful for bulk conversations of existing code. + */ +const enableDoubleToSingleQuoteFixes = false; + + export = new class NoUnexternalizedStrings implements eslint.Rule.RuleModule { private static _rNlsKeys = /^[_a-zA-Z0-9][ .\-_a-zA-Z0-9]*$/; @@ -27,6 +36,7 @@ export = new class NoUnexternalizedStrings implements eslint.Rule.RuleModule { badMessage: 'Message argument to \'{{message}}\' must be a string literal.' }, schema: false, + fixable: enableDoubleToSingleQuoteFixes ? 'code' : undefined, }; create(context: eslint.Rule.RuleContext): eslint.Rule.RuleListener { @@ -112,7 +122,30 @@ export = new class NoUnexternalizedStrings implements eslint.Rule.RuleModule { // (1) // report all strings that are in double quotes for (const node of doubleQuotedStringLiterals) { - context.report({ loc: node.loc, messageId: 'doubleQuoted' }); + context.report({ + loc: node.loc, + messageId: 'doubleQuoted', + fix: enableDoubleToSingleQuoteFixes ? (fixer) => { + // Get the raw string content, unescaping any escaped quotes + const content = (node as ESTree.SimpleLiteral).raw! + .slice(1, -1) + .replace(/(?