Skip to content

Conversation

@b1rdfree
Copy link

@b1rdfree b1rdfree commented Aug 28, 2018

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.value === null || (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);

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);
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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant