Skip to content

Commit

Permalink
Merge pull request #90 from nul/master
Browse files Browse the repository at this point in the history
Fixes issues with multiple await return.
  • Loading branch information
xoofx authored Mar 17, 2020
2 parents 2991237 + 63fe3fc commit 926d4bb
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 19 deletions.
6 changes: 6 additions & 0 deletions src/NUglify.Tests/JavaScript/ES2017.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,11 @@ public void AsyncArrowFunction()
{
TestHelper.Instance.RunTest();
}

[Test]
public void MultipleAwaitExpression()
{
TestHelper.Instance.RunTest();
}
}
}
6 changes: 6 additions & 0 deletions src/NUglify.Tests/NUglify.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,9 @@
<Content Include="TestData\JS\Expected\ES2017\AsyncFunctionExpressions.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestData\JS\Expected\ES2017\MultipleAwaitExpression.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestData\JS\Expected\ES2017\TrailingComma.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
Expand Down Expand Up @@ -3014,6 +3017,9 @@
<Content Include="TestData\JS\Input\ES2017\AsyncArrowFunction.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestData\JS\Input\ES2017\MultipleAwaitExpression.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestData\JS\Input\ES2017\AsyncClassFunction.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
async function getNumberAfterTimeout(value) {
return new Promise((resolve) => {
window.setTimeout(() => resolve(value), 2000);
});
}

async function sum(x) {
var a = getNumberAfterTimeout(20);
var b = getNumberAfterTimeout(30);
return x + await a + await b;
}

(async () => { return x + await a(t) + await b; })()

// keep grouping
(async () => { return x + (await a(t) + await b); })()
29 changes: 10 additions & 19 deletions src/NUglify/JavaScript/JSParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4344,30 +4344,21 @@ private AstNode ParseAwaitExpression()
{
ParsedVersion = ScriptVersion.EcmaScript6;

// save the context of the yield operator, then skip past it
// save the context of the await operator, then skip past it
var context = m_currentToken.Clone();
var operatorContext = context.Clone();
GetNextToken();

// must be followed by an expression
var expression = ParseExpression(true);
if (expression == null)
{
// we only call this method if we KNOW we are ES6, so if there is no expression,
// then throw an error.
ReportError(JSError.ExpressionExpected);
}
else
{
context.UpdateWith(expression.Context);
}
bool bAssign;
AstNode expression = ParseUnaryExpression(out bAssign, false);
expression = new UnaryExpression(expression.Context.CombineWith(operatorContext))
{
OperatorContext = operatorContext,
OperatorToken = JSToken.Await,
Operand = expression
};

return new UnaryExpression(context)
{
OperatorContext = operatorContext,
OperatorToken = JSToken.Await,
Operand = expression
};
return expression;
}

private FunctionObject ParseArrowFunction(AstNode parameters)
Expand Down

0 comments on commit 926d4bb

Please sign in to comment.