eslint解析错误:解析错误:意外的令牌=>

时间:2017-05-12 09:17:06

标签: node.js asynchronous async-await eslint

我正在使用支持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)
  }
}

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

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

要执行此操作,请安装babel-eslint包并添加到.eslintrc.json(或等效的js)中:

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

答案 1 :(得分:0)

最后,我使用了不同的linter,效果很好。

我现在正在使用eslint 3.10.1