ESLint:let的第一个实例导致“意外令牌”错误 [英] ESLint: First instance of let results in "unexpected token" error

查看:203
本文介绍了ESLint:let的第一个实例导致“意外令牌”错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的javascript:

My javascript:

let foo = 'bar'

为什么ESLint会回复以下内容?

Why does ESLint respond with the following?

~/index.js
  1:5  error  Parsing error: Unexpected token foo

✖ 1 problem (1 error, 0 warnings)

似乎无论脚本在哪里,使用let设置变量的第一个实例都会收到此错误。为什么??

It seems like no matter where in the script, the first instance of using let to set a variable gets this error. Why??

我的.eslintrc文件:

My .eslintrc file:

module.exports = {
    "env": {
        "node": true
    },
    "extends": "eslint:recommended",
    "rules": {
        "indent": [
            "error",
            "tab"
        ],
        "linebreak-style": [
            "error",
            "unix"
        ],
        "quotes": [
            "error",
            "single"
        ],
        "semi": [
            "error",
            "never"
        ]
    }
};


推荐答案

关于的答案让在全局范围内被禁止是错误的。没关系。

The answers about let being forbidden in the global scope are wrong. That's fine.

问题是你需要让eslint知道你正在使用es6功能。

The problem is that you need to let eslint know you're using es6 features.

通过在配置中添加es6:true 行到 env 块来实现:

Do that by adding an "es6": true line to the env block in your config:

.eslintrc.js

module.exports = {
    "env": {
        "node": true,
        "es6": true
    },
    "extends": "eslint:recommended",
    "rules": {
        "indent": [
            "error",
            "tab"
        ],
        "linebreak-style": [
            "error",
            "unix"
        ],
        "quotes": [
            "error",
            "single"
        ],
        "semi": [
            "error",
            "never"
        ]
    }
};

这可以修复您的特定错误,但您很快就会看到有关未使用名称的错误。以下是我能找到的最小源代码,它将通过您的eslint设置:

That fixes your particular error, but you'll soon see errors about unused names. Below is the minimum source code I could find that will pass your eslint settings:

let.js

let foo = 'bar'

function main() {
  foo
}

main()

这篇关于ESLint:let的第一个实例导致“意外令牌”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