facebook / jscodeshift

A JavaScript codemod toolkit.
https://jscodeshift.com
MIT License
9.24k stars 477 forks source link

template.statements breaks TypeScript namespaces #327

Open thorn0 opened 5 years ago

thorn0 commented 5 years ago

AST Explorer

Input

interface Foo { foo: string; }
interface Bar { bar: string; }

Transformation

export default function transformer(file, api) {
  const j = api.jscodeshift.withParser('ts');

  return j(file.source)
    .find('Program')
    .replaceWith( p => j.template.statement`namespace Z { ${p.node.body} }`)
    .toSource();
}

Output

namespace Z interface Foo { foo: string; }
interface Bar { bar: string; }

Expected

namespace Z {
  interface Foo { foo: string; }
  interface Bar { bar: string; }
}
fkling commented 5 years ago

Mmh. Have to look into whether there is an issue inserting arrays into placeholders or whether it's in upstream printing issue in recast.

thorn0 commented 5 years ago

If I delete end of the created TSModuleDeclaration, then it works as expected.