File tree 4 files changed +76
-0
lines changed
4 files changed +76
-0
lines changed Original file line number Diff line number Diff line change @@ -396,6 +396,7 @@ module.exports = {
396
396
'yml/no-empty-document' : 'off' ,
397
397
398
398
// antfu
399
+ 'antfu/no-import-node-modules-by-path' : 'error' ,
399
400
'antfu/if-newline' : 'error' ,
400
401
'antfu/import-dedupe' : 'error' ,
401
402
'antfu/top-level-function' : 'error' ,
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ import ifNewline from './rules/if-newline'
3
3
import importDedupe from './rules/import-dedupe'
4
4
import preferInlineTypeImport from './rules/prefer-inline-type-import'
5
5
import topLevelFunction from './rules/top-level-function'
6
+ import noImportNodeModulesByPath from './rules/no-import-node-modules-by-path'
6
7
import noTsExportEqual from './rules/no-ts-export-equal'
7
8
import noCjsExports from './rules/no-cjs-exports'
8
9
import noConstEnum from './rules/no-const-enum'
@@ -15,6 +16,7 @@ export default {
15
16
'prefer-inline-type-import' : preferInlineTypeImport ,
16
17
'generic-spacing' : genericSpacing ,
17
18
'top-level-function' : topLevelFunction ,
19
+ 'no-import-node-modules-by-path' : noImportNodeModulesByPath ,
18
20
'no-cjs-exports' : noCjsExports ,
19
21
'no-ts-export-equal' : noTsExportEqual ,
20
22
'no-const-enum' : noConstEnum ,
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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
+ } )
You can’t perform that action at this time.
0 commit comments