From 641d459e30ff53a10a6467de413e02bef0320254 Mon Sep 17 00:00:00 2001 From: b1rdfree <378099757@qq.com> Date: Tue, 28 Aug 2018 20:04:26 +0800 Subject: [PATCH 1/3] Fix null object conversion error in AST node MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In escodegen.js, converting a null object into source code in AST will generate an error on toString(). This is because the original judgment considers that the null object node has a "value" key value. Previous code:escodegen.js-----Lines 2310 to 2313: if (expr.value === null) {return 'null'; } I think I can change the above code to: if (expr.raw === 'null' && expr.value===undefined) {return 'null'; } This way it can be converted from the ast tree to the original code when it encounters a null object normally. testcase code(Nodejs environment): var esprima=require("esprima"); var escodegen=require("escodegen"); var ast=esprima.parse("var a=null;"); var code=escodegen.generate(ast); --- escodegen.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/escodegen.js b/escodegen.js index 54e7cfab..5186eecf 100644 --- a/escodegen.js +++ b/escodegen.js @@ -2308,7 +2308,7 @@ } } - if (expr.value === null) { + if (expr.raw === 'null' && expr.value===undefined) { return 'null'; } From 5d2df844fcd2085cd622a7251ca63abf923d138c Mon Sep 17 00:00:00 2001 From: b1rdfree <378099757@qq.com> Date: Tue, 28 Aug 2018 20:24:59 +0800 Subject: [PATCH 2/3] Fix null object conversion error in AST node MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In escodegen.js, converting a null object into source code in AST will generate an error on toString(). This is because the original judgment considers that the null object node has a "value" key value. Previous code:escodegen.js-----Lines 2310 to 2313: if (expr.value === null || expr.raw === 'null' && expr.value===undefined) {return 'null'; } I think I can change the above code to: if (expr.raw === 'null' && expr.value===undefined) {return 'null'; } This way it can be converted from the ast tree to the original code when it encounters a null object normally. testcase code(Nodejs environment): var esprima=require("esprima"); var escodegen=require("escodegen"); var ast=esprima.parse("var a=null;"); var code=escodegen.generate(ast); --- escodegen.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/escodegen.js b/escodegen.js index 5186eecf..e807fe46 100644 --- a/escodegen.js +++ b/escodegen.js @@ -2308,7 +2308,7 @@ } } - if (expr.raw === 'null' && expr.value===undefined) { + if (expr.value === null || expr.raw === 'null' && expr.value===undefined) { return 'null'; } From b8908b44356d640b2f84f22a5d82aaf165177d93 Mon Sep 17 00:00:00 2001 From: b1rdfree <378099757@qq.com> Date: Tue, 28 Aug 2018 21:30:25 +0800 Subject: [PATCH 3/3] Update escodegen.js --- escodegen.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/escodegen.js b/escodegen.js index e807fe46..8e67489a 100644 --- a/escodegen.js +++ b/escodegen.js @@ -2308,7 +2308,7 @@ } } - if (expr.value === null || expr.raw === 'null' && expr.value===undefined) { + if (expr.value === null || (expr.raw === 'null' && expr.value===undefined)) { return 'null'; }