Skip to content

Commit

Permalink
Add util to verify constructor calls use 'new'
Browse files Browse the repository at this point in the history
  • Loading branch information
stewart committed May 13, 2015
1 parent 8fe7a50 commit 3f4140b
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/adaptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ function formatErrorMessage(name, message) {
//
// Returns a new Adaptor
var Adaptor = module.exports = function Adaptor(opts) {
Adaptor.__super__.constructor.apply(this, arguments);

opts = opts || {};

this.name = opts.name;
Expand Down
1 change: 1 addition & 0 deletions lib/basestar.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var Utils = require("./utils"),
// It also extends EventEmitter, so child classes are capable of emitting events
// for other parts of the system to handle.
var Basestar = module.exports = function Basestar() {
Utils.classCallCheck(this, Basestar);
};

Utils.subclass(Basestar, EventEmitter);
Expand Down
2 changes: 2 additions & 0 deletions lib/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ function formatErrorMessage(name, message) {
//
// Returns a new Driver
var Driver = module.exports = function Driver(opts) {
Driver.__super__.constructor.apply(this, arguments);

opts = opts || {};

this.name = opts.name;
Expand Down
2 changes: 2 additions & 0 deletions lib/robot.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ var Async = require("async"),
//
// Returns a new Robot
var Robot = module.exports = function Robot(opts) {
Utils.classCallCheck(this, Robot);

opts = opts || {};

var methods = [
Expand Down
6 changes: 6 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ var Utils = module.exports = {
return base;
},

classCallCheck: function(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
},

// Public: Analogue to Ruby"s Hash#fetch method for looking up object
// properties.
//
Expand Down
18 changes: 18 additions & 0 deletions spec/lib/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,4 +251,22 @@ describe("Utils", function() {
});
});
});

describe("#classCallCheck", function() {
it("checks that an object is an instance of a constructor", function() {
var fn = function(instance, Constructor) {
return utils.classCallCheck.bind(null, instance, Constructor);
};

function Class() {
utils.classCallCheck(this, Class);
}

expect(fn([], Array)).to.not.throw;
expect(fn([], Number)).to.throw(TypeError);

expect(Class).to.throw(TypeError);
expect(function() { return new Class(); }).not.to.throw(TypeError);
});
});
});

0 comments on commit 3f4140b

Please sign in to comment.