node.js - 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 ?
解决方案
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;
}
}
推荐阅读
- pointers - 在 OpenCL 中按照结构访问结构化数据
- javascript - 如何使用反应路由器使目录中的每个文件成为自己的页面
- recursion - 没有尾调用优化的尾递归
- python-3.x - 嗨,我正在尝试使用 while 循环来显示 (4, 49, 2) ex 4 8 12 16 20 的范围......但我必须使用 while 循环。我将如何编码thise?
- tensorflow - 使用 DP-FedAvg 联合的 TensorFlow 中的每个示例剪辑
- python - 确定矩阵中是否存在复数
- java - 在Java中读取文件大于3 GB的Java流
- android - 使用 addView() 时未添加视图
- python - 将 vlc 窗口上的 tkinter 带到前台
- python - VS Code 中某些模块的导入时间长