Skip to content

Commit a2d31c9

Browse files
authored
Fix bug in await using transformer (#1579)
1 parent 920f3e5 commit a2d31c9

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/transformation/pre-transformers/using-transformer.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export function usingTransformer(context: TransformationContext): ts.Transformer
1818
ts.setParent(node2, parent[parent.length - 1]);
1919
parent.push(node2);
2020
ts.visitEachChild(node2, setParent, ctx);
21-
parent.push();
21+
parent.pop();
2222
return node2;
2323
}
2424
ts.visitEachChild(updatedBlock, setParent, ctx);
@@ -74,12 +74,13 @@ function transformBlockWithUsing(
7474
);
7575

7676
const callback = ts.factory.createFunctionExpression(
77-
undefined,
77+
// Put async keyword in front of callback when we are in an async using
78+
isAwaitUsing ? [ts.factory.createModifier(ts.SyntaxKind.AsyncKeyword)] : undefined,
7879
undefined,
7980
undefined,
8081
undefined,
8182
variableNames,
82-
undefined,
83+
ts.factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword), // Required for TS to not freak out trying to infer the type of synthetic nodes
8384
callbackBody
8485
);
8586

test/unit/using.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,21 @@ test("await using can handle non-async disposables", () => {
175175
.setTsHeader(usingTestLib)
176176
.expectToEqual({ logs: ["Creating a", "function content", "Disposing a"] });
177177
});
178+
179+
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1571
180+
test("await using no extra diagnostics (#1571)", () => {
181+
util.testModule`
182+
async function getResource(): Promise<AsyncDisposable> {
183+
return {
184+
[Symbol.asyncDispose]: async () => {}
185+
};
186+
}
187+
188+
async function someOtherAsync() {}
189+
190+
async function main() {
191+
await using resource = await getResource();
192+
await someOtherAsync();
193+
}
194+
`.expectToHaveNoDiagnostics();
195+
});

0 commit comments

Comments
 (0)