Skip to content

Commit

Permalink
fix(eslint-plugin): [quotes] ignore backticks for Enum members (#1355)
Browse files Browse the repository at this point in the history
* fix(eslint-plugin): [quotes] ignore backticks for Enum members

* feat(eslint-plugin): [quotes] handle more TypeScript node types

Co-authored-by: Brad Zacher <brad.zacher@gmail.com>
  • Loading branch information
a-tarasyuk and bradzacher committed Dec 20, 2019
1 parent 2aa696c commit e51048c
Show file tree
Hide file tree
Showing 2 changed files with 514 additions and 8 deletions.
36 changes: 28 additions & 8 deletions packages/eslint-plugin/src/rules/quotes.ts
@@ -1,4 +1,7 @@
import { AST_NODE_TYPES } from '@typescript-eslint/experimental-utils';
import {
AST_NODE_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import baseRule from 'eslint/lib/rules/quotes';
import * as util from '../util';

Expand Down Expand Up @@ -29,15 +32,32 @@ export default util.createRule<Options, MessageIds>({
create(context, [option]) {
const rules = baseRule.create(context);

function isAllowedAsNonBacktick(node: TSESTree.Literal): boolean {
const parent = node.parent;

switch (parent?.type) {
case AST_NODE_TYPES.TSAbstractMethodDefinition:
case AST_NODE_TYPES.TSMethodSignature:
case AST_NODE_TYPES.TSPropertySignature:
case AST_NODE_TYPES.TSModuleDeclaration:
case AST_NODE_TYPES.TSLiteralType:
return true;

case AST_NODE_TYPES.TSEnumMember:
return node === parent.id;

case AST_NODE_TYPES.TSAbstractClassProperty:
case AST_NODE_TYPES.ClassProperty:
return node === parent.key;

default:
return false;
}
}

return {
Literal(node): void {
const parent = node.parent;
if (
option === 'backtick' &&
(parent?.type === AST_NODE_TYPES.TSModuleDeclaration ||
parent?.type === AST_NODE_TYPES.TSLiteralType ||
parent?.type === AST_NODE_TYPES.TSPropertySignature)
) {
if (option === 'backtick' && isAllowedAsNonBacktick(node)) {
return;
}

Expand Down

0 comments on commit e51048c

Please sign in to comment.