【问题标题】:Eslint for a express and react project in the same project用于同一项目中的 express 和 react 项目的 Eslint
【发布时间】:2021-11-13 00:39:03
【问题描述】:

我有一个项目,其中包含 API (expess.js) 和客户端 (Reactjs)。

Project/
├─ client/           // All my reactjs files
├─ src/              // All my express.js files
├─ .eslintrc.json

我正在尝试配置 eslint,对于 express.js 部分我没有任何问题。但是对于反应部分,我有以下错误:

/home/dev/project/client/index.js
  5:17  error  Parsing error: Unexpected token <

这是我的 eslint 配置

{

    "env": {
      "node": true
    },
    "parserOptions": {
      "ecmaVersion": 2018,
      "sourceType": "module"
    },
  
    "rules": {
      "block-scoped-var":             ["error"],
      "callback-return":              ["error", ["done", "proceed", "next", "onwards", "callback", "cb"]],
      "comma-style":                  ["warn", "last"],
      "curly":                        ["warn"],
      "eqeqeq":                       ["error", "always"],
      "eol-last":                     ["warn"],
      "handle-callback-err":          ["error"],
      "indent":                       ["warn", 2, {
        "SwitchCase": 1,
        "MemberExpression": "off",
        "FunctionDeclaration": {"body":1, "parameters":"off"},
        "FunctionExpression": {"body":1, "parameters":"off"},
        "CallExpression": {"arguments":"off"},
        "ArrayExpression": 1,
        "ObjectExpression": 1,
        "ignoredNodes": ["ConditionalExpression"]
      }],
      "linebreak-style":              ["error", "unix"],
      "no-dupe-keys":                 ["error"],
      "no-duplicate-case":            ["error"],
      "no-extra-semi":                ["warn"],
      "no-labels":                    ["error"],
      "no-mixed-spaces-and-tabs":     [2, "smart-tabs"],
      "no-redeclare":                 ["warn"],
      "no-return-assign":             ["error", "always"],
      "no-sequences":                 ["error"],
      "no-trailing-spaces":           ["warn"],
      "no-undef":                     ["off"],
      "no-unexpected-multiline":      ["warn"],
      "no-unreachable":               ["warn"],
      "no-unused-vars":               ["warn", {"caughtErrors":"all", "caughtErrorsIgnorePattern": "^unused($|[A-Z].*$)", "argsIgnorePattern": "^unused($|[A-Z].*$)", "varsIgnorePattern": "^unused($|[A-Z].*$)" }],
      "no-use-before-define":         ["error", {"functions":false}],
      "one-var":                      ["warn", "never"],
      "prefer-arrow-callback":        ["warn", {"allowNamedFunctions":true}],
      "quotes":                       ["warn", "single", {"avoidEscape":false, "allowTemplateLiterals":true}],
      "semi":                         ["warn", "always"],
      "semi-spacing":                 ["warn", {"before":false, "after":true}],
      "semi-style":                   ["warn", "last"]
    }
  
  }
  

我尝试添加这些行:

"overrides": [
      {
        "files": [ "client/**/*.js" ],
        "parser": "@babel/eslint-parser",
        "parserOptions": {
          "requireConfigFile": false,
          "ecmaFeatures": {
            "jsx": true
          }
        },
        "extends": [
          "eslint:recommended",
          "plugin:react/recommended"
        ],
        "plugins": [
          "react"
        ]
      }
    ],

但我还是有错误

/home/dev/project/client/index.js
  5:16  error  Parsing error: This experimental syntax requires enabling one of the following parser plugin(s): 'jsx, flow, typescript' (5:16)

如何在同一个项目中同时使用 eslint 和 express.js 后端和 reactjs 前端?

