Skip to content

Commit cf1240c

Browse files
authoredAug 1, 2023
feat: add rule no-import-node-modules-by-path (#219)
1 parent 0ff5e45 commit cf1240c

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed
 

‎packages/eslint-config-basic/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,7 @@ module.exports = {
396396
'yml/no-empty-document': 'off',
397397

398398
// antfu
399+
'antfu/no-import-node-modules-by-path': 'error',
399400
'antfu/if-newline': 'error',
400401
'antfu/import-dedupe': 'error',
401402
'antfu/top-level-function': 'error',

‎packages/eslint-plugin-antfu/src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import ifNewline from './rules/if-newline'
33
import importDedupe from './rules/import-dedupe'
44
import preferInlineTypeImport from './rules/prefer-inline-type-import'
55
import topLevelFunction from './rules/top-level-function'
6+
import noImportNodeModulesByPath from './rules/no-import-node-modules-by-path'
67
import noTsExportEqual from './rules/no-ts-export-equal'
78
import noCjsExports from './rules/no-cjs-exports'
89
import noConstEnum from './rules/no-const-enum'
@@ -15,6 +16,7 @@ export default {
1516
'prefer-inline-type-import': preferInlineTypeImport,
1617
'generic-spacing': genericSpacing,
1718
'top-level-function': topLevelFunction,
19+
'no-import-node-modules-by-path': noImportNodeModulesByPath,
1820
'no-cjs-exports': noCjsExports,
1921
'no-ts-export-equal': noTsExportEqual,
2022
'no-const-enum': noConstEnum,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { RuleTester } from '@typescript-eslint/utils/dist/ts-eslint'
2+
import { it } from 'vitest'
3+
import rule, { RULE_NAME } from './no-import-node-modules-by-path'
4+
5+
const valids = [
6+
'import xxx from "a"',
7+
'import "b"',
8+
'const c = require("c")',
9+
'require("d")',
10+
]
11+
12+
const invalids = [
13+
'import a from "../node_modules/a"',
14+
'import "../node_modules/b"',
15+
'const c = require("../node_modules/c")',
16+
'require("../node_modules/d")',
17+
]
18+
19+
it('runs', () => {
20+
const ruleTester: RuleTester = new RuleTester({
21+
parser: require.resolve('@typescript-eslint/parser'),
22+
})
23+
24+
ruleTester.run(RULE_NAME, rule, {
25+
valid: valids,
26+
invalid: invalids.map(i => ({
27+
code: i,
28+
errors: [{ messageId: 'noImportNodeModulesByPath' }],
29+
})),
30+
})
31+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { createEslintRule } from '../utils'
2+
3+
export const RULE_NAME = 'no-import-node-modules-by-path'
4+
export type MessageIds = 'noImportNodeModulesByPath'
5+
export type Options = []
6+
7+
export default createEslintRule<Options, MessageIds>({
8+
name: RULE_NAME,
9+
meta: {
10+
type: 'problem',
11+
docs: {
12+
description: 'Prevent importing modules in `node_modules` folder by relative or absolute path',
13+
recommended: 'error',
14+
},
15+
schema: [],
16+
messages: {
17+
noImportNodeModulesByPath: 'Do not import modules in `node_modules` folder by path',
18+
},
19+
},
20+
defaultOptions: [],
21+
create: (context) => {
22+
return {
23+
'ImportDeclaration': (node) => {
24+
if (node.source.value.includes('/node_modules/')) {
25+
context.report({
26+
node,
27+
messageId: 'noImportNodeModulesByPath',
28+
})
29+
}
30+
},
31+
'CallExpression[callee.name="require"]': (node: any) => {
32+
const value = node.arguments[0]?.value
33+
if (typeof value === 'string' && value.includes('/node_modules/')) {
34+
context.report({
35+
node,
36+
messageId: 'noImportNodeModulesByPath',
37+
})
38+
}
39+
},
40+
}
41+
},
42+
})

0 commit comments

Comments
 (0)
Please sign in to comment.