Javascript eslint 应该列在项目的依赖项中,而不是 devDependencies
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44939304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
eslint should be listed in the project's dependencies, not devDependencies
提问by PositiveGuy
Either I don't understand dependenciesvs. devDependenciesin node 100% yet or eslint is just wrong here (not capable of analyzing this correctly):
要么我不了解dependenciesvs.devDependencies在节点 100% 中,要么 eslint 在这里是错误的(无法正确分析):
3:1 error 'chai' should be listed in the project's dependencies, not devDependencies import/no-extraneous-dependencies
4:1 error 'chai-enzyme' should be listed in the project's dependencies, not devDependencies import/no-extraneous-dependencies
5:1 error 'enzyme' should be listed in the project's dependencies, not devDependencies import/no-extraneous-dependencies
7:1 error 'sinon' should be listed in the project's dependencies, not devDependencies import/no-extraneous-dependencies
9:1 error 'redux-mock-store' should be listed in the project's dependencies, not devDependencies import/no-extraneous-dependencies
These are test dependencies, so why is it saying that they should be listed in dependencies?
这些是测试依赖项,那么为什么说它们应该列在dependencies?
Additional note: We're using Travis as our CI so I don't know if it makes a difference for that at all either.
附加说明:我们使用 Travis 作为我们的 CI,所以我不知道它是否对此有任何影响。
回答by PositiveGuy
Solved it with adding this to my .eslintrc:
通过将其添加到我的来解决它.eslintrc:
"import/no-extraneous-dependencies": ["error", {"devDependencies": true}]
"import/no-extraneous-dependencies": ["error", {"devDependencies": true}]
[no-extraneous-dependencies] Add exceptions? #422
[no-extraeous-dependencies] 添加异常?第422章
Based on this user's reply:
根据该用户的回复:
you could set the option devDependencies: true in an .eslintrc in your test folder:
rules: import/no-extraneous-dependencies: [error, { devDependencies: true }] Then you'll get reports of any packages referenced that are not included dependencies or devDependencies. Then you get the goodness of the rule, with no noise from the disable comments.
I think that might work for you? This is how I would use the rule, in your case, since you have your test code separated into a test directory.
您可以在测试文件夹的 .eslintrc 中设置选项 devDependencies: true :
规则: import/no-extraneous-dependencies: [error, { devDependencies: true }] 然后你会得到任何不包含依赖项或 devDependencies 的引用包的报告。然后你会得到规则的好处,没有来自禁用评论的噪音。
我认为这可能对你有用?在您的情况下,这就是我将如何使用规则的方式,因为您将测试代码分隔到一个测试目录中。
Also this post was helpful to confirm I wasn't insane to not want some of these in my dependencies list: Sharable ESLint Config
此外,这篇文章有助于确认我不想要我的依赖项列表中的某些内容并不是疯了:Sharable ESLint Config
回答by magic_al
If you want to allow imports of devDependenciesin test files onlyyou can use an array of globs, as the documentationof no-extraneous-dependenciesstates:
如果你想允许进口devDependencies的测试文件只有您可以使用array of globs,作为文档的no-extraneous-dependencies状态:
When using an array of globs, the setting will be set to true (no errors reported) if the name of the file being linted matches a single glob in the array, and false otherwise.
使用 glob 数组时,如果被 linted 的文件名与数组中的单个 glob 匹配,则该设置将设置为 true(不报告错误),否则设置为 false。
The following setting will disable the lint for test files only.
以下设置将仅为测试文件禁用 lint。
"import/no-extraneous-dependencies": ["error", {"devDependencies": ["**/*.test.ts", "**/*.test.tsx"]}]
That way imports from devDependenciesare still reported as errors.
这样导入 fromdevDependencies仍然报告为错误。

