【问题标题】:Babel / ESLint failing on Arrow FunctionsBabel / ESLint 在箭头函数上失败
【发布时间】:2016-10-01 06:39:36
【问题描述】:

我正在使用最新的 babel/eslint(参见下面的 packages.json)。使用箭头函数时出现此错误:

功能:

  handleChange = (event, index, value) => this.setState({value});

错误:

Fatal Parsing error: Unexpected token =: src/components/Dashboard.js:15

我读到这在 5.x 版本的 Babel 中存在问题,但已修复,我现在大约 6 岁,所以我不太确定是什么导致了问题。我的想法是它必须是 eslint 或 babel 自己。谢谢!

Packages.json

    {
  "name": "react-slingshot",
  "version": "4.0.0",
  "description": "Starter kit for creating apps with React and Redux",
  "scripts": {
    "remove-demo": "babel-node tools/removeDemo.js",
    "start-message": "babel-node tools/startMessage.js",
    "prestart": "npm-run-all --parallel start-message remove-dist",
    "start": "npm-run-all --parallel open:src lint:watch",
    "open:src": "babel-node tools/srcServer.js",
    "open:dist": "babel-node tools/distServer.js",
    "lint": "esw webpack.config.* src tools",
    "lint:watch": "npm run lint -- --watch",
    "clean-dist": "npm run remove-dist && mkdir dist",
    "remove-dist": "rimraf ./dist",
    "build:html": "babel-node tools/buildHtml.js",
    "prebuild": "npm run clean-dist && npm run build:html && npm run lint && npm run test",
    "build": "babel-node tools/build.js && npm run open:dist"
  },
  "author": "",
  "dependencies": {
    "material-ui": "0.15.0",
    "object-assign": "4.1.0",
    "react": "15.1.0",
    "react-dom": "15.1.0",
    "react-redux": "4.4.5",
    "react-router": "2.4.1",
    "react-tap-event-plugin": "1.0.0",
    "redux": "3.5.2"
  },
  "devDependencies": {
    "babel-cli": "6.9.0",
    "babel-core": "6.9.1",
    "babel-eslint": "6.0.4",
    "babel-loader": "6.2.4",
    "babel-plugin-react-display-name": "2.0.0",
    "babel-preset-es2015": "6.9.0",
    "babel-preset-react": "6.5.0",
    "babel-preset-react-hmre": "1.1.1",
    "babel-preset-stage-1": "6.5.0",
    "babel-register": "6.9.0",
    "browser-sync": "2.12.10",
    "chai": "3.5.0",
    "chalk": "1.1.3",
    "cheerio": "0.20.0",
    "connect-history-api-fallback": "1.2.0",
    "coveralls": "2.11.9",
    "cross-env": "1.0.8",
    "css-loader": "0.23.1",
    "enzyme": "2.3.0",
    "eslint": "2.11.1",
    "eslint-config-airbnb": "9.0.1",
    "eslint-plugin-babel": "3.2.0",
    "eslint-plugin-import": "1.8.1",
    "eslint-plugin-jsx-a11y": "1.2.2",
    "eslint-plugin-react": "5.1.1",
    "eslint-watch": "2.1.11",
    "extract-text-webpack-plugin": "1.0.1",
    "file-loader": "0.8.5",
    "isparta": "4.0.0",
    "mocha": "2.5.3",
    "node-sass": "3.7.0",
    "npm-run-all": "2.1.1",
    "react-addons-test-utils": "15.1.0",
    "redux-immutable-state-invariant": "1.2.3",
    "rimraf": "2.5.2",
    "sass-loader": "3.2.0",
    "sinon": "1.17.4",
    "sinon-chai": "2.8.0",
    "style-loader": "0.13.1",
    "webpack": "1.13.1",
    "webpack-dev-middleware": "1.6.1",
    "webpack-hot-middleware": "2.10.0"
  },
  "keywords:": [
    "react",
    "reactjs",
    "react-router",
    "hot",
    "reload",
    "hmr",
    "live",
    "edit",
    "webpack",
    "redux",
    "flux",
    "boilerplate",
    "starter"
  ]
}

.eslintrc:

{
  "extends": [
    "eslint:recommended",
    "plugin:import/errors",
    "plugin:import/warnings"
  ],
  "plugins": [
    "react"
  ],
  "parserOptions": {
    "ecmaVersion": 6,
    "sourceType": "module",
    "ecmaFeatures": {
      "jsx": true,
      "experimentalObjectRestSpread": true
    }
  },
  "env": {
    "es6": true,
    "browser": true,
    "node": true,
    "jquery": true,
    "mocha": true
  },
  "rules": {
    "quotes": 0,
    "no-console": 1,
    "no-debugger": 1,
    "no-var": 1,
    "semi": [1, "always"],
    "no-trailing-spaces": 0,
    "eol-last": 0,
    "no-underscore-dangle": 0,
    "no-alert": 0,
    "no-lone-blocks": 0,
    "jsx-quotes": 1,
    "react/display-name": [ 1, {"ignoreTranspilerName": false }],
    "react/forbid-prop-types": [1, {"forbid": ["any"]}],
    "react/jsx-boolean-value": 0,
    "react/jsx-closing-bracket-location": 0,
    "react/jsx-curly-spacing": 1,
    "react/jsx-indent-props": 0,
    "react/jsx-key": 1,
    "react/jsx-max-props-per-line": 0,
    "react/jsx-no-bind": 0,
    "react/jsx-no-duplicate-props": 1,
    "react/jsx-no-literals": 0,
    "react/jsx-no-undef": 1,
    "react/jsx-pascal-case": 1,
    "react/jsx-sort-prop-types": 0,
    "react/jsx-sort-props": 0,
    "react/jsx-uses-react": 1,
    "react/jsx-uses-vars": 1,
    "react/no-danger": 1,
    "react/no-did-mount-set-state": 1,
    "react/no-did-update-set-state": 1,
    "react/no-direct-mutation-state": 1,
    "react/no-multi-comp": 1,
    "react/no-set-state": 1,
    "react/no-unknown-property": 1,
    "react/prefer-es6-class": 1,
    "react/prop-types": 1,
    "react/react-in-jsx-scope": 1,
    "react/require-extension": 1,
    "react/self-closing-comp": 1,
    "react/sort-comp": 1,
    "react/wrap-multilines": 1
  },
  "globals": {
    "React": true
  }
}

.babelrc

  "presets": [
    "es2015",
    "react"
  ],
  "env": {
    "development": {
      "presets": [
        "react-hmre"
      ]
    }
  }
}

【问题讨论】:

    标签: javascript eslint babeljs


    【解决方案1】:

    将我的 babelrc 更改为“stage-1”解决了这个问题:

    {
      "presets": [
        "es2015",
        "react",
        "stage-1"
      ],
      "env": {
        "development": {
          "presets": [
            "react-hmre"
          ]
        }
      }
    }
    

    如果有人有更好的答案,我会尽力而为。

    【讨论】:

      【解决方案2】:

      你也可以使用babel's transform-class-properties plugin:

      安装: npm install --save-dev babel-plugin-transform-class-properties

      添加到 babelrc:

        "plugins": [
          "transform-class-properties",
        ],
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-09-13
        • 2017-12-13
        • 2016-07-06
        • 1970-01-01
        • 2020-06-26
        • 2020-07-30
        • 1970-01-01
        • 2019-05-23
        相关资源
        最近更新 更多