/
quotes.ts
62 lines (55 loc) · 1.52 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
import {
AST_NODE_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import baseRule from 'eslint/lib/rules/quotes';
import * as util from '../util';
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',
category: 'Stylistic Issues',
recommended: false,
},
fixable: 'code',
messages: baseRule.meta.messages,
schema: baseRule.meta.schema,
},
defaultOptions: [
'double',
{
allowTemplateLiterals: false,
avoidEscape: false,
},
],
create(context, [option]) {
const rules = baseRule.create(context);
const isModuleDeclaration = (node: TSESTree.Literal): boolean => {
return (
!!node.parent && node.parent.type === AST_NODE_TYPES.TSModuleDeclaration
);
};
const isTypeLiteral = (node: TSESTree.Literal): boolean => {
return !!node.parent && node.parent.type === AST_NODE_TYPES.TSLiteralType;
};
return {
Literal(node): void {
if (
option === 'backtick' &&
(isModuleDeclaration(node) || isTypeLiteral(node))
) {
return;
}
rules.Literal(node);
},
TemplateLiteral(node): void {
rules.TemplateLiteral(node);
},
};
},
});