forked from typescript-eslint/typescript-eslint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
no-unsafe-declaration-merging.ts
52 lines (48 loc) · 1.41 KB
/
no-unsafe-declaration-merging.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
import type { TSESTree } from '@typescript-eslint/utils';
import * as ts from 'typescript';
import * as util from '../util';
export default util.createRule({
name: 'no-unsafe-declaration-merging',
meta: {
type: 'problem',
docs: {
description: 'Disallow unsafe declaration merging',
recommended: 'strict',
requiresTypeChecking: true,
},
messages: {
unsafeMerging:
'Unsafe declaration merging between classes and interfaces.',
},
schema: [],
},
defaultOptions: [],
create(context) {
const parserServices = util.getParserServices(context);
const checker = parserServices.program.getTypeChecker();
function checkUnsafeDeclaration(
node: TSESTree.Identifier,
unsafeKind: ts.SyntaxKind,
): void {
const tsNode = parserServices.esTreeNodeToTSNodeMap.get(node);
const type = checker.getTypeAtLocation(tsNode);
const symbol = type.getSymbol();
if (symbol?.declarations?.some(decl => decl.kind === unsafeKind)) {
context.report({
node,
messageId: 'unsafeMerging',
});
}
}
return {
ClassDeclaration(node): void {
if (node.id) {
checkUnsafeDeclaration(node.id, ts.SyntaxKind.InterfaceDeclaration);
}
},
TSInterfaceDeclaration(node): void {
checkUnsafeDeclaration(node.id, ts.SyntaxKind.ClassDeclaration);
},
};
},
});