简体   繁体   English

如何使用 ESLint 通过特定文件夹的绝对路径限制模块的导入

[英]How to restrict the import of the module by absolute path for the particular folder with ESLint

I have the folder structure:我有文件夹结构:

my_project
 |------ modules.private
 |        |------ broker_module
 |                 |------ BrokerModule.js
 |
 |------ src
 |        |------ AppService
 |                 |------ AppService.js
 |        
 |        |------ Components
 |                 |------ ScreenLogin.js
 | 
   

And I need to restrict the absolute import from modules.private for all zones, except './src/AppService'.而且我需要限制所有区域从 modules.private 的绝对导入,除了“./src/AppService”。

Case no error.案例没有错误。 Linted file is './src/AppService/AppService.js': Linted 文件是“./src/AppService/AppService.js”:

// NO ERROR
import { BrokerModule } from 'broker_module';

Case with error.有错误的情况。 Linted file is './src/componets/ScreenLogin.js': Linted 文件是“./src/componets/ScreenLogin.js”:

// Error: can not import broker_module to restricted zone
import { BrokerModule } from 'broker_module';

I already tried https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/no-restricted-paths.md , and made the rule like this:我已经尝试过https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/no-restricted-paths.md ,并制定了这样的规则:

// the rule is interpreted like you can not import to target zone './src/components' from 'from': 'broker_module'

'rules': {
    'import/no-restricted-paths': [
      'error',
      {
        'zones': [
          { 'target': './src/components', 'from': 'broker_module' },
        ],
      }
    ],
  },

But it is not working with absolute path.但它不适用于绝对路径。 And more over I tried this ESLint rule https://eslint.org/docs/rules/no-restricted-imports :而且我尝试了这个 ESLint 规则https://eslint.org/docs/rules/no-restricted-imports

'no-restricted-imports': ["error", {
    "paths": ["broker_module"],
}]

And it works, but for all zones.它有效,但适用于所有区域。 It means get the error in all places where I import entities from broker_module.这意味着在我从 broker_module 导入实体的所有地方都会出错。 Can you tell me please how it possible in case to use eslint-plugin-import/no-restricted-paths to write to restricted zone for absolute path, or maybe in case to use ESLint/no-restricted-imports to restrict the import only for the particular zone, not for the all folders.你能告诉我如果使用 eslint-plugin-import/no-restricted-paths 写入绝对路径的受限区域,或者使用 ESLint/no-restricted-imports 来限制只导入对于特定区域,而不是所有文件夹。

I have found the decision how to restrict the particular zone.我已经找到了如何限制特定区域的决定。 It could be achieved with two ways:可以通过两种方式实现:

  1. Write global pattern in your .eslintrc.js file with overrides:在您的 .eslintrc.js 文件中使用覆盖写入全局模式:
'rules': {
    'no-restricted-imports': ['error', {
      'paths': ['vtb.broker2.api.0'],
    }],
    'import/no-restricted-paths': [
      'error',
      {
        'zones': [
          { 'target': './src/vtb', 'from': './src/vtb/api.1' },
        ],
      }
    ],
  },
  'overrides': [
    {
      'files': ['./src/vtb/api.1/*.ts', './src/vtb/api.1/**/*.ts'],
      'rules': {
        'no-restricted-imports': 'off',
      },
    }
  ],

2.According the official docs https://eslint.org/docs/user-guide/configuring#configuration-cascading-and-hierarchy possible to overrides the rule just to create in the parent of the folder directory where you want to change the rule make a file .eslintrc.js that will work only for this folder and for example to off the rule: 2.根据官方文档https://eslint.org/docs/user-guide/configuring#configuration-cascading-and-hierarchy可以覆盖规则只是在要更改的文件夹目录的父目录中创建规则创建一个文件 .eslintrc.js ,该文件仅适用于此文件夹,例如关闭规则:

  'import/no-restricted-paths': [
      'off',
  },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM