简体   繁体   English

NodeJs 类静态属性 eslint 解析错误:Unexpected token =

[英]NodeJs class Static atribute eslint Parsing error: Unexpected token =

I am creating a NodeJs application with express.js, i created a class with static attribute.我正在使用 express.js 创建一个 NodeJs 应用程序,我创建了一个具有静态属性的类。

class ErrorCode {
    static userPasswordLength = {
        error: "ERROR_USER_PASSWORD_LENGTH"
    };
}

I am using eslint, here is my .eslintrc.json我正在使用 eslint,这是我的.eslintrc.json

{

    "env": {
      "node": true,
      "es6": true
    },
    "parserOptions": {
      "ecmaVersion": 2018,
      "sourceType": "module"
    },
    "overrides": [
      { 
        "env" : {
          "browser": true
        },
        "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": {
          "react/prop-types": "off"
        }        
      }
    ],
  
    "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"]
    }
  
  }

On my static attribute eslint is showing an error at the equal sign.在我的静态属性上,eslint 在等号处显示错误。 But my code works fine, i use it and nothing is crashing, so i don't understand why i have this error.但是我的代码工作正常,我使用它并且没有任何崩溃,所以我不明白为什么我有这个错误。

error  Parsing error: Unexpected token =

What rules should i disable or add ?我应该禁用或添加哪些规则? Or should i change something in my code ?或者我应该更改代码中的某些内容?

I changed my code to this, and now the error is fixed :我将代码更改为此,现在错误已修复:

class ErrorCode {
  static get(name) {
    let error;
    switch (name) {
      case 'userPasswordLength':
        error = {
          error: 'ERROR_USER_PASSWORD_LENGTH'
        };
        break;
    }
    return error;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM