【问题标题】:eslint parsing error: Parsing error: Unexpected token =>eslint 解析错误:解析错误:Unexpected token =>
【发布时间】:2017-10-11 13:08:54
【问题描述】:

我正在使用支持 ES6 的节点 v7.10.0,因此我不转译我的代码。

ESLint v3.19.0 在以下代码中给出错误Parsing error: Unexpected token =>

给出错误:

module.exports = {

    index: async (req, res) => {
        await functionThatReturnsSomePromise();
  }
}

另外,当我只使用函数时,它会失败并出现错误Parsing error: Unexpected token function

给出错误:

module.exports = {

    index: async function(req, res) {
        await functionThatReturnsSomePromise();
  }
}

当我定义这样的类时,linter 不会抱怨它:

没有错误:

class test {

    testing() {
        async () => {
            console.log('test');
        }
    }
}

.eslintrc

{
  "extends": "eslint:recommended",
  "ecmaFeatures": {
    "binaryLiterals": true,                    // enable binary literals
     "blockBindings": true,                      // enable let and const (aka block bindings)
     "defaultParams": true,                     // enable default function parameters
     "forOf": true,                             // enable for-of loops
     "generators": true,                        // enable generators
     "objectLiteralComputedProperties": true,   // enable computed object literal property names
     "objectLiteralDuplicateProperties": true,  // enable duplicate object literal properties in strict mode
     "objectLiteralShorthandMethods": true,     // enable object literal shorthand methods
     "objectLiteralShorthandProperties": true,  // enable object literal shorthand properties
     "octalLiterals": true,                     // enable octal literals
     "regexUFlag": true,                        // enable the regular expression u flag
     "regexYFlag": true,                        // enable the regular expression y flag
     "templateStrings": true,                   // enable template strings
     "unicodeCodePointEscapes": true,           // enable code point escapes
     "jsx": true                                // enable JSX
  },

  "env": {
    "browser": false,     // browser global variables.
    "node": true,        // Node.js global variables and Node.js-specific rules.
    "es6": true,          // for ES6
    "amd": false,         // defines require() and define() as global variables as per the amd spec.
    "mocha": true,       // adds all of the Mocha testing global variables.
    "jasmine": false,     // adds all of the Jasmine testing global variables for version 1.3 and 2.0.
    "phantomjs": false,   // phantomjs global variables.
    "jquery": false,      // jquery global variables.
    "prototypejs": false, // prototypejs global variables.
    "shelljs": false      // shelljs global variables.
  },

  "globals": {
    // e.g. "angular": true
  },

  "parserOptions": {
      "ecmaVersion": 7,
      "sourceType": "module",
      "ecmaFeatures": {
        arrowFunctions: true,
        defaultParams: true
      }
  },

  "rules": {
    ////////// Stylistic Issues //////////

    "no-underscore-dangle": 0,      // disallow dangling underscores in identifiers


    ////////// ECMAScript 6 //////////

    "no-var": 2          // require let or const instead of var (off by default)
  }
}

我该如何解决这个问题?

【问题讨论】:

    标签: node.js asynchronous async-await eslint


    【解决方案1】:

    我通过编辑解决了这个问题:

    parserOptions.ecmaVersion = 8
    

    async/await 在 ES8(又名 ES2017)中,我认为它在 ES7 中?

    【讨论】:

    • 是的,实际上是与节点版本相关的版本而不是 ecma 版本。
    • 谢谢,这是这里唯一有效的答案。用包来处理这种错误是很繁重的,而不是理解配置。
    【解决方案2】:

    由于您的代码需要转译,您是否尝试过使用babel-eslint 解析器?

    为此,请安装 babel-eslint 包并将其包含在您的 .eslintrc.json(或 js 等效项)中:

    {
      (...)
      "parser": "babel-eslint",
      (...)
    }
    

    【讨论】:

    • 我不编译我的代码,我使用节点 v7.10.0。我更新了问题。谢谢。当我使用旧的函数方法时,我也有错误。也许是因为它是一个 JsonObject 之类的结构,它不起作用?而不是一个类...
    【解决方案3】:

    最后,我使用了一个可以正常工作的其他 linter。

    我现在使用的是 eslint 3.10.1

    【讨论】:

      猜你喜欢
      • 2021-11-13
      • 1970-01-01
      • 2019-06-16
      • 1970-01-01
      • 2019-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-18
      相关资源
      最近更新 更多