joc*_*ers 6 javascript import absolute-path eslint eslintrc
我有文件夹结构:
my_project
|------ modules.private
| |------ broker_module
| |------ BrokerModule.js
|
|------ src
| |------ AppService
| |------ AppService.js
|
| |------ Components
| |------ ScreenLogin.js
|
Run Code Online (Sandbox Code Playgroud)
我需要限制所有区域的 module.private 的绝对导入,除了“./src/AppService”。
情况无错误。Linted 文件是“./src/AppService/AppService.js”:
// NO ERROR
import { BrokerModule } from 'broker_module';
Run Code Online (Sandbox Code Playgroud)
有错误的情况。Linted 文件是“./src/componets/ScreenLogin.js”:
// Error: can not import broker_module to restricted zone
import { BrokerModule } from 'broker_module';
Run Code Online (Sandbox Code Playgroud)
我已经尝试过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' },
],
}
],
},
Run Code Online (Sandbox Code Playgroud)
但它不适用于绝对路径。而且我尝试了这个 ESLint 规则https://eslint.org/docs/rules/no-restricted-imports:
'no-restricted-imports': ["error", {
"paths": ["broker_module"],
}]
Run Code Online (Sandbox Code Playgroud)
它有效,但适用于所有区域。这意味着在我从broker_module导入实体的所有地方都会出现错误。您能否告诉我,如果使用 eslint-plugin-import/no-restricted-paths 写入绝对路径的受限区域,或者可能使用 ESLint/no-restricted-imports 仅限制导入,如何可能对于特定区域,而不是所有文件夹。
我已经找到了如何限制特定区域的决定。可以通过两种方式实现:
'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',
},
}
],
Run Code Online (Sandbox Code Playgroud)
2.根据官方文档https://eslint.org/docs/user-guide/configuring#configuration-cascading-and-hierarchy可以覆盖规则,仅在要更改的文件夹目录的父级中创建规则创建一个文件 .eslintrc.js ,该文件仅适用于此文件夹,例如关闭规则:
'import/no-restricted-paths': [
'off',
},
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8457 次 |
| 最近记录: |