Skip to content

Commit

Permalink
fix(eslint-plugin): [switch-exhaustiveness-check] Support non English…
Browse files Browse the repository at this point in the history
… characters
  • Loading branch information
karishnu committed Jul 8, 2020
1 parent 9c1f8bd commit 75386dd
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions packages/eslint-plugin/src/rules/switch-exhaustiveness-check.ts
Expand Up @@ -32,6 +32,24 @@ 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);
Expand All @@ -44,7 +62,6 @@ export default createRule({
missingBranchTypes: Array<ts.Type>,
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
Expand Down Expand Up @@ -75,7 +92,7 @@ export default createRule({
if (
symbolName &&
missingBranchName &&
!identifierRegex.test(missingBranchName.toString())
requiresQuoting(missingBranchName.toString())
) {
caseTest = `${symbolName}['${missingBranchName}']`;
}
Expand Down

0 comments on commit 75386dd

Please sign in to comment.