Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shorthand async function interpreted as beginning of function paren. #865

Closed
lesander opened this issue Apr 20, 2017 · 6 comments

Comments

@lesander
Copy link

commented Apr 20, 2017

Hi!

Not quite sure if the issue I've described below is a 'bug' in the standard parser, or if shorthand functions are not supported at all.

Let's have a look at the following code snippet.

const bar = async () => { return true }
const foo = bar

(async () => {
  // do something async directly without creating a named function.
  await foo
})()

As you can see, foo is set to bar and after that, we invoke a directly executed async function. Now let's run standard on the snippet.

$ standard test.js
test.js:2:13: Unexpected space between function name and paren.
test.js:4:1: Unexpected newline between function and ( of function call.

We see that standard interprets the shorthand function and assignment of foo = bar as an invalid formatted function.

After running standard --fix, the shorthand function is pasted directly after the assignment of foo.

const bar = async () => { return true }
const foo = bar(async () => {
  // do something async directly without creating a named function.
  await foo
})()

My question is; is this wanted behaviour?

@LinusU

This comment has been minimized.

Copy link
Member

commented Apr 20, 2017

The code that you get after running standard --fix is the same as will be run before. I think that you want to write:

const bar = async () => { return true }
const foo = bar

;(async () => {
  // do something async directly without creating a named function.
  await foo
})()
@lesander

This comment has been minimized.

Copy link
Author

commented Apr 20, 2017

Thanks @LinusU, I noticed that I was missing a semicolon in my shorthand function.

Is it possible for StandardJS to recognise and correctly fix this mistake?

@LinusU

This comment has been minimized.

Copy link
Member

commented Apr 20, 2017

I think it's quite hard since the code you originally wrote still is valid javascript, and it would be weird if standard --fix impacted the output of the script...

@tunnckoCore

This comment has been minimized.

Copy link

commented Apr 20, 2017

Yea, it's sounds almost impossible.

But still

test.js:2:13: Unexpected space between function name and paren.

sounds strange for me and like a bug in ESLint parser. @lesander is it fixed when use semicolon?

But yea as @LinusU said, the --fix is totally correct, that's one of the "special" cases where you must add semi-colon.

@LinusU

This comment has been minimized.

Copy link
Member

commented Apr 21, 2017

I think that "Unexpected space between function name and paren." is correct, since there are whitespace between bar and (. To be precise, there are two newlines between them...

@feross

This comment has been minimized.

Copy link
Member

commented Apr 22, 2017

I believe this is working as intended.

@feross feross closed this Apr 22, 2017

@lock lock bot locked as resolved and limited conversation to collaborators May 10, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
4 participants
You can’t perform that action at this time.