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

Formatting of await misses parentheses #589

Closed
skeggse opened this issue Apr 23, 2019 · 6 comments · Fixed by #712
Closed

Formatting of await misses parentheses #589

skeggse opened this issue Apr 23, 2019 · 6 comments · Fixed by #712
Assignees

Comments

@skeggse
Copy link

skeggse commented Apr 23, 2019

The following two functions differ, in that one awaits the result of a() while one dereferences b off of a's return value and then awaits that value.

async function fnA() {
  return await a().b;
}
async function fnB() {
  return (await a()).b;
}

The AST for the latter produces the former in recast:

const recast = require('recast');
const {builders} = require('ast-types');

console.log(
  recast.print(
    builders.functionDeclaration(
      builders.identifier('fnB'),
      [],
      builders.blockStatement([
        builders.returnStatement(
          builders.memberExpression(
            builders.awaitExpression(builders.callExpression(builders.identifier('a'), [])),
            builders.identifier('b')
          )
        ),
      ])
    )
  ).code
);

// produces

function fnB() {
    return await a().b;
}

Moreover, I can't find a workaround for this bug - I'd like to explicitly wrap the await expression in parentheses but don't see a way to do so. EDIT: I missed the ParenthesizedExpression type.

@kindy
Copy link

kindy commented May 7, 2019

seems escodegen works fine for await.

@benjamn
Copy link
Owner

benjamn commented May 18, 2019

This is definitely a bug, since Recast takes responsibility for inserting parentheses when necessary to preserve AST structure. There are ways to force parentheses, but that's not the recommended way of handling this. The Recast printer should just get it right.

@skeggse
Copy link
Author

skeggse commented Aug 16, 2019

I opened a possible fix for this benjamn/ast-types#349 - does that seem like the right approach?

@splitice
Copy link

splitice commented Sep 6, 2019

Just hit this bug as well.

The obvious workaround for those who find this is to throw a function around the await as function calls are correctly handled with awaits.

@jedwards1211
Copy link
Contributor

jedwards1211 commented Apr 16, 2020

I'm seeing a similar issue when wrapping a ConditionalExpression in an AwaitExpression. Like OP I was able to workaround by using ParenthesizedExpression.

@conartist6
Copy link
Contributor

conartist6 commented May 25, 2020

I've published a PR which should fix these and other issues with parens and also expands relevant test coverage.

@skeggse I believe it improves on your PR by eliminating parens in places where they are irrelevant, e.g. new Class(await args), which would otherwise print as new Class((await args)).

conartist6 added a commit to conartist6/recast that referenced this issue May 26, 2020
benjamn pushed a commit to conartist6/recast that referenced this issue Aug 23, 2020
benjamn pushed a commit to conartist6/recast that referenced this issue Aug 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
6 participants