Eslint
规则 | Rules

no-restricted-imports

禁止特定的导入(无限制导入)

进口是一个 ES6 / ES2015 标准,用于使您当前模块中的其他模块具有功能。在 CommonJS 中,这是通过 require()调用实现的,这使得这个 ESLint 规则大致等同于它的 CommonJS 规则no-restricted-modules

你为什么要限制进口?

  • 某些进口在特定环境下可能没有意义。例如,Node.js 的fs模块在没有文件系统的环境中没有意义。

  • 一些模块提供相似或相同的功能,思考lodashunderscore。您的项目可能已在模块上标准化。你想确保没有使用其他替代方案,因为这会不必要地膨胀项目,并且在满足时提供两个相关性的更高维护成本。

规则细节

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

选项

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

"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." }]]

或者像这样:

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

或者像这样,如果你只需要限制某个模块的某些输入:

"no-restricted-imports": ["error", { "paths": [{ "name": "import-foo", "importNames": ["Bar"], "message": "Please use Bar from /import-bar/baz/ 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", { "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 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", "fs"]*/ import crypto from 'crypto';

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

/*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 错误或警告的情况下导入项目中的模块,请不要使用此规则,也不要在此规则的列表中包含模块。

版本

此规则在 ESLint 2.0.0-alpha-1 中引入。

资源