ESLint 解析错误:意外的令牌 [英] ESLint Parsing error: Unexpected token
问题描述
使用此代码:
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 错误:
I get this eslint error:
7:16 error Parsing error: Unexpected token .. Why?
这是我的 eslint 配置:
Here is my eslint config:
{
"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
}],
}
}
........有什么问题?
.... .... What's the problem?
推荐答案
由于您的开发环境与 ESLint 当前的解析能力不兼容,以及 JavaScript ES6~7 的持续变化,导致 ESLint 解析出现意外令牌错误.
Unexpected token errors in ESLint parsing occur due to incompatibility between your development environment and ESLint's current parsing capabilities with the ongoing changes with JavaScripts ES6~7.
添加parserOptions";.eslintrc 的属性不再适用于特定情况,例如使用
Adding the "parserOptions" property to your .eslintrc is no longer enough for particular situations, such as using
static contextTypes = { ... } /* react */
在 ES6 类中,ESLint 目前无法自行解析.这种特殊情况会抛出以下错误:
in ES6 classes as ESLint is currently unable to parse it on its own. This particular situation will throw an error of:
error Parsing error: Unexpected token =
解决方案是让 ESLint 由兼容的解析器解析,即 @babel/eslint-parser 或 babel-eslint 用于 v7 以下的 babel 版本.
The solution is to have ESLint parsed by a compatible parser, i.e @babel/eslint-parser or babel-eslint for babel version below v7.
只需添加:
"parser": "@babel/eslint-parser"
到你的 .eslintrc
文件并运行 npm install @babel/eslint-parser --save-dev
或 yarn add -D @babel/eslint-解析器
.
to your .eslintrc
file and run npm install @babel/eslint-parser --save-dev
or yarn add -D @babel/eslint-parser
.
请注意,由于从 React ^16.3
开始的 新 Context API 有一些重要的变化,请参阅 官方指南.
Please note that as the new Context API starting from React ^16.3
has some important changes, please refer to the official guide.
这篇关于ESLint 解析错误:意外的令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!