no-restricted-imports

import 加载时禁止指定模块

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

为什么要限制进口?

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

  • AB1ZbdFCMoSKN77+K7ZJ+SFIw1h8XFRtjrv0vn3ruuoUmwjRe/5W4S8EaizmLfoxbLY2NEvcC7eHuxH0waKUJlFLZ1FI5qSnLVyba41uNfCYg7jrHBh2qqUUPlQJ6oGMizfIa4PMqeHoA/A784xT5DJgEd3xL8InClxHFtwAaiW7i9AGaLiwAiBDP5guZbmMUTVxhSJ0sIYmt3nvT2HUrtGgUDv2J96t0fNSuaVqNrGquaiGyqM75hYxjfUr9ufRWNrQ/svwA8u40PAJOjJhwRQ5dQwLHx9M6KSFFBexwTArBsaR3BcO4VAljcOoD9tD5lC3ISPulnTMYvSxPA9gCMXIQvRk3EvGF3HwpNXPJtrnDA4x46JpUcHNqXZtEYU2X8agog5Ch0zgsI6/JtBQd5U/KNX9lK4qfsQE8ej1xBM=

规则详情

o8k9Mglf6ux7ala7L080i/2Ct3Z5b85zDD4DEM6wxiKNtSeAbKM+4XojaVxhKMp5NHjFRGmEiambP5nbbeWuGznFIyN2JAONdNP7g76CChg=

r1EJjWvePInhX32MvsWliYu2s9QxwhyzPQS7CWL97CZ1uMi+K35iD2pvcY4bjVWHyWHJ0V9YWJHODkOJAsQWWA==

选项

Lnx4PIab1IFdOgoPWn2+UemqMAVwJp5AtCYcSIqDqbI6OdayRr0BzEZIpc9dvEXt

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

6o8DjqQ6tGHaq/pPjySZtA==

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

srQavXZK6I978GIGfU78SCSQAKn5OMENa559YPLS6fOpS0A17HGFPQ6DNNsMo4zD4HchEdCyZiOWYbFg9LOw/uXg57RR4LgI4Uf0s7RPcZrXLs49vzoLK0KlboI6ovv5

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

PhpLhZx9SSLAv2GkrlI2HWEhCGhMOChrJcPhivZl/hVzeSnLd8ukBz6Fddu7hdn9GfrrGZi1yuc0s+su75f5Cd7zZ2jSNwvqMZ1vQ/Frnhs=

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

6o8DjqQ6tGHaq/pPjySZtA==

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

Olu162naPDoFbD/6P2jgBfrF306Qi6PVFIbp5AGjiKf1sNvcVV2Jb2ZxStVb3ez99CXzgvCdv1888EyQlcEEdpbcAw0McFgRrCICjM7Ezm4=

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

KeodynB0henrPknUXpEfEhiCCN282tZFPtfUlhrhS8W8whktwkdjNXvQtzc9Iw+GptLUV2TwHIqrBPm8JQfc4NWIIqkO9h33Qqnf4rTpEew=

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

8qfY36ZFg9s9Ur1zRi0UlK/DeE+Sb98lqvcdesHDq6R4lSq1HukhzylF4JZV+ImTx3ELrD4R2GV+rRFuYZ/wdQ==

KG3acIjoZZIbL88VwgZBz1w5BXhj39AI8pxpd2HxFnoHNAQId3oIevO6E0z3rc4kSqMfot+ocnUrNx16CaLLWA==

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

GTR6S4/jMz66N1gfN86Tw4VMVkyB4TM8g58tV9gpoREWVrAhHHeObMiQcLDrc7yZ6Aib3FE5HBTEQ4xy9t1V9mGOUIGqSN4ocK1aau9cWOEILoWCBxZzh3r7wG5dq7qb

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

cJL0asVLqnOCetlQp8pzRp5te7UKHj4daZ7ywXHKxrLUDww2EV/0+QXl44/HIpxXCuf6XSW6nNL4EEK7bxeTyvLrMO+oXgDj9m/Gac7GHKbjKB3Awaarlp+G7wyw6LaA8InrJP7OiqCh8sLy5pYaViF9GCwOwQ65xjK9kpJsCOP7UNEMwbxyyVARbI4oYyjnZJX8+QnJ7dfmq1C9HBSesrGGSh/2xQJzJBfjtGu/Qas=

    "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"
    ],

示例

zblpFAyoEiDF1GS7EEgT+Fw+zDBsVLJNOcYSNQAXW6dSdz0JClE4rOOJ51S8csCM

/*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';

h+1Jf/rEEE6yYiKhWnK/aHl9/UYIfowckm44qhmqvwwaWj0QX+JXnab7YJgxSmDw

/*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';

何时不使用

9jN+8aDOy5CE3OVoE4yeuXMwToobwDCif0v0bGB8R0rIYpAY0Eq0LRKEQw5rjyUol2sLetzWCBEgrCSAVuwRn6GWS2xwonsJKStj6O/YM19NDYrXuZ3XRVfMG4ttisH98B3daW4dhXZa6pNe/Y++THT9Z2YVL3wUbJ42VlVG+Jfc3YEWivj2kCFJoYn9VJ3sjzBIVvRzXv+B3sQjYvL15Q==