2016-03-15 114 views
14

有了這個代碼:ESLint解析錯誤:意外的令牌

import React from 'react'; 
import { Link } from 'react-router'; 
import { View, NavBar } from 'amazeui-touch'; 

import * as Pages from '../components'; 

const { Home, ...Components } = Pages; 

我得到這個eslint錯誤:

7:16 error Parsing error: Unexpected token .. Why? 

這裏是我的eslint配置:

{ 
    "extends": "airbnb", 
    "rules": { 
    /* JSX */ 
    "react/prop-types": [1, { 
     "ignore": ["className", "children", "location", "params", "location*"] 
    }], 
    "no-param-reassign": [0, { 
     "props": false 
    }], 
    "prefer-rest-params": 1, 
    "arrow-body-style": 0, 
    "prefer-template": 0, 
    "react/prefer-stateless-function": 1, 
    "react/jsx-no-bind": [0, { 
     "ignoreRefs": false, 
     "allowArrowFunctions": false, 
     "allowBind": true 
    }], 
    } 
} 

.... .... 什麼問題?

+0

你可以發表你的eslint配置? – azium

+0

謝謝 我已經上傳〜 – DongYao

+2

您需要使用支持對象傳播屬性提議的解析器。 –

回答

16

ESLint 2.x的實驗支持ObjectRestSpread語法,您可以通過添加啓用它下面您.eslintrcdocs

"parserOptions": { 
    "ecmaVersion": 6, 
    "ecmaFeatures": { 
    "experimentalObjectRestSpread": true 
    } 
}, 

ESLint 1.x中不支持本地傳播經營者,一個方式來獲得在這附近使用babel-eslint parser。最新的安裝和使用說明在項目自述文件中。

+2

這不是事實。 ESLint的默認解析器Espree支持傳播,甚至是對象休息傳播(這是espree支持的唯一實驗性功能)。欲瞭解更多信息,請參閱:http://eslint.org/docs/user-guide/configuring#specifying-parser-options –

+0

你是對的,我的原始答案只適用於ESLint 1.x,我更新了信息2.x –

8

由於您的開發環境與ESLint當前的解析功能與JavaScript ES6〜7的持續更改不兼容,ESLint解析中出現意外的令牌錯誤。

添加「parserOptions」屬性,你.eslintrc不再足以特定情況下,如在ES6類中使用

static contextTypes = { ... } /* react */ 

爲ESLint目前無法解析它自身。這種特殊情況會導致以下錯誤:

error Parsing error: Unexpected token = 

解決方案是讓ESLint通過兼容的解析器進行解析。 babel-eslint是在閱讀本頁後最近保存了我的一個軟件包,我決定將此作爲替代解決方案,供以後任何人使用。

只需添加:

"parser": "babel-eslint" 

.eslintrc文件並運行npm install babel-eslint --save-dev

-1

"parser": "babel-eslint"對我來說OK!

reading more : https://github.com/gildata/Roles/issues/6

{ 
 
    "parser": "babel-eslint", 
 
    "parserOptions": { 
 
     "ecmaVersion": 6, 
 
     "sourceType": "module", 
 
     "ecmaFeatures": { 
 
      "jsx": true, 
 
      "modules": true, 
 
      "experimentalObjectRestSpread": true 
 
     } 
 
    }, 
 
    "plugins": [ 
 
     "react" 
 
    ], 
 
    "extends": ["eslint:recommended", "plugin:react/recommended"], 
 
    "rules": { 
 
     "comma-dangle": 0, 
 
     "react/jsx-uses-vars": 1, 
 
     "react/display-name": 1, 
 
     "no-unused-vars": "warn", 
 
     "no-console": 1, 
 
     "no-unexpected-multiline": "warn" 
 
    }, 
 
    "settings": { 
 
     "react": { 
 
      "pragma": "React", 
 
      "version": "15.6.1" 
 
     } 
 
    } 
 
}

+1

此答案不會向@ JaysQubeXon的答案添加任何內容。 – cs01

相關問題