-
-
Notifications
You must be signed in to change notification settings - Fork 478
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add rule
no-import-node-modules-by-path
(#219)
- Loading branch information
1 parent
0ff5e45
commit cf1240c
Showing
4 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
packages/eslint-plugin-antfu/src/rules/no-import-node-modules-by-path.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { RuleTester } from '@typescript-eslint/utils/dist/ts-eslint' | ||
import { it } from 'vitest' | ||
import rule, { RULE_NAME } from './no-import-node-modules-by-path' | ||
|
||
const valids = [ | ||
'import xxx from "a"', | ||
'import "b"', | ||
'const c = require("c")', | ||
'require("d")', | ||
] | ||
|
||
const invalids = [ | ||
'import a from "../node_modules/a"', | ||
'import "../node_modules/b"', | ||
'const c = require("../node_modules/c")', | ||
'require("../node_modules/d")', | ||
] | ||
|
||
it('runs', () => { | ||
const ruleTester: RuleTester = new RuleTester({ | ||
parser: require.resolve('@typescript-eslint/parser'), | ||
}) | ||
|
||
ruleTester.run(RULE_NAME, rule, { | ||
valid: valids, | ||
invalid: invalids.map(i => ({ | ||
code: i, | ||
errors: [{ messageId: 'noImportNodeModulesByPath' }], | ||
})), | ||
}) | ||
}) |
42 changes: 42 additions & 0 deletions
42
packages/eslint-plugin-antfu/src/rules/no-import-node-modules-by-path.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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', | ||
}) | ||
} | ||
}, | ||
} | ||
}, | ||
}) |