2016-03-15 160 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

相关问题