question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Complex Async Arrow Throws Parser Error

See original GitHub issue

Environment Information

Node / Yarn Versions:

$ node --version
v6.11.4
$ yarn --version
1.3.2

ESLint Package Versions:

{
    "babel-eslint": "^8.0.2",
    "eslint": "^4.3.0",
    "eslint-config-airbnb": "^15.1.0",
    "eslint-import-resolver-node": "^0.3.1",
    "eslint-plugin-dependencies": "^2.4.0",
    "eslint-plugin-import": "^2.7.0",
    "eslint-plugin-jsx-a11y": "^5.1.1",
    "eslint-plugin-react": "^7.1.0"
}
Configuration
{
  "extends": "airbnb",
  "plugins": [
    "dependencies"
  ],
  "settings": {
    "import/resolver": {
      "node": {
        "moduleDirectory": [
          "client",
          "node_modules"
        ]
      }
    }
  },
  "parserOptions": {
    "ecmaVersion": 6,
    "sourceType": "module",
    "ecmaFeatures": {
      "jsx": true,
      "experimentalObjectRestSpread": true
    }
  },
  "rules": {
    "comma-dangle": ["error", "never"],
    "semi": ["error", "always"],
    "react/jsx-filename-extension": 0,
    "react/prop-types": 0,
    "react/no-find-dom-node": 0,
    "jsx-a11y/label-has-for": 0,
    "jsx-a11y/href-no-hash": 0,
    "dependencies/no-cycles": 1
  },
  "globals": {
    "document": true,
    "window": true
  },
  "env": {
    "jest": true
  }
}

Issue Details

const reducer = async (state = initialState, action) => {
  const { payload } = action;
  switch (action.type) {
  // ...
};

Specifically, the errors that are thrown are via the import module; rules no-named-as-default and no-named-as-default-member introduce Parse errors when attempting to validate.

   8:63  error    Parse errors in imported module './Path/pathReducer': Unexpected token => (18:54)  import/no-named-as-default
   8:63  error    Parse errors in imported module './Path/pathReducer': Unexpected token => (18:54)  import/no-named-as-default-member

Unfortunately this is entirely valid syntax. I’m not quite sure why this isn’t working.

After further investigation, it seems as though the eslint’s parser is interpreting async ( //... as a function call, not as a function declaration modifier construct; it’s as if it’s favoring the following code (which is entirely invalid):

const reducer = async(state = initialState, action); // => {

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
not-an-aardvarkcommented, Dec 13, 2017

The issue is that you’re using ecmaVersion: 6 in your configuration, and async functions were only added in ECMAScript 2017. To fix this, you should update your config:

  "parserOptions": {
-    "ecmaVersion": 6,
+    "ecmaVersion": 2017,
    "sourceType": "module",
    "ecmaFeatures": {
      "jsx": true,
      "experimentalObjectRestSpread": true
    }
  },
0reactions
ikhsanalatsarycommented, Jan 17, 2018
exports.readAllNotification = async (req, res) => {
  try {
    await Notifications.update();
    ....
  } catch (e) {
    ....
  }
}

screen shot 2018-01-17 at 11 36 21 am

my eslintrc:

{
  "parserOptions": {
    "ecmaVersion": 2017,
    "sourceType": "module",
    "ecmaFeatures": {
      "arrowFunctions": true,
      "blockBindings": true,
      "classes": true,
      "defaultParams": true,
      "destructuring": true,
      "experimentalObjectRestSpread": true,
      "forOf": true,
      "generators": true,
      "modules": true,
      "objectLiteralComputedProperties": true,
      "objectLiteralDuplicateProperties": false,
      "objectLiteralShorthandMethods": true,
      "objectLiteralShorthandProperties": true,
      "spread": true,
      "superInFunctions": true,
      "templateStrings": true,
      "jsx": true
    }
  },
  "extends": [
    "standard",
    "eslint:recommended",
    "plugin:node/recommended"
  ],
  "rules": {
    "camelcase": 0,
    "comma-dangle": [ 0, "always-multiline"],
    "comma-spacing": [2, { "before": false, "after": true }],
    "consistent-return": 0,
    "curly": 0,
    "default-case": 0,
    "eqeqeq": [2, "smart"],
    "func-names": 0,
    "guard-for-in": 2,
    "indent": [2, 2, { "SwitchCase": 1 }],
    "key-spacing": [2, { "beforeColon": false, "afterColon": true }],
    "keyword-spacing": [2, { "before": true, "after": true }],
    "max-len": 0,
    "new-cap": [2, { "newIsCapExceptions": ["acl.memoryBackend", "acl"] }],
    "no-bitwise": 0,
    "no-caller": 2,
    "no-console": 0,
    "no-else-return": 0,
    "no-empty-class": 0,
    "no-multi-spaces": 2,
    "no-param-reassign": 0,
    "no-shadow": 0,
    "no-spaced-func": 2,
    "no-throw-literal": 2,
    "no-trailing-spaces": 2,
    "no-undef": 2,
    "no-unneeded-ternary": 2,
    "no-unreachable": 2,
    "no-underscore-dangle": 0,
    "no-unused-expressions": 0,
    "no-unused-vars": 0,
    "no-use-before-define": [1, "nofunc"],
    "no-var": 0,
    "object-curly-spacing": [2, "always"],
    "one-var": [0, "never"],
    "one-var-declaration-per-line": [2, "always"],
    "padded-blocks": 0,
    "space-before-function-paren": 0,
    "space-in-parens": [2, "never"],
    "spaced-comment": [2, "always"],
    "strict": 0,
    "quote-props": 0,
    "quotes": [1, "single"],
    "wrap-iife": [2, "outside"],
    "vars-on-top": 0,
    "async-await/space-after-async": 2,
    "async-await/space-after-await": 2
  },
  "env": {
    "es6": true,
    "node": true,
    "mocha": true,
    "commonjs": true
  },
  "globals": {
    "by": true,
    "io": true,
    "Promise": true,
    "__TESTING__": true,
    "_": false,
    "ApplicationConfiguration": true
  },
  "plugins": ["node", "async-await"]
}

deps: “eslint”: “4.15.0”, “eslint-config-standard”: “11.0.0-beta.0”, “eslint-plugin-async-await”: “0.0.0”, “eslint-plugin-import”: “2.8.0”, “eslint-plugin-node”: “5.2.1”, “eslint-plugin-promise”: “3.6.0”, “eslint-plugin-standard”: “3.0.1”,

Read more comments on GitHub >

github_iconTop Results From Across the Web

ESLint parsing error when using arrow syntax with async
I'm using ESLint to analyze my code. The code runs fine, but I get this error from ...
Read more >
javascript arrow function async | The Search Engine You Control
The error you're having says consistent-return at the end, which basically means your function should consistently return the same type. With that in...
Read more >
JavaScript async and await - in plain English, please
It means the function may be taking a while before it finishes execution, returns a result, or throw an error. We use the...
Read more >
async - Documentation - GitHub Pages
A callback which is called when all iteratee functions have finished, or an error occurs. Invoked with (err). Returns: a promise, if a...
Read more >
Typing and semantics of asynchronous arrows in JavaScript
Asynchronous programs in JavaScript using callbacks and promises are difficult to write correctly. Many programs have subtle errors due to the unwanted ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found