eslint parsing error: Parsing error: Unexpected token =>

eslint parsing error: Parsing error: Unexpected token =>

我使用的是支持 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();
  }
}

当我这样定义 class 时,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)
  }
}

我该如何解决这个问题?

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

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

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

最后我使用了一个工作正常的不同 linter。

我现在使用的是eslint 3.10.1

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

parserOptions.ecmaVersion = 8

async/await 在 ES8(又名 ES2017)中我认为是在 ES7