【问题讨论】:

    标签: reactjs express eslint eslintrc


    【解决方案1】:

    我终于找到了解决方案,在使用的覆盖行中,我通过 jsx 更改了反应插件。

    为此我需要安装一个新包

    npm install eslint-plugin-jsx@latest --save-dev
    

    这里是覆盖部分:

    "overrides": [
      {
        "files": [ "client/**/*.js" ],
        "parser": "@babel/eslint-parser",
        "parserOptions": {
          "requireConfigFile": false,
          "ecmaFeatures": {
            "jsx": true
          },
          "babelOptions": {
            "presets": ["@babel/preset-react"]
         }
        },
        "extends": [
          "eslint:recommended",
          "plugin:react/recommended"
        ],
        "plugins": [
          "jsx"
        ]
      }
    ],
    

    这是完整的 .eslintrc.json

    {
    
        "env": {
          "node": true,
          "es6": true
        },
        "parserOptions": {
          "ecmaVersion": 2018,
          "sourceType": "module"
        },
        "overrides": [
          {
            "files": [ "client/**/*.js" ],
            "parser": "@babel/eslint-parser",
            "parserOptions": {
              "requireConfigFile": false,
              "ecmaFeatures": {
                "jsx": true
              },
              "babelOptions": {
                "presets": ["@babel/preset-react"]
             }
            },
            "extends": [
              "eslint:recommended",
              "plugin:react/recommended"
            ],
            "plugins": [
              "jsx"
            ]
          }
        ],
      
        "rules": {
          "block-scoped-var":             ["error"],
          "callback-return":              ["error", ["done", "proceed", "next", "onwards", "callback", "cb"]],
          "comma-style":                  ["warn", "last"],
          "curly":                        ["warn"],
          "eqeqeq":                       ["error", "always"],
          "eol-last":                     ["warn"],
          "handle-callback-err":          ["error"],
          "indent":                       ["warn", 2, {
            "SwitchCase": 1,
            "MemberExpression": "off",
            "FunctionDeclaration": {"body":1, "parameters":"off"},
            "FunctionExpression": {"body":1, "parameters":"off"},
            "CallExpression": {"arguments":"off"},
            "ArrayExpression": 1,
            "ObjectExpression": 1,
            "ignoredNodes": ["ConditionalExpression"]
          }],
          "linebreak-style":              ["error", "unix"],
          "no-dupe-keys":                 ["error"],
          "no-duplicate-case":            ["error"],
          "no-extra-semi":                ["warn"],
          "no-labels":                    ["error"],
          "no-mixed-spaces-and-tabs":     [2, "smart-tabs"],
          "no-redeclare":                 ["warn"],
          "no-return-assign":             ["error", "always"],
          "no-sequences":                 ["error"],
          "no-trailing-spaces":           ["warn"],
          "no-undef":                     ["off"],
          "no-unexpected-multiline":      ["warn"],
          "no-unreachable":               ["warn"],
          "no-unused-vars":               ["warn", {"caughtErrors":"all", "caughtErrorsIgnorePattern": "^unused($|[A-Z].*$)", "argsIgnorePattern": "^unused($|[A-Z].*$)", "varsIgnorePattern": "^unused($|[A-Z].*$)" }],
          "no-use-before-define":         ["error", {"functions":false}],
          "one-var":                      ["warn", "never"],
          "prefer-arrow-callback":        ["warn", {"allowNamedFunctions":true}],
          "quotes":                       ["warn", "single", {"avoidEscape":false, "allowTemplateLiterals":true}],
          "semi":                         ["warn", "always"],
          "semi-spacing":                 ["warn", {"before":false, "after":true}],
          "semi-style":                   ["warn", "last"]
        }
      
      }
    

    我在article找到了解决方案

    【讨论】:

      猜你喜欢
      • 2021-05-14
      • 2019-11-02
      • 2019-03-07
      • 2018-10-20
      • 2017-06-16
      • 1970-01-01
      • 2022-01-12
      • 2021-07-08
      • 2017-06-22
      相关资源
      最近更新 更多