简体   繁体   English

如何修复意外标记“/”的 ESLint 解析错误?

[英]How can I fix ESLint Parsing Error for unexpected token "/"?

在此处输入图片说明

const Index = () => (
    <div>
        <p>Hello World</p>
        <Link href="/posts">
            <a>Posts</a>
        </Link>
    </div>
)

ESLint is returning a Parsing Error (Unexpected token) for the closing </p> tag. ESLint 正在为结束</p>标记返回解析错误(意外标记)。 What am I missing?我错过了什么? Are normal HTML attributes not allowed in JSX? JSX 中不允许正常的 HTML 属性吗? (The div seems to work fine) div似乎工作正常)

The exact error is:确切的错误是:

[eslint] Parsing error: Unexpected token "/"
  • ESLint is installed ESLint 已安装
  • ESLint React is installed安装了 ESLint React
  • ESLint React is configured in .eslintrc.json ESLint React 在.eslintrc.json配置

EDIT:编辑:

  • Using VS Code (with ESLint plugin)使用 VS Code(使用 ESLint 插件)

Partial .eslintrc.json :部分.eslintrc.json

"env": {
    "browser": true,
    "commonjs": true,
    "es6": true
},
"extends": "eslint:recommended",
"parserOptions": {
    "ecmaFeatures": {
    "experimentalObjectRestSpread": true,
    "jsx": true
    },
    "sourceType": "module"
},
"plugins": [
    "react"
],
"rules": {
    ...
}

I received a similar error in Visual Studio 2017 (not Code) .我在Visual Studio 2017 (not Code) 中收到了类似的错误。

The error "ESLint encountered a parsing error" occurred at the beginning of an import statement.错误“ESLint 遇到解析错误”出现在import语句的开头。

janniks' solution did not work for me. janniks 的解决方案对我不起作用。 I suspect because "es6: true "enable[s] all ECMAScript 6 features except for modules" .我怀疑是因为"es6: true "启用[s] 除模块之外的所有 ECMAScript 6 功能"

Since I'm using TypeScript, I don't want to use babel-eslint , per Sean's answer (though it did resolve the parsing error in a plain JS file).由于我使用的是 TypeScript,我不想使用babel-eslint ,根据 Sean 的回答(尽管它确实解决了普通 JS 文件中的解析错误)。

The key trade-off can be summarized as: babel-eslint supports additional syntax which TypeScript itself does not, but typescript-eslint supports creating rules based on type information, which is not available to babel because there is no type-checker.关键的权衡可以概括为: babel-eslint支持 TypeScript 本身不支持的额外语法,但typescript-eslint支持根据类型信息创建规则,这对 babel 不可用,因为没有类型检查器。

Instead, I continued to use "@typescript-eslint/parser".相反,我继续使用“@typescript-eslint/parser”。 Below is my minimal working .eslintrc :下面是我的最小工作.eslintrc

{
    "parser": "@typescript-eslint/parser",
    "parserOptions": {
        "sourceType": "module"
    },
    "rules": {
        "quotes": [ "error", "single" ]
    }
}

Note: The error was resolved in plain JS files (not TS) even if I removed the "parser" line, therefore using the default eslint parser.注意:即使我删除了"parser"行,该错误也已在纯 JS 文件(而非 TS)中解决,因此使用默认的 eslint 解析器。

I encountered the same issue today while setting up a new React project.我今天在建立一个新的 React 项目时遇到了同样的问题。 The fix that worked for me was installing the babel-eslint package ( npm install babel-eslint --save-dev or yarn add babel-eslint -D ).对我babel-eslint的修复是安装babel-eslint包( npm install babel-eslint --save-devyarn add babel-eslint -D )。 I then added "parser": "babel-eslint" to my .eslintrc config file.然后我将"parser": "babel-eslint"到我的.eslintrc配置文件中。 This seems to help babel and ESLint get along a little better than using the default parser.与使用默认解析器相比,这似乎有助于 babel 和 ESLint 更好地相处。

I'm not sure what caused the problem, but this solved it for me.我不确定是什么导致了这个问题,但这为我解决了这个问题。 I changed the .eslintrc.json to the following:我将.eslintrc.json更改为以下内容:

{
    "env": {
        "browser": true,
        "commonjs": true,
        "es6": true
    },
    "extends": [
        "standard",
        "standard-react"
    ]
}

I left in my original rules as well.我也保留了原来的rules


This problem seems to have multiple different causes, so check out the other answers as well.这个问题似乎有多种不同的原因,所以也请查看其他答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM