Skip to content

Commit

Permalink
Merge pull request #480 from SBoudrias/assert
Browse files Browse the repository at this point in the history
Replace throwing if statement with assertion
  • Loading branch information
sindresorhus committed Jan 25, 2014
2 parents 7831022 + 8eb059a commit eef64d4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 31 deletions.
20 changes: 5 additions & 15 deletions lib/base.js
Expand Up @@ -3,6 +3,7 @@ var fs = require('fs');
var util = require('util');
var path = require('path');
var events = require('events');
var assert = require('assert');
var _ = require('lodash');
var async = require('async');
var findup = require('findup-sync');
Expand Down Expand Up @@ -73,13 +74,8 @@ var Base = module.exports = function Base(args, options) {
this.options = options || {};

// checks required paramaters
if (!this.options.env) {
throw new Error('You must provide the environment object. Use env#create() to create a new generator.');
}

if (!this.options.resolved) {
throw new Error('You must provide the resolved path value. Use env#create() to create a new generator.');
}
assert(this.options.env, 'You must provide the environment object. Use env#create() to create a new generator.');
assert(this.options.resolved, 'You must provide the resolved path value. Use env#create() to create a new generator.');

this.env = this.options.env;
this.resolved = this.options.resolved;
Expand Down Expand Up @@ -314,9 +310,7 @@ Base.prototype.run = function run(args, cb) {

var methods = Object.keys(Object.getPrototypeOf(this));

if (!methods.length) {
throw new Error('This Generator is empty. Add at least one method for it to be runned.');
}
assert(methods.length, 'This Generator is empty. Add at least one method for it to be runned.');

this.env.runLoop.once('end', function () {
self.emit('end');
Expand Down Expand Up @@ -454,11 +448,7 @@ Base.prototype.hookFor = function hookFor(name, config) {
config = config || {};

// enforce use of hookFor during instantiation
if (this._running) {
return this.emit('error', new Error(
'hookFor must be used within the constructor only'
));
}
assert(!this._running, 'hookFor can only be used inside the constructor function');

// add the corresponding option to this class, so that we output these hooks
// in help
Expand Down
10 changes: 3 additions & 7 deletions lib/util/conflicter.js
Expand Up @@ -3,6 +3,7 @@
var fs = require('fs');
var path = require('path');
var events = require('events');
var assert = require('assert');
var async = require('async');
var isBinaryFile = require('isbinaryfile');
var util = require('util');
Expand All @@ -23,13 +24,8 @@ Conflicter.prototype.add = function add(conflict) {
};
}

if (!conflict.file) {
throw new Error('Missing conflict.file option');
}

if (conflict.content === undefined) {
throw new Error('Missing conflict.content option');
}
assert(conflict.file, 'Missing conflict.file option');
assert(conflict.content !== undefined, 'Missing conflict.content option');

this.conflicts.push(conflict);
return this;
Expand Down
13 changes: 4 additions & 9 deletions lib/util/storage.js
@@ -1,6 +1,7 @@
'use strict';
var fs = require('fs');
var path = require('path');
var assert = require('assert');
var EventEmitter = require('events').EventEmitter;
var _ = require('lodash');
var util = require('util');
Expand All @@ -26,9 +27,7 @@ var util = require('util');
var Storage = module.exports = function Storage(name, configPath) {
EventEmitter.call(this);

if (!name) {
throw new Error('A name parameter is required to create a storage');
}
assert(name, 'A name parameter is required to create a storage');

this.path = configPath || path.join(process.cwd(), '.yo-rc.json');
this.name = name;
Expand Down Expand Up @@ -108,9 +107,7 @@ Storage.prototype.getAll = function () {
*/

Storage.prototype.set = function (key, val) {
if (_.isFunction(val)) {
throw new Error('Storage value can\'t be a function');
}
assert(!_.isFunction(val), 'Storage value can\'t be a function');

if (_.isObject(key)) {
_.extend(this._store, key);
Expand Down Expand Up @@ -139,9 +136,7 @@ Storage.prototype.delete = function (key) {
*/

Storage.prototype.defaults = function (defaults) {
if (!_.isObject(defaults)) {
throw new Error('Storage `defaults` method only accept objects');
}
assert(_.isObject(defaults), 'Storage `defaults` method only accept objects');
var val = _.defaults(this.getAll(), defaults);
this.set(val);
};

0 comments on commit eef64d4

Please sign in to comment.