Skip to content

Commit

Permalink
fix: Exceptions occurring when synchronously calling forgetNodesCreat…
Browse files Browse the repository at this point in the history
…edInBlock weren't being thrown.
  • Loading branch information
dsherret committed Jan 27, 2018
1 parent 99e8585 commit 82798c1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/factories/CompilerFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,18 +390,24 @@ export class CompilerFactory {
* Forgets the nodes created in the block.
* @param block - Block of code to run.
*/
async forgetNodesCreatedInBlock(block: (remember: (...node: compiler.Node[]) => void) => (void | Promise<void>)) {
forgetNodesCreatedInBlock(block: (remember: (...node: compiler.Node[]) => void) => (void | Promise<void>)): Promise<void> {
// can't use the async keyword here because exceptions that happen when doing this synchronously need to be thrown
this.nodeCache.setForgetPoint();
let wasPromise = false;
try {
const result = block((...nodes) => {
for (const node of nodes)
this.nodeCache.rememberNode(node);
});

if (result != null && typeof result.then === "function")
await result;
if (result != null && typeof result.then === "function") {
wasPromise = true;
return result.then(() => this.nodeCache.forgetLastPoint());
}
} finally {
this.nodeCache.forgetLastPoint();
if (!wasPromise)
this.nodeCache.forgetLastPoint();
}
return Promise.resolve();
}
}
4 changes: 4 additions & 0 deletions src/tests/tsSimpleAstTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,10 @@ describe(nameof(TsSimpleAst), () => {
expect(() => remember(classDec)).to.throw(errors.InvalidOperationError);
});
});

it("should get exceptions thrown in the body", () => {
expect(() => ast.forgetNodesCreatedInBlock(() => { throw new Error(""); })).to.throw();
});
});

describe("asynchronous", async () => {
Expand Down

0 comments on commit 82798c1

Please sign in to comment.