Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions parser/tsx.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -23,5 +24,6 @@ module.exports = function() {
parse(code) {
return babylon.parse(code, options);
},
printers,
};
};
7 changes: 7 additions & 0 deletions src/__tests__/core-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ describe('core API', function() {
});
});

it('returns a Collection from a TypeScript interface declaration', function () {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this have to do with the issue in the PR title?

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;

Expand Down
8 changes: 6 additions & 2 deletions src/collections/Node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
});
},

Expand All @@ -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));
}
});
},

Expand Down
7 changes: 7 additions & 0 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable isn't used anywhere

tabWidth: 2,
});

// Register all built-in collections
for (var name in collections) {
Expand Down