-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
quotes.ts
75 lines (63 loc) · 1.99 KB
/
quotes.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import type { TSESTree } from '@typescript-eslint/utils';
import { AST_NODE_TYPES } from '@typescript-eslint/utils';
import * as util from '../util';
import { getESLintCoreRule } from '../util/getESLintCoreRule';
const baseRule = getESLintCoreRule('quotes');
export type Options = util.InferOptionsTypeFromRule<typeof baseRule>;
export type MessageIds = util.InferMessageIdsTypeFromRule<typeof baseRule>;
export default util.createRule<Options, MessageIds>({
name: 'quotes',
meta: {
type: 'layout',
docs: {
description:
'Enforce the consistent use of either backticks, double, or single quotes',
recommended: false,
extendsBaseRule: true,
},
fixable: 'code',
hasSuggestions: baseRule.meta.hasSuggestions,
messages: baseRule.meta.messages,
schema: baseRule.meta.schema,
},
defaultOptions: [
'double',
{
allowTemplateLiterals: false,
avoidEscape: false,
},
],
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:
case AST_NODE_TYPES.TSExternalModuleReference:
return true;
case AST_NODE_TYPES.TSEnumMember:
return node === parent.id;
case AST_NODE_TYPES.TSAbstractPropertyDefinition:
case AST_NODE_TYPES.PropertyDefinition:
return node === parent.key;
default:
return false;
}
}
return {
Literal(node): void {
if (option === 'backtick' && isAllowedAsNonBacktick(node)) {
return;
}
rules.Literal(node);
},
TemplateLiteral(node): void {
rules.TemplateLiteral(node);
},
};
},
});