Skip to content

Fix root level using statement not disposing object #1633

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

Merged
merged 1 commit into from
May 24, 2025
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
20 changes: 15 additions & 5 deletions src/transformation/pre-transformers/using-transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import { LuaLibFeature, importLuaLibFeature } from "../utils/lualib";
export function usingTransformer(context: TransformationContext): ts.TransformerFactory<ts.SourceFile> {
return ctx => sourceFile => {
function visit(node: ts.Node): ts.Node {
if (ts.isBlock(node)) {
if (ts.isBlock(node) || ts.isSourceFile(node)) {
const [hasUsings, newStatements] = transformBlockWithUsing(context, node.statements, node);
if (hasUsings) {
// Recurse visitor into updated block to find further usings
const updatedBlock = ts.factory.updateBlock(node, newStatements);
const updatedBlock = ts.isBlock(node)
? ts.factory.updateBlock(node, newStatements)
: ts.factory.updateSourceFile(node, newStatements);
const result = ts.visitEachChild(updatedBlock, visit, ctx);

// Set all the synthetic node parents to something that makes sense
Expand All @@ -29,7 +31,8 @@ export function usingTransformer(context: TransformationContext): ts.Transformer
}
return ts.visitEachChild(node, visit, ctx);
}
return ts.visitEachChild(sourceFile, visit, ctx);
const transformedSourceFile = ts.visitEachChild(sourceFile, visit, ctx);
return visit(transformedSourceFile) as ts.SourceFile;
};
}

Expand All @@ -40,7 +43,7 @@ function isUsingDeclarationList(node: ts.Node): node is ts.VariableStatement {
function transformBlockWithUsing(
context: TransformationContext,
statements: ts.NodeArray<ts.Statement> | ts.Statement[],
block: ts.Block
block: ts.Block | ts.SourceFile
): [true, ts.Statement[]] | [false] {
const newStatements: ts.Statement[] = [];

Expand Down Expand Up @@ -102,7 +105,14 @@ function transformBlockWithUsing(
call = ts.factory.createAwaitExpression(call);
}

if (ts.isBlock(block.parent) && block.parent.statements[block.parent.statements.length - 1] !== block) {
if (ts.isSourceFile(block)) {
// If block is a sourcefile, don't insert a return statement into root code
newStatements.push(ts.factory.createExpressionStatement(call));
} else if (
block.parent &&
ts.isBlock(block.parent) &&
block.parent.statements[block.parent.statements.length - 1] !== block
) {
// If this is a free-standing block in a function (not the last statement), dont return the value
newStatements.push(ts.factory.createExpressionStatement(call));
} else {
Expand Down
16 changes: 16 additions & 0 deletions test/unit/using.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,19 @@ test("works with disposable classes (#1584)", () => {
return log;
`.expectToEqual(["action", "cleanup"]);
});

// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1632
test("works on root level (#1632)", () => {
util.testModule`
export let disposed = false;

class A {
[Symbol.dispose] = function (this: A) {
disposed = true;
}
}
using a = new A();
`.expectToEqual({
disposed: true,
});
});
Loading