# no-restricted-imports

import 加载时禁止指定模块

Imports 是 ES6/ES2015 标准,用于使当前模块中的其他模块的功能可用。在 CommonJS 中,这是通过 require() 调用实现的,这使得这个 ESLint 规则大致相当于其 CommonJS 对应的 no-restricted-modules

为什么要限制进口?

  • 某些导入在特定环境中可能没有意义。例如,Node.js 的 fs 模块在没有文件系统的环境中没有意义。
  • 一些模块提供相似或相同的功能,想想 lodash 和 underscore。您的项目可能已经标准化了一个模块。您要确保不使用其他替代方案,因为这会不必要地使项目膨胀,并在一个足够的情况下提供两个依赖项的更高维护成本。

# 规则详情

此规则允许您指定不想在应用程序中使用的导入。

它仅适用于静态导入,不适用于动态导入。

# 选项

指定受限导入的语法如下所示:

"no-restricted-imports": ["error", "import1", "import2"]

或像这样:

"no-restricted-imports": ["error", { "paths": ["import1", "import2"] }]

使用对象形式时,还可以指定一个 gitignore 样式模式的数组:

"no-restricted-imports": ["error", {
    "paths": ["import1", "import2"],
    "patterns": ["import1/private/*", "import2/*", "!import2/good"]
}]

您还可以为要限制的任何路径指定自定义消息,如下所示:

"no-restricted-imports": ["error", {
    "name": "import-foo",
    "message": "Please use import-bar instead."
}, {
    "name": "import-baz",
    "message": "Please use import-quux instead."
}]

或像这样:

"no-restricted-imports": ["error", {
    "paths": [{
        "name": "import-foo",
        "message": "Please use import-bar instead."
    }, {
        "name": "import-baz",
        "message": "Please use import-quux instead."
    }]
}]

或者如果您只需要限制来自模块的某些导入,则像这样:

"no-restricted-imports": ["error", {
  "paths": [{
    "name": "import-foo",
    "importNames": ["Bar"],
    "message": "Please use Bar from /import-bar/baz/ instead."
  }]
}]

如果您想将自定义消息应用于模式匹配,或者像这样:

"no-restricted-imports": ["error", {
    "patterns": [{
      "group": ["import1/private/*"],
      "message": "usage of import1 private modules not allowed."
    }, {
      "group": ["import2/*", "!import2/good"],
      "message": "import2 is deprecated, except the modules in import2/good."
    }]
}]

自定义消息将附加到默认错误消息中。

模式匹配也可以配置为区分大小写:

"no-restricted-imports": ["error", {
    "patterns": [{
      "group": ["import1/private/prefix[A-Z]*"],
      "caseSensitive": true
    }]
}]

模式匹配只能限制特定的导入名称,类似于 paths 选项:

"no-restricted-imports": ["error", {
    "patterns": [{
      "group": ["utils/*"],
      "importNames": ["isEmpty"],
      "message": "Use 'isEmpty' from lodash instead."
    }]
}]

要限制使用所有 Node.js 核心导入(通过 https://github.com/nodejs/node/tree/master/lib):

    "no-restricted-imports": ["error",
         "assert","buffer","child_process","cluster","crypto","dgram","dns","domain","events","freelist","fs","http","https","module","net","os","path","punycode","querystring","readline","repl","smalloc","stream","string_decoder","sys","timers","tls","tracing","tty","url","util","vm","zlib"
    ],

# 示例

此规则的错误代码示例:

/*eslint no-restricted-imports: ["error", "fs"]*/

import fs from 'fs';
/*eslint no-restricted-imports: ["error", "fs"]*/

export { fs } from 'fs';
/*eslint no-restricted-imports: ["error", "fs"]*/

export * from 'fs';
/*eslint no-restricted-imports: ["error", { "paths": ["cluster"] }]*/

import cluster from 'cluster';
/*eslint no-restricted-imports: ["error", { "patterns": ["lodash/*"] }]*/

import pick from 'lodash/pick';
/*eslint no-restricted-imports: ["error", { paths: [{
    name: "foo",
    importNames: ["default"],
    message: "Please use the default import from '/bar/baz/' instead."
}]}]*/

import DisallowedObject from "foo";
/*eslint no-restricted-imports: ["error", { paths: [{
    name: "foo",
    importNames: ["DisallowedObject"],
    message: "Please import 'DisallowedObject' from '/bar/baz/' instead."
}]}]*/

import { DisallowedObject } from "foo";

import { DisallowedObject as AllowedObject } from "foo";

import { "DisallowedObject" as AllowedObject } from "foo";
/*eslint no-restricted-imports: ["error", { paths: [{
    name: "foo",
    importNames: ["DisallowedObject"],
    message: "Please import 'DisallowedObject' from '/bar/baz/' instead."
}]}]*/

import * as Foo from "foo";
/*eslint no-restricted-imports: ["error", { patterns: [{
    group: ["lodash/*"],
    message: "Please use the default import from 'lodash' instead."
}]}]*/

import pick from 'lodash/pick';
/*eslint no-restricted-imports: ["error", { patterns: [{
    group: ["foo[A-Z]*"],
    caseSensitive: true
}]}]*/

import pick from 'fooBar';
/*eslint no-restricted-imports: ["error", { patterns: [{
    group: ["utils/*"],
    importNames: ['isEmpty'],
    message: "Use 'isEmpty' from lodash instead."
}]}]*/

import { isEmpty } from 'utils/collection-utils';

此规则的正确代码示例:

/*eslint no-restricted-imports: ["error", "fs"]*/

import crypto from 'crypto';
export { foo } from "bar";
/*eslint no-restricted-imports: ["error", { "paths": ["fs"], "patterns": ["eslint/*"] }]*/

import crypto from 'crypto';
import eslint from 'eslint';
export * from "path";
/*eslint no-restricted-imports: ["error", { paths: [{ name: "foo", importNames: ["DisallowedObject"] }] }]*/

import DisallowedObject from "foo"
/*eslint no-restricted-imports: ["error", { paths: [{
    name: "foo",
    importNames: ["DisallowedObject"],
    message: "Please import 'DisallowedObject' from '/bar/baz/' instead."
}]}]*/

import { AllowedObject as DisallowedObject } from "foo";
/*eslint no-restricted-imports: ["error", { patterns: [{
    group: ["lodash/*"],
    message: "Please use the default import from 'lodash' instead."
}]}]*/

import lodash from 'lodash';
/*eslint no-restricted-imports: ["error", { patterns: [{
    group: ["foo[A-Z]*"],
    caseSensitive: true
}]}]*/

import pick from 'food';
/*eslint no-restricted-imports: ["error", { patterns: [{
    group: ["utils/*"],
    importNames: ['isEmpty'],
    message: "Use 'isEmpty' from lodash instead."
}]}]*/

import { hasValues } from 'utils/collection-utils';

# 何时不使用

如果您希望能够在项目中导入模块而不会出现 ESLint 错误或警告,请不要使用此规则或不在此规则的列表中包含模块。

Last Updated: 5/13/2023, 8:55:38 PM