【问题标题】:error eslint parsing: unexpected token eslint error错误 eslint 解析:意外的令牌 eslint 错误
【发布时间】:2019-12-25 04:12:02
【问题描述】:

我在方括号上得到了意外的标记。

我已尝试设置 eslint 参数并安装 babel-eslint,但对我没有任何帮助。

const [state,dispatch] = useReducer(createUserReducer, 
  {
    email: '',
    password: '',
    verifyPassword: ''
   });


my eslint configuration: 

{
  "parser": "babel-eslint",
  "parserOptions": {
    "ecmaVersion": 6,
    "sourceType": "module",
    "ecmaFeatures": {
      "jsx": true,
      "modules": true,
      "blockBindings": true,
      "experimentalObjectRestSpread": true
    }
  },
  "extends": "rallycoding",
  "rules": {
    "react/require-extension": "off",
    "global-require": 0,
    "no-unused-vars": 0,
    "unexpected-token": 0
  }
}

我应该能够构建代码,但 eslint 抛出错误,提示意外令牌。

【问题讨论】:

    标签: javascript react-native ecmascript-6 eslint ecmascript-2016


    【解决方案1】:

    请注意,支持JSX 语法与支持React 不同。 React 将特定语义应用于 ESLint 无法识别的 JSX 语法。如果您正在使用 React 并且想要 React 语义,我们建议使用 eslint-plugin-react。同理,支持 ES6 语法与支持新的 ES6 全局变量(例如 Set 等新类型)不同。对于 ES6 语法,使用 { "parserOptions": { "ecmaVersion": 6 } };对于新的ES6 全局变量,请使用{ "env": { "es6": true } }

    【讨论】:

    • 我看不出这与问题中的代码有何关系,它根本不使用 jsx?
    • @Bergi 提问者想全局使用参数。但就像我的回答一样,React 不会自动激活整个 ES6。
    【解决方案2】:

    试试这些配置:

    eslintrc.js:

    module.exports = {
          root: true,
          "extends": "eslint:recommended",
        };
    

    eslintrc.json:

    {
        "env": {
            "browser": true,
            "es6": true
        },
        "extends": [
            "google"
        ],
        "globals": {
            "Atomics": "readonly",
            "SharedArrayBuffer": "readonly"
        },
        "parserOptions": {
            "ecmaFeatures": {
                "jsx": true
            },
            "ecmaVersion": 2018,
            "sourceType": "module"
        },
        "plugins": [
            "react"
        ],
        "rules": {
        }
    }
    

    【讨论】:

    猜你喜欢
    • 2022-01-02
    • 2017-11-21
    • 2017-07-16
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 2018-08-14
    • 2021-01-15
    相关资源
    最近更新 更多