-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
no-typescript-estree-import.ts
63 lines (58 loc) · 1.84 KB
/
no-typescript-estree-import.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
import { createRule } from '../util';
const TSESTREE_NAME = '@typescript-eslint/typescript-estree';
const TYPES_NAME = '@typescript-eslint/types';
const UTILS_NAME = '@typescript-eslint/experimental-utils';
/*
Typescript will not error if people use typescript-estree within eslint-plugin.
This is because it's an indirect dependency.
We don't want people to import it, instead we want them to import from the utils package.
*/
export default createRule({
name: 'no-typescript-estree-import',
meta: {
type: 'problem',
docs: {
description: `Enforces that eslint-plugin rules don't require anything from ${TSESTREE_NAME} or ${TYPES_NAME}`,
recommended: 'error',
},
fixable: 'code',
schema: [],
messages: {
dontImportPackage: [
`Don't import from {{packageName}}. Everything you need should be available in ${UTILS_NAME}.`,
`{{packageName}} is an indirect dependency of this package, and thus should not be used directly.`,
].join('\n'),
},
},
defaultOptions: [],
create(context) {
return {
ImportDeclaration(node): void {
if (typeof node.source.value !== 'string') {
return;
}
let packageName: string | null = null;
if (node.source.value.startsWith(TSESTREE_NAME)) {
packageName = TSESTREE_NAME;
} else if (node.source.value.startsWith(TYPES_NAME)) {
packageName = TYPES_NAME;
}
if (packageName != null) {
context.report({
node,
messageId: 'dontImportPackage',
data: {
packageName,
},
fix(fixer) {
return fixer.replaceTextRange(
[node.source.range[0] + 1, node.source.range[1] - 1],
UTILS_NAME,
);
},
});
}
},
};
},
});