/
prefer-type-alias.ts
92 lines (83 loc) · 2.83 KB
/
prefer-type-alias.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import * as util from '../util';
import { RuleFix } from 'ts-eslint';
export default util.createRule({
name: 'prefer-type-alias',
meta: {
type: 'suggestion',
docs: {
description: 'Prefer a type alias over an interface declaration.',
category: 'Stylistic Issues',
recommended: false,
tslintName: 'prefer-type-alias',
},
messages: {
preferTypeAlias: 'Expected a `type` instead of an `interface`',
},
schema: [],
fixable: 'code',
},
defaultOptions: [],
create(context) {
const sourceCode = context.getSourceCode();
return {
TSInterfaceDeclaration: node => {
context.report({
node: node.id,
messageId: 'preferTypeAlias',
fix(fixer) {
const typeNode = node.typeParameters || node.id;
const fixes: RuleFix[] = [];
const interfaceKeyword = sourceCode.getFirstToken(node);
const extendsKeyword = sourceCode.getTokenAfter(interfaceKeyword!, {
filter: token =>
token.type === 'Keyword' && token.value === 'extends',
});
const headBracket = sourceCode.getTokenAfter(interfaceKeyword!, {
filter: token =>
token.type === 'Punctuator' && token.value === '{',
});
fixes.push(fixer.replaceText(interfaceKeyword!, 'type'));
fixes.push(
fixer.replaceTextRange(
[typeNode.range[1], node.body.range[0]],
' ',
),
);
fixes.push(fixer.insertTextBefore(headBracket!, '= '));
if (extendsKeyword) {
const interfaceIdentifier = sourceCode.getTokenAfter(
extendsKeyword,
{
filter: token => token.type === 'Identifier',
},
);
const [tailBracket] = sourceCode.getLastTokens(node, {
filter: token =>
token.type === 'Punctuator' && token.value === '}',
});
// NOTE: insertion `& Keyword` to tail
fixes.push(fixer.insertTextAfter(tailBracket, ' & '));
fixes.push(
fixer.insertTextAfter(tailBracket, interfaceIdentifier!.value),
);
// NOTE: remove `extends` & interface name
fixes.push(
fixer.removeRange([
extendsKeyword.range[0],
extendsKeyword.range[1] + 1,
]),
);
fixes.push(
fixer.removeRange([
interfaceIdentifier!.range[0],
interfaceIdentifier!.range[1] + 1, // include space after Identifier like `A `
]),
);
}
return fixes;
},
});
},
};
},
});