Skip to content

Commit

Permalink
feat: add rule no-import-node-modules-by-path (#219)
Browse files Browse the repository at this point in the history
  • Loading branch information
zanminkian committed Aug 1, 2023
1 parent 0ff5e45 commit cf1240c
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/eslint-config-basic/index.js
Expand Up @@ -396,6 +396,7 @@ module.exports = {
'yml/no-empty-document': 'off',

// antfu
'antfu/no-import-node-modules-by-path': 'error',
'antfu/if-newline': 'error',
'antfu/import-dedupe': 'error',
'antfu/top-level-function': 'error',
Expand Down
2 changes: 2 additions & 0 deletions packages/eslint-plugin-antfu/src/index.ts
Expand Up @@ -3,6 +3,7 @@ import ifNewline from './rules/if-newline'
import importDedupe from './rules/import-dedupe'
import preferInlineTypeImport from './rules/prefer-inline-type-import'
import topLevelFunction from './rules/top-level-function'
import noImportNodeModulesByPath from './rules/no-import-node-modules-by-path'
import noTsExportEqual from './rules/no-ts-export-equal'
import noCjsExports from './rules/no-cjs-exports'
import noConstEnum from './rules/no-const-enum'
Expand All @@ -15,6 +16,7 @@ export default {
'prefer-inline-type-import': preferInlineTypeImport,
'generic-spacing': genericSpacing,
'top-level-function': topLevelFunction,
'no-import-node-modules-by-path': noImportNodeModulesByPath,
'no-cjs-exports': noCjsExports,
'no-ts-export-equal': noTsExportEqual,
'no-const-enum': noConstEnum,
Expand Down
@@ -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' }],
})),
})
})
@@ -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',
})
}
},
}
},
})

0 comments on commit cf1240c

Please sign in to comment.