From 27fd2d256bc46b4acef1c631c9c9bb6d4f979025 Mon Sep 17 00:00:00 2001 From: Karishnu Poddar Date: Wed, 8 Jul 2020 14:41:20 +0530 Subject: [PATCH] fix(eslint-plugin): [switch-exhaustiveness-check] Support non English characters --- .../src/rules/switch-exhaustiveness-check.ts | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/packages/eslint-plugin/src/rules/switch-exhaustiveness-check.ts b/packages/eslint-plugin/src/rules/switch-exhaustiveness-check.ts index bf282f74d86..d8a7750efba 100644 --- a/packages/eslint-plugin/src/rules/switch-exhaustiveness-check.ts +++ b/packages/eslint-plugin/src/rules/switch-exhaustiveness-check.ts @@ -32,6 +32,25 @@ export default createRule({ const sourceCode = context.getSourceCode(); const service = getParserServices(context); const checker = service.program.getTypeChecker(); + const compilerOptions = service.program.getCompilerOptions(); + + function requiresQuoting(name: string): boolean { + if (name.length === 0) { + return true; + } + + if (!ts.isIdentifierStart(name.charCodeAt(0), compilerOptions.target)) { + return true; + } + + for (let i = 1; i < name.length; i += 1) { + if (!ts.isIdentifierPart(name.charCodeAt(i), compilerOptions.target)) { + return true; + } + } + + return false; + } function getNodeType(node: TSESTree.Node): ts.Type { const tsNode = service.esTreeNodeToTSNodeMap.get(node); @@ -44,7 +63,6 @@ export default createRule({ missingBranchTypes: Array, symbolName?: string, ): TSESLint.RuleFix | null { - const identifierRegex = /^[a-zA-Z_$][0-9a-zA-Z_$]*$/; const lastCase = node.cases.length > 0 ? node.cases[node.cases.length - 1] : null; const caseIndent = lastCase @@ -74,8 +92,8 @@ export default createRule({ if ( symbolName && - missingBranchName && - !identifierRegex.test(missingBranchName.toString()) + (missingBranchName || missingBranchName === '') && + requiresQuoting(missingBranchName.toString()) ) { caseTest = `${symbolName}['${missingBranchName}']`; }