Error generating code with escodegen after node removal

1k views Asked by At

First I created an esprima AST, then I want to remove a node using estraverse and finally regenerate the code with escodegen. But I get an error.

The code I'm trying is:

var esprima = require('esprima');
var estraverse = require('estraverse');
var escodegen = require('escodegen');

(function () {
//build an ast with 2 lines of code
var ast = esprima.parse("console.log('1');\n console.log('2');")
console.log("original code:\n" + escodegen.generate(ast));
console.log();

//change one of the lines, works
ast = estraverse.replace(ast, {
  enter: function (node) {
  },
  leave: function (node) {
    if (node.type === esprima.Syntax.CallExpression) {
      this.break();
      return esprima.parse("console.log('patch');").body[0].expression;
    }
  }
});
console.log("patched code:\n" + escodegen.generate(ast));
console.log();

//remove one of the lines, error
ast = estraverse.replace(ast, {
  enter: function (node) {
  },
  leave: function (node) {
    if (node.type === esprima.Syntax.CallExpression) {
      this.break();
      return this.remove();
    }
  }
});
console.log("removed node:\n" + escodegen.generate(ast));
})()

The error trace is:

C:\temp\node_modules\escodegen\escodegen.js:2450
        type = expr.type || Syntax.Property;
                  ^

TypeError: Cannot read property 'type' of null
    at CodeGenerator.generateExpression (C:\temp\node_modules\escodegen\escodegen.js:2450:20)
    at CodeGenerator.ExpressionStatement (C:\temp\node_modules\escodegen\escodegen.js:1335:28)
    at CodeGenerator.generateStatement (C:\temp\node_modules\escodegen\escodegen.js:2469:33)
    at CodeGenerator.Program (C:\temp\node_modules\escodegen\escodegen.js:1717:43)
    at CodeGenerator.generateStatement (C:\temp\node_modules\escodegen\escodegen.js:2469:33)
    at generateInternal (C:\temp\node_modules\escodegen\escodegen.js:2490:28)
    at Object.generate (C:\temp\node_modules\escodegen\escodegen.js:2558:18)
    at C:\temp\bug1.js:35:45
    at Object.<anonymous> (C:\temp\bug1.js:38:3)
    at Module._compile (module.js:570:32)

Am I doing something wrong? Is this an error in escodegen or maybe in estraverse?

Thanks in advance.

2

There are 2 answers

1
fonkap On BEST ANSWER

I put an issue on github and I got an answer, I was making an invalid AST.

Deleting the CallExpression was leaving his parent ExpressionStatement empty and therefore invalid. The solution is simply deleting the ExpressionStatement.

This code works as expected:

var esprima = require('esprima');
var estraverse = require('estraverse');
var escodegen = require('escodegen');

(function () {
  //build an ast with 2 lines of code
  var ast = esprima.parse("console.log('1');\n console.log('2');")
  console.log("original code:\n" + escodegen.generate(ast));
  console.log();

  //remove one of the lines, works!
  var done = false;
  ast = estraverse.replace(ast, {
    enter: function (node) {
      if (done)
        return this.break();
      if (node.type === esprima.Syntax.ExpressionStatement) {
        done = true;
        this.remove();
      }
    },
    leave: function (node) {
      if (done)
        return this.break();
    }
  });
  console.log("removed node:\n" + escodegen.generate(ast));
})()

The output:

original code:
console.log('1');
console.log('2');

removed node:
console.log('2');
0
Kevin Reilly On

It appears as though one reason this can occur is when code is removed that leaves an empty arrow function body. For example, the original code:

() => console.log(1);

Resulting in:

() =>

With one solution being:

() => { console.log(1); }

Arguably the parent should be removed too in this instance, just might be a little tricky if it was in practice something like

useEffect(() => console.log(1))