Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix null object conversion error in AST node #380

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Fix null object conversion error in AST node #380

wants to merge 3 commits into from

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