Skip to content

Commit

Permalink
fix: account for user-provided file descriptors in Tree.printDotGraph
Browse files Browse the repository at this point in the history
Co-authored-by: Amaan Qureshi <[email protected]>
  • Loading branch information
segevfiner and amaanq authored Jul 5, 2024
1 parent b7bc2ad commit c1a64f4
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ Napi::Value Parser::SetLogger(const Napi::CallbackInfo &info) {

Napi::Value Parser::PrintDotGraphs(const Napi::CallbackInfo &info) {
bool should_print = true;
int32_t fd = fileno(stderr);
int fd = fileno(stderr);

if (info.Length() > 0) {
if (!info[0].IsBoolean()) {
Expand Down
11 changes: 10 additions & 1 deletion src/tree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,16 @@ Napi::Value Tree::GetEditedRange(const Napi::CallbackInfo &info) {
}

Napi::Value Tree::PrintDotGraph(const Napi::CallbackInfo &info) {
ts_tree_print_dot_graph(tree_, fileno(stderr));
int fd = fileno(stderr);

if (info.Length() > 0) {
if (!info[0].IsNumber()) {
throw TypeError::New(info.Env(), "First argument must be a number");
}
fd = info[0].As<Number>().Int32Value();
}

ts_tree_print_dot_graph(tree_, fd);
return info.This();
}

Expand Down
24 changes: 24 additions & 0 deletions test/tree_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,30 @@ describe("Tree", () => {
assert.notDeepEqual(node1.firstChild, node2);
});
});

describe(".printDotGraph()", () => {
it("prints a dot graph to the output file", () => {
if (process.platform === "win32") {
return;
}

const tmp = require("tmp");
const debugGraphFile = tmp.fileSync({ postfix: ".dot" });
const tree = parser.parse("const zero = 0");
tree.printDotGraph(debugGraphFile.fd);

const fs = require('fs');
const logReader = fs.readFileSync(debugGraphFile.name, 'utf8').split('\n');
for (let line of logReader) {
const match = line.match(/error-cost: (\d+)/);
if (match) {
assert.equal(parseInt(match[1]), 0); // error-cost should always be 0
}
}

debugGraphFile.removeCallback();
});
});
});

function assertCursorState(cursor, params) {
Expand Down

0 comments on commit c1a64f4

Please sign in to comment.