Skip to content

Commit c1a64f4

Browse files
segevfineramaanq
andauthored
fix: account for user-provided file descriptors in Tree.printDotGraph
Co-authored-by: Amaan Qureshi <[email protected]>
1 parent b7bc2ad commit c1a64f4

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

src/parser.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ Napi::Value Parser::SetLogger(const Napi::CallbackInfo &info) {
274274

275275
Napi::Value Parser::PrintDotGraphs(const Napi::CallbackInfo &info) {
276276
bool should_print = true;
277-
int32_t fd = fileno(stderr);
277+
int fd = fileno(stderr);
278278

279279
if (info.Length() > 0) {
280280
if (!info[0].IsBoolean()) {

src/tree.cc

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,16 @@ Napi::Value Tree::GetEditedRange(const Napi::CallbackInfo &info) {
231231
}
232232

233233
Napi::Value Tree::PrintDotGraph(const Napi::CallbackInfo &info) {
234-
ts_tree_print_dot_graph(tree_, fileno(stderr));
234+
int fd = fileno(stderr);
235+
236+
if (info.Length() > 0) {
237+
if (!info[0].IsNumber()) {
238+
throw TypeError::New(info.Env(), "First argument must be a number");
239+
}
240+
fd = info[0].As<Number>().Int32Value();
241+
}
242+
243+
ts_tree_print_dot_graph(tree_, fd);
235244
return info.This();
236245
}
237246

test/tree_test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,30 @@ describe("Tree", () => {
562562
assert.notDeepEqual(node1.firstChild, node2);
563563
});
564564
});
565+
566+
describe(".printDotGraph()", () => {
567+
it("prints a dot graph to the output file", () => {
568+
if (process.platform === "win32") {
569+
return;
570+
}
571+
572+
const tmp = require("tmp");
573+
const debugGraphFile = tmp.fileSync({ postfix: ".dot" });
574+
const tree = parser.parse("const zero = 0");
575+
tree.printDotGraph(debugGraphFile.fd);
576+
577+
const fs = require('fs');
578+
const logReader = fs.readFileSync(debugGraphFile.name, 'utf8').split('\n');
579+
for (let line of logReader) {
580+
const match = line.match(/error-cost: (\d+)/);
581+
if (match) {
582+
assert.equal(parseInt(match[1]), 0); // error-cost should always be 0
583+
}
584+
}
585+
586+
debugGraphFile.removeCallback();
587+
});
588+
});
565589
});
566590

567591
function assertCursorState(cursor, params) {

0 commit comments

Comments
 (0)