14

.js 应用程序。我需要为此应用程序使用 eslint。我正在使用https://www.npmjs.com/package/eslint-config-airbnb并在 VS Code 编辑器中使用更漂亮的插件。

.eslintrc

{
  "extends": "airbnb"
}

在添加 eslint 插件https://marketplace.visualstudio.comitems?itemName=dbaeumer.vscode-eslint和 npm 包后,我看到 VS Code 现在在完整项目中给了我很多错误。

很少的错误

[eslint] Definition for rule 'jsx-a11y/href-no-hash' was not found (jsx-a11y/href-no-hash)
[eslint] Expected linebreaks to be 'LF' but found 'CRLF'. (linebreak-style)
[eslint] Unexpected unnamed function. (func-names)
[eslint] Missing space before function parentheses. (space-before-function-paren)
[eslint] Strings must use singlequote. (quotes)
[eslint] Unexpected function expression. (prefer-arrow-callback)
[eslint] Unexpected unnamed function 'bind'. (func-names)
[eslint] Missing space before function parentheses. (space-before-function-paren)

包.json

  "devDependencies": {
    "babel": "^6.23.0",
    "babel-cli": "^6.5.1",
    "babel-core": "^6.9.0",
    "babel-eslint": "^7.2.3",
    "babel-preset-es2015": "^6.5.0",
    "babel-preset-stage-0": "6.5.0",
    "eslint": "^3.19.0",
    "eslint-config-airbnb": "^15.1.0",
    "eslint-plugin-import": "^2.7.0",
    "eslint-plugin-jsx-a11y": "^6.0.2",
    "eslint-plugin-react": "^7.4.0",
    "nodemon": "^1.12.1"
  }

index.js

import request from "superagent";

module.exports = function(req, res, next) {
  const id = "abc";

  request
    .post(url)
    .send(`p1=v1`)
    .send(`p2=v2`)
    .end(function(error, response) {}.bind(this));
  next();
};

在此处输入图像描述

每个 JS 文件中都有相同类型的错误。有谁知道如何解决这些问题?

4

5 回答 5

27

1)将以下模块添加到您的devDependencies使用中:

npm install --save-dev eslint
npm install --save-dev eslint-config-airbnb-base
npm install --save-dev eslint-plugin-import

2)添加一个eslintConfig部分到您的package.json

"eslintConfig": {
    "extends": "airbnb-base",
    "env": {
        "es6": true,
        "browser": true
    },
    "rules": {
        "brace-style": [
            "error",
            "stroustrup"
        ],
        "comma-dangle": [
            "error",
            "never"
        ],
        "no-unused-vars": [
            "warn"
        ],
        "no-var": [
            "off"
        ],
        "one-var": [
            "off"
        ]
    }
}

3) 访问eslint.org/docs/rules,搜索你想要调整的警告并将它们添加到eslintConfig上面。

4)删除.eslintrc项目根目录中的文件。

5)重启你的IDE

于 2017-10-10T19:55:51.580 回答
5

将 eslint 包本地安装到您的项目中。

$yarn add eslint --dev

理想情况下,可以通过以下命令生成配置文件 .eslintrc,不幸的是它安装了错误版本的 peerDependencies 导致出现奇怪的 linting 错误。

$./node_modules/.bin/eslint --init

修复 peerDependencies 版本问题的更简单方法,使用以下命令并安装(我建议您安装与 jsx 相关的软件包并做出反应,尽管您可能不会做任何与 React 相关的事情)peerDependencies 的相应版本以及 eslint-config-airbnb

$npm info "eslint-config-airbnb@latest" peerDependencies

使用以下内容编辑 .eslintrc 文件

{
  "extends": "airbnb",
  "plugins": [
    "react",
    "jsx-a11y",
    "import"
  ],
  "rules": {
  },
  "env": {
  }
}

*注意:请根据您的编码规则,您遵循的环境编辑 eslintrc 文件。参考开发者指南

于 2018-03-07T10:10:45.497 回答
3

ESLint 写了你需要做的消息。就读吧。

具有此规则的正确代码应如下所示:

import request from 'superagent';

module.exports = (req, res, next) => {
  const id = 'abc';

  request
    .post(url)
    .send('p1=v1')
    .send('p2=v2')
    .end((error, response) => {
      // do something on end
    });
  next();
};
于 2017-10-09T09:22:57.370 回答
1

其他答案很接近,希望这个答案有助于完整的答案:

1:

文件 .eslintrc.js

module.exports = {
  env: {
    es6: true,
    node: true,
  },
  extends: "eslint:recommended",
  globals: {
    Atomics: "readonly",
    SharedArrayBuffer: "readonly",
  },
  // parser: "babel-eslint",
  parserOptions: {
    ecmaVersion: 2018,
    sourceType: "module",
  },
  // plugins: [],
  rules: {
    "no-console": "off", // "warn" // "off"
  },
  settings: {},
};

2:

文件 .eslintignore

node_modules/

3:

文件package.json

{
  "scripts": {
    "lint": "eslint \"./**/*.js\" --quiet",
    "lintFull": "eslint \"./**/*.js\"",
    "lintFix": "eslint --fix './**/*.js'"
  },
  "devDependencies": {
    "eslint": "^6.8.0",
  }
}

4:

npm i
npm run lint

就这样

----------------

但是,此外,如果您想使用 Typescript 等自定义设置制作自己的 .eslintrc.js,请尝试以下操作:

1:

npm i eslint -g

2:

eslint --init

? How would you like to use ESLint? To check syntax and find problems
? What type of modules does your project use? JavaScript modules (import/export)
? Which framework does your project use? None of these
? Does your project use TypeScript? No
? Where does your code run? Node
? What format do you want your config file to be in? JavaScript
    Successfully created .eslintrc.js file in C:\repo\nodeapp1

希望有帮助。

于 2020-05-11T06:00:33.083 回答
-4

如果你使用 atom, vs code... 可能编辑器需要全局安装 eslint。

npm install -g eslint

于 2017-10-04T10:36:03.927 回答