javascript recast .toSource选项被忽略

时间:2017-07-21 11:24:46

标签: javascript jscodeshift

我正在尝试转换并重新格式化此javascript代码:

if (name == "c") {b();}

使用此重新编码插件:

  return j(file.source)
    .find(j.Identifier)
    .forEach(path => {
      j(path).replaceWith(
        j.identifier(path.node.name.split('').reverse().join(''))
      );
    })
    .toSource({quote:'single'});

保存在这里https://astexplorer.net/#/gist/994b660144d9e065906dc41bc14c9c39/c3910178f527d57de5422a0ddce9e515a460182d

我想获得以下输出:

if (eman == 'c') {
  b();
}

{quote:'single'}选项被忽略,我不确定是否有一个选项可以在新行上强制缩进。 这是astexplorer的错误,重新编码还是我做错了什么?

1 个答案:

答案 0 :(得分:0)

问题是.toSource()使用recast.print()尝试保留原始格式。 prettyPrint()会尊重更多选项:

var rc = require('recast');
rc.prettyPrint(ast, {quote:'single'}).code
相关问题