diff --git a/parser/tsx.js b/parser/tsx.js index 2adfe711..38a12e2d 100644 --- a/parser/tsx.js +++ b/parser/tsx.js @@ -10,6 +10,7 @@ const babylon = require('@babel/parser'); const baseOptions = require('./tsOptions'); +const printers = babylon.printers.typescript; const options = Object.assign({}, baseOptions); options.plugins = ['jsx'].concat(baseOptions.plugins); @@ -23,5 +24,6 @@ module.exports = function() { parse(code) { return babylon.parse(code, options); }, + printers, }; }; diff --git a/src/__tests__/core-test.js b/src/__tests__/core-test.js index e7f794d5..ddaaed56 100644 --- a/src/__tests__/core-test.js +++ b/src/__tests__/core-test.js @@ -61,6 +61,13 @@ describe('core API', function() { }); }); + it('returns a Collection from a TypeScript interface declaration', function () { + const source = 'interface Foo { bar: string; }'; + const ast = recast.parse(source, { parser: require('@typescript-eslint/typescript-estree') }); + const interfaceDecl = ast.program.body[0]; + expect(core(interfaceDecl).constructor.name).toContain('Collection'); + }); + it('plugins are only registered once', function () { let ct = 0; diff --git a/src/collections/Node.js b/src/collections/Node.js index a1a909dd..8df3bb77 100644 --- a/src/collections/Node.js +++ b/src/collections/Node.js @@ -154,7 +154,9 @@ const mutationMethods = { return this.forEach(function(path, i) { const newNodes = (typeof insert === 'function') ? insert.call(path, path, i) : insert; - path.insertBefore.apply(path, toArray(newNodes)); + if (newNodes != null) { + path.insertBefore.apply(path, toArray(newNodes)); + } }); }, @@ -168,7 +170,9 @@ const mutationMethods = { return this.forEach(function(path, i) { const newNodes = (typeof insert === 'function') ? insert.call(path, path, i) : insert; - path.insertAfter.apply(path, toArray(newNodes)); + if (newNodes != null) { + path.insertAfter.apply(path, toArray(newNodes)); + } }); }, diff --git a/src/core.js b/src/core.js index 9f51a69d..2ad3f90c 100644 --- a/src/core.js +++ b/src/core.js @@ -17,6 +17,13 @@ const template = require('./template'); const Node = recast.types.namedTypes.Node; const NodePath = recast.types.NodePath; +const Printer = recast.types.Printer; + +// Configure printer with TypeScript support for proper handling +// of constructs like TSInterfaceDeclaration and TSPropertySignature +const printer = new Printer({ + tabWidth: 2, +}); // Register all built-in collections for (var name in collections) {