【问题标题】:Why doesn't atom package linter-eslint recognize template literals?为什么 atom 包 linter-eslint 不能识别模板文字?
【发布时间】:2016-10-08 05:50:48
【问题描述】:

ES6 模板文字上的documentation 具有以下语法示例:

`string text ${expression} string text`

接下来,我的功能是:

function madLib (verb, adjective, noun) {
    return `We shall ${verb} the ${adjective} ${noun}.`;
}

在控制台中,按预期输出:

We shall fly the iridescent zoo.

但是,eslinter 包在第一个反引号时抱怨一个致命的解析错误,引用

Unexpected character '`' 

这是为什么?

(文字上的 SO post 是关于语法错误——找不到任何其他相关的 eslinter 帖子。)

【问题讨论】:

    标签: ecmascript-6 atom-editor eslint template-literals


    【解决方案1】:

    ESLint 默认配置为仅 lint ES5 代码。模板文字是 ES6 规范的一部分。因此,解析器将无法解析您的 JavaScript 代码并出现致命错误。您需要在项目的根目录中创建一个.eslintrc 文件并将ecmaVersion 设置为6。有关配置ESLint 的更多信息,请参阅http://eslint.org/docs/user-guide/configuring#specifying-parser-options

    【讨论】:

    • 工作,谢谢!另一个给你的 Ilya,ESLint 仍然让我对默认参数分配感到悲伤,例如function myFunction (name = 'hello') { return name; }。它说Unexpected character '='。这不是通过将ecmaVersion 版本更改为 6 来解决的吗?
    • 启用 ES6 后应该可以工作。默认参数是 ES6 规范的一部分。
    【解决方案2】:

    这是你可以做到的。在 .eslintrc.js 中更改“引号”的值(对我来说是单个),如下所示:

    "quotes": ["error", "single", {
            avoidEscape: true,
            allowTemplateLiterals: true
        }],
    

    您可以使用反引号并避免转义!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-09
      • 2014-09-03
      • 1970-01-01
      • 2019-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多