Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide plugin registration method that avoids duplicate registrations. #108

Merged
merged 1 commit into from
Apr 22, 2016
Merged
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
20 changes: 20 additions & 0 deletions src/__tests__/core-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,25 @@ describe('core API', function() {
var source = '\nvar foo;\n';
expect(core(source).toSource()).toEqual(source);
});

it('plugins are called with core', function (done) {
core.use(function (j) {
expect(j).toBe(core);
done();
});
});

it('plugins are only registered once', function () {
var ct = 0;

function plugin() {
ct++;
}

core.use(plugin);
core.use(plugin);

expect(ct).toBe(1);
});

});
20 changes: 20 additions & 0 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,24 @@ for (var name in collections) {
}
}

var plugins = [];

/**
* Utility function for registering plugins.
*
* Plugins are simple functions that are passed the core jscodeshift instance.
* They should extend jscodeshift by call `registerMethods`, etc.
* This method guards against repeated registrations (the plugin callback will only be called once).
*
* @param {Function} plugin
*/
function use(plugin) {
if (plugins.indexOf(plugin) === -1) {
plugins.push(plugin);
plugin(core);
}
}

core.use = use;

module.exports = core;