Search code examples
node.jsexpresseslinteslintrc

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.

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

I am using eslint, here is my .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. 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 ?


Solution

  • 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;
      }
    }