Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/transformation/visitors/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ export function transformFunctionBody(
): [lua.Statement[], Scope] {
const scope = pushScope(context, ScopeType.Function);
scope.node = node;
const bodyStatements = transformFunctionBodyContent(context, body);
let bodyStatements = transformFunctionBodyContent(context, body);
if (node && isAsyncFunction(node)) {
bodyStatements = wrapInAsyncAwaiter(context, bodyStatements);
}
const headerStatements = transformFunctionBodyHeader(context, scope, parameters, spreadIdentifier);
popScope(context);
return [[...headerStatements, ...bodyStatements], scope];
Expand Down Expand Up @@ -197,10 +200,8 @@ export function transformFunctionToExpression(
node
);

const possiblyAsyncBody = isAsyncFunction(node) ? wrapInAsyncAwaiter(context, transformedBody) : transformedBody;

const functionExpression = lua.createFunctionExpression(
lua.createBlock(possiblyAsyncBody),
lua.createBlock(transformedBody),
paramNames,
dotsLiteral,
flags,
Expand Down
5 changes: 5 additions & 0 deletions src/transformation/visitors/spread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ export function isOptimizedVarArgSpread(context: TransformationContext, symbol:
return false;
}

// Scope cannot be an async function
if (scope.node.modifiers?.some(m => m.kind === ts.SyntaxKind.AsyncKeyword)) {
return false;
}

// Identifier must be a vararg in the local function scope's parameters
const isSpreadParameter = (p: ts.ParameterDeclaration) =>
p.dotDotDotToken && ts.isIdentifier(p.name) && context.checker.getSymbolAtLocation(p.name) === symbol;
Expand Down
36 changes: 36 additions & 0 deletions test/unit/builtins/async-await.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,3 +333,39 @@ test.each([
.setOptions({ module: ModuleKind.ESNext, target: ScriptTarget.ES2017 })
.expectToHaveDiagnostics([awaitMustBeInAsyncFunction.code]);
});

test("async function can access varargs", () => {
util.testFunction`
const { promise, resolve } = defer<string>();

async function a(...args: string[]) {
log(await promise);
log(args[1]);
}

const awaitingPromise = a("A", "B", "C");
resolve("resolved");

return allLogs;
`
.setTsHeader(promiseTestLib)
.expectToEqual(["resolved", "B"]);
});

test("async function can forward varargs", () => {
util.testFunction`
const { promise, resolve } = defer<string>();

async function a(...args: string[]) {
log(await promise);
log(...args);
}

const awaitingPromise = a("A", "B", "C");
resolve("resolved");

return allLogs;
`
.setTsHeader(promiseTestLib)
.expectToEqual(["resolved", "A", "B", "C"]);
});