Skip to content
This repository was archived by the owner on Feb 8, 2020. It is now read-only.
This repository was archived by the owner on Feb 8, 2020. It is now read-only.

Strange parser error when an if statement is at the top of an async arrow function #61

Closed
@TooTallNate

Description

@TooTallNate

Apologies if this is not a bug in parse-function itself, but I've tried this with all 3 recommended AST parsers and they all exhibit the same problem.

A parser error is thrown when an async arrow function has an if statement at the top. Ex:

$ cat t.js
const parser = require('parse-function')()

const fn = async (req, res) => {
  if (req) {}
}

console.log(parser.parse(fn));

$ node t
/test/node_modules/parse-function/node_modules/babylon/lib/index.js:812
    throw err;
    ^

SyntaxError: Unexpected token, expected { (1:19)

Strangely, the same error is thrown even if the if statement is commented out!

$ cat t.js
const parser = require('parse-function')()

const fn = async (req, res) => {
  //if (req) {}
}

console.log(parser.parse(fn));

$ node t
/test/node_modules/parse-function/node_modules/babylon/lib/index.js:812
    throw err;
    ^

SyntaxError: Unexpected token, expected { (1:19)

Now, it works if I remove the if statement:

$ cat t.js
const parser = require('parse-function')()

const fn = async (req, res) => {
}

console.log(parser.parse(fn));

$ node t
{ name: null,
  body: '\n',
  args: [ 'req', 'res' ],
  params: 'req, res' }

It also works if I change it to a regular async function instead of an arrow function:

$ cat t.js
const parser = require('parse-function')()

const fn = async function (req, res) {
  if (req) {}
}

console.log(parser.parse(fn));

$ node t
{ name: null,
  body: '\n  if (req) {}\n',
  args: [ 'req', 'res' ],
  params: 'req, res' }

So, strange behavior to say the least. Or maybe I'm doing something dumb. V8 at least has no problem parsing any of these versions. Thanks in advance for any support!

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions