/
no-poorly-typed-ts-props.ts
110 lines (101 loc) · 3.03 KB
/
no-poorly-typed-ts-props.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import {
TSESTree,
ESLintUtils,
TSESLint,
} from '@typescript-eslint/experimental-utils';
import { createRule } from '../util';
/*
TypeScript declares some bad types for certain properties.
See: https://github.com/microsoft/TypeScript/issues/24706
This rule simply warns against using them, as using them will likely introduce type safety holes.
*/
const BANNED_PROPERTIES = [
// {
// type: 'Node',
// property: 'parent',
// fixWith: null,
// },
{
type: 'Symbol',
property: 'declarations',
fixWith: 'getDeclarations()',
},
{
type: 'Type',
property: 'symbol',
fixWith: 'getSymbol()',
},
];
export default createRule({
name: 'no-poorly-typed-ts-props',
meta: {
type: 'problem',
docs: {
description:
"Enforces rules don't use TS API properties with known bad type definitions",
category: 'Possible Errors',
recommended: 'error',
suggestion: true,
requiresTypeChecking: true,
},
fixable: 'code',
schema: [],
messages: {
doNotUse: 'Do not use {{type}}.{{property}} because it is poorly typed.',
doNotUseWithFixer:
'Do not use {{type}}.{{property}} because it is poorly typed. Use {{type}}.{{fixWith}} instead.',
suggestedFix: 'Use {{type}}.{{fixWith}} instead.',
},
},
defaultOptions: [],
create(context) {
const { program, esTreeNodeToTSNodeMap } = ESLintUtils.getParserServices(
context,
);
const checker = program.getTypeChecker();
return {
'MemberExpression[computed = false]'(
node: TSESTree.MemberExpressionNonComputedName,
): void {
for (const banned of BANNED_PROPERTIES) {
if (node.property.name !== banned.property) {
continue;
}
// make sure the type name matches
const tsObjectNode = esTreeNodeToTSNodeMap.get(node.object);
const objectType = checker.getTypeAtLocation(tsObjectNode);
const objectSymbol = objectType.getSymbol();
if (objectSymbol?.getName() !== banned.type) {
continue;
}
const tsNode = esTreeNodeToTSNodeMap.get(node.property);
const symbol = checker.getSymbolAtLocation(tsNode);
const decls = symbol?.getDeclarations();
const isFromTs = decls?.some(decl =>
decl.getSourceFile().fileName.includes('/node_modules/typescript/'),
);
if (isFromTs !== true) {
continue;
}
return context.report({
node,
messageId: banned.fixWith ? 'doNotUseWithFixer' : 'doNotUse',
data: banned,
suggest: [
{
messageId: 'suggestedFix',
data: banned,
fix(fixer): TSESLint.RuleFix | null {
if (banned.fixWith == null) {
return null;
}
return fixer.replaceText(node.property, banned.fixWith);
},
},
],
});
}
},
};
},
});