-
-
Notifications
You must be signed in to change notification settings - Fork 388
/
no-import-node-modules-by-path.ts
42 lines (40 loc) · 1.16 KB
/
no-import-node-modules-by-path.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
import { createEslintRule } from '../utils'
export const RULE_NAME = 'no-import-node-modules-by-path'
export type MessageIds = 'noImportNodeModulesByPath'
export type Options = []
export default createEslintRule<Options, MessageIds>({
name: RULE_NAME,
meta: {
type: 'problem',
docs: {
description: 'Prevent importing modules in `node_modules` folder by relative or absolute path',
recommended: 'error',
},
schema: [],
messages: {
noImportNodeModulesByPath: 'Do not import modules in `node_modules` folder by path',
},
},
defaultOptions: [],
create: (context) => {
return {
'ImportDeclaration': (node) => {
if (node.source.value.includes('/node_modules/')) {
context.report({
node,
messageId: 'noImportNodeModulesByPath',
})
}
},
'CallExpression[callee.name="require"]': (node: any) => {
const value = node.arguments[0]?.value
if (typeof value === 'string' && value.includes('/node_modules/')) {
context.report({
node,
messageId: 'noImportNodeModulesByPath',
})
}
},
}
},
})