File tree Expand file tree Collapse file tree 3 files changed +35
-2
lines changed
Expand file tree Collapse file tree 3 files changed +35
-2
lines changed Original file line number Diff line number Diff line change @@ -274,7 +274,7 @@ Napi::Value Parser::SetLogger(const Napi::CallbackInfo &info) {
274274
275275Napi::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 ()) {
Original file line number Diff line number Diff line change @@ -231,7 +231,16 @@ Napi::Value Tree::GetEditedRange(const Napi::CallbackInfo &info) {
231231}
232232
233233Napi::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
Original file line number Diff line number Diff 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 ( / e r r o r - c o s t : ( \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
567591function assertCursorState ( cursor , params ) {
You can’t perform that action at this time.
0 commit comments