davidbonnet / astring

🌳 Tiny and fast JavaScript code generator from an ESTree-compliant AST.

Home Page:https://david.bonnet.cc/astring/demo/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Missing parenthesis for await assignment expression argument

mischnic opened this issue · comments

Motivation

Parenthesis need to be inserted around the await argument if it's an assignment expression

Input

Input:

var obj;
async function main() {
	return await (obj = Promise.resolve(1));
}
Script
const acorn = require("acorn");
const astring = require("astring");

const code = `var obj;
async function main() {
	return await (obj = Promise.resolve(1));
}
`;
const ast = acorn.parse(code, { ecmaVersion: 2020 });
const formattedCode = astring.generate(ast);
console.log(formattedCode);

/*

undefined:3
  return await obj = Promise.resolve(1);
         ^^^^^^^^^

SyntaxError: Invalid left-hand side in assignment

*/
eval(formattedCode);

Expected behavior

= same as input

var obj;
async function main() {
	return await (obj = Promise.resolve(1));
}

Actual behavior

var obj;
async function main() {
  return await obj = Promise.resolve(1);
}

I guess the fix would be the identical to this

astring/src/astring.js

Lines 690 to 694 in 00690ab

if (node.argument.type === 'ArrowFunctionExpression') {
state.write('(')
this[node.argument.type](node.argument, state)
state.write(')')
} else {

@mischnic Thanks for reporting this, I'm fixing it now.

Well, actually, the yield operator has a very low precedence. Fixed in d3af2fc.