ESLint没有输出

7
我正在尝试使用该教程中的ESLint:https://davidwalsh.name/eslint。我已经复制了示例文件。当我在目录中运行eslint uploader.js时,什么也没有发生——换行后提示符返回。没有任何输出。
我已经在同一目录下的JSON文件上运行它,并使用Grunt任务在整个项目的JS文件上运行它。这些会返回一些错误,但跟我的预期相差甚远。
当我在命令行上运行eslint时,它表现如预期——返回参数和选项文档。我尝试重新启动实例、重新安装ESLint,甚至有意将错误引入到uploader.js中,但什么也没发生,更不用说教程中显示的输出了。能有人提供帮助吗?

你期望得到什么输出呢? eslint 只是一个检查工具。 - E net4
教程非常具体,可以预期哪些错误-它们提供了一个示例文件及其输出(在“实际示例”中从顶部开始的第一张图片)。 - Ben
2个回答

9

从 ESLint 1.0.0 版本开始,默认关闭所有的代码检测规则。

该教程是在此版本之前编写的,因此可能会出现一些错误而没有配置文件。官方网站提供了一个 迁移指南。在您的情况下,您可能希望在整个教程中使用 "eslint:recommended" 扩展您的 ".eslintrc" 文件(或仅手动包含预期规则)。根据指南,以下内容是最接近旧版本的:

{
    "extends": "eslint:recommended",
    "rules": {
        "no-alert": 2,
        "no-array-constructor": 2,
        "no-caller": 2,
        "no-catch-shadow": 2,
        "no-empty-label": 2,
        "no-eval": 2,
        "no-extend-native": 2,
        "no-extra-bind": 2,
        "no-implied-eval": 2,
        "no-iterator": 2,
        "no-label-var": 2,
        "no-labels": 2,
        "no-lone-blocks": 2,
        "no-loop-func": 2,
        "no-multi-spaces": 2,
        "no-multi-str": 2,
        "no-native-reassign": 2,
        "no-new": 2,
        "no-new-func": 2,
        "no-new-object": 2,
        "no-new-wrappers": 2,
        "no-octal-escape": 2,
        "no-process-exit": 2,
        "no-proto": 2,
        "no-return-assign": 2,
        "no-script-url": 2,
        "no-sequences": 2,
        "no-shadow": 2,
        "no-shadow-restricted-names": 2,
        "no-spaced-func": 2,
        "no-trailing-spaces": 2,
        "no-undef-init": 2,
        "no-underscore-dangle": 2,
        "no-unused-expressions": 2,
        "no-use-before-define": 2,
        "no-with": 2,
        "camelcase": 2,
        "comma-spacing": 2,
        "consistent-return": 2,
        "curly": [2, "all"],
        "dot-notation": [2, { "allowKeywords": true }],
        "eol-last": 2,
        "no-extra-parens": [2, "functions"],
        "eqeqeq": 2,
        "key-spacing": [2, { "beforeColon": false, "afterColon": true }],
        "new-cap": 2,
        "new-parens": 2,
        "quotes": [2, "double"],
        "semi": 2,
        "semi-spacing": [2, {"before": false, "after": true}],
        "space-infix-ops": 2,
        "space-return-throw-case": 2,
        "space-unary-ops": [2, { "words": true, "nonwords": false }],
        "strict": [2, "function"],
        "yoda": [2, "never"]
    }
}

自从 版本 3.0.0 起,ESLint 将拒绝在没有配置文件的情况下工作,并打印出错误信息。这将有望防止人们遇到此问题。

是的! 小错误是最糟糕的。谢谢!很惊讶,因为这个教程甚至还不到一年... - Ben
很高兴能帮助到你。老实说,我更喜欢新的行为方式,它不那么狂热。根据迁移指南,我已添加了完整的.eslintrc文件(可以看到,它有许多规则)。 - E net4

1

我也遇到了同样的问题(顺便说一下,是在2023年)。我安装了eslint,但当我输入npx eslint app.js时,它返回的结果是空的,只有一个新的空行。 所以,对我来说解决方案是:

  1. I checked globally installed eslint npm list -g

  2. Eslint was installed globally, and I didn't want that, so I deleted it npm uninstall -g eslint

  3. Then I checked that package.json was on place (If you don't have package.json, then create it npm init and hit enter several times)

  4. I reinstalled eslint npm init eslint@/config

  5. And the most important part: in .eslintrc.json (which will be created after installing eslint) you have to set rules (I took this solution from an answer E_net4). For example:

     {
         "extends": "eslint:recommended",
         "rules": {
             "no-alert": 2,
             "no-array-constructor": 2,
             "no-caller": 2,
             "no-catch-shadow": 2,
             "no-empty-label": 2,
             "no-eval": 2,
             "no-extend-native": 2,
             "no-extra-bind": 2,
             "no-implied-eval": 2,
             "no-iterator": 2,
             "no-label-var": 2,
             "no-labels": 2,
             "no-lone-blocks": 2,
             "no-loop-func": 2,
             "no-multi-spaces": 2,
             "no-multi-str": 2,
             "no-native-reassign": 2,
             "no-new": 2,
             "no-new-func": 2,
             "no-new-object": 2,
             "no-new-wrappers": 2,
             "no-octal-escape": 2,
             "no-process-exit": 2,
             "no-proto": 2,
             "no-return-assign": 2,
             "no-script-url": 2,
             "no-sequences": 2,
             "no-shadow": 2,
             "no-shadow-restricted-names": 2,
             "no-spaced-func": 2,
             "no-trailing-spaces": 2,
             "no-undef-init": 2,
             "no-underscore-dangle": 2,
             "no-unused-expressions": 2,
             "no-use-before-define": 2,
             "no-with": 2,
             "camelcase": 2,
             "comma-spacing": 2,
             "consistent-return": 2,
             "curly": [2, "all"],
             "dot-notation": [2, { "allowKeywords": true }],
             "eol-last": 2,
             "no-extra-parens": [2, "functions"],
             "eqeqeq": 2,
             "key-spacing": [2, { "beforeColon": false, "afterColon": true }],
             "new-cap": 2,
             "new-parens": 2,
             "quotes": [2, "double"],
             "semi": 2,
             "semi-spacing": [2, {"before": false, "after": true}],
             "space-infix-ops": 2,
             "space-return-throw-case": 2,
             "space-unary-ops": [2, { "words": true, "nonwords": false }],
             "strict": [2, "function"],
             "yoda": [2, "never"]
         }
     }
    
  6. And npx eslint app.js returned a bunch of errors in my code (as it suppose to work). But I decided to change rules to some "standardized rules" and installed airbnb rules (instructions here https://www.npmjs.com/package/eslint-config-airbnb)

谢谢E_net4提供了解决办法。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接