Skip to content

Commit

Permalink
Fix undefined boolean options - Fix #988 (#989)
Browse files Browse the repository at this point in the history
  • Loading branch information
SBoudrias authored Dec 20, 2016
1 parent e841f35 commit 244c92c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
32 changes: 24 additions & 8 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ var Storage = require('./util/storage');
var promptSuggestion = require('./util/prompt-suggestion');
var debug = require('debug')('yeoman:generator');

var EMPTY = '@@_YEOMAN_EMPTY_MARKER_@@';

/**
* The `Base` class provides the common API shared by all generators.
* It define options, arguments, file, prompt, log, API, etc.
Expand Down Expand Up @@ -65,13 +67,15 @@ var Base = module.exports = function Base(args, options) {
}

this.options = options || {};
this._initOptions = _.clone(options);
this._args = args || [];
this._options = {};
this._arguments = [];
this._composedWith = [];
this._transformStreams = [];

this.option('help', {
type: Boolean,
alias: 'h',
description: 'Print the generator\'s options and usage'
});
Expand Down Expand Up @@ -193,7 +197,9 @@ Base.prototype.option = function option(name, config) {
config = config || {};

// alias default to defaults for backward compatibility.
config.default = config.default != null ? config.default : config.defaults;
if ('defaults' in config) {
config.default = config.defaults;
}
config.description = config.description || config.desc;

_.defaults(config, {
Expand Down Expand Up @@ -237,7 +243,9 @@ Base.prototype.argument = function argument(name, config) {
config = config || {};

// alias default to defaults for backward compatibility.
config.default = config.default != null ? config.default : config.defaults;
if ('defaults' in config) {
config.default = config.defaults;
}
config.description = config.description || config.desc;

_.defaults(config, {
Expand All @@ -263,6 +271,9 @@ Base.prototype.parseOptions = function () {
_.each(this._options, function (option) {
if (option.type === Boolean) {
minimistDef.boolean.push(option.name);
if (!('default' in option) && !option.required) {
minimistDef.default[option.name] = EMPTY;
}
} else {
minimistDef.string.push(option.name);
}
Expand All @@ -273,10 +284,10 @@ Base.prototype.parseOptions = function () {

// Only apply default values if we don't already have a value injected from
// the runner
if (option.name in this.options) {
minimistDef.default[option.name] = this.options[option.name];
} else if (option.alias && option.alias in this.options) {
minimistDef.default[option.alias] = this.options[option.alias];
if (option.name in this._initOptions) {
minimistDef.default[option.name] = this._initOptions[option.name];
} else if (option.alias && option.alias in this._initOptions) {
minimistDef.default[option.name] = this._initOptions[option.alias];
} else if ('default' in option) {
minimistDef.default[option.name] = option.default;
}
Expand All @@ -286,6 +297,11 @@ Base.prototype.parseOptions = function () {

// Parse options to the desired type
_.each(parsedOpts, function (option, name) {
// Manually set value as undefined if it should be.
if (option === EMPTY) {
parsedOpts[name] = undefined;
return;
}
if (this._options[name] && option !== undefined) {
parsedOpts[name] = this._options[name].type(option);
}
Expand All @@ -295,8 +311,8 @@ Base.prototype.parseOptions = function () {
this._arguments.forEach(function (config, index) {
var value;
if (index >= parsedOpts._.length) {
if (config.name in this.options) {
value = this.options[config.name];
if (config.name in this._initOptions) {
value = this._initOptions[config.name];
} else if ('default' in config) {
value = config.default;
} else {
Expand Down
23 changes: 19 additions & 4 deletions test/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -625,11 +625,10 @@ describe('Base', function () {
it('doesn\'t raise an error if required arguments are not provided, but the help option has been specified', function () {
var dummy = new Base([], {
env: this.env,
resolved: 'dummy:all'
resolved: 'dummy:all',
help: true
});

dummy.options.help = true;

assert.equal(dummy._arguments.length, 0);
assert.doesNotThrow(dummy.argument.bind(dummy, 'foo', { required: true }));
assert.equal(dummy._arguments.length, 1);
Expand Down Expand Up @@ -661,7 +660,6 @@ describe('Base', function () {
description: 'Description for foo',
name: 'foo',
type: Boolean,
default: undefined,
hide: false
});
});
Expand All @@ -686,6 +684,23 @@ describe('Base', function () {

assert.equal(gen.options['long-name'], 'that value');
});

it('allows Boolean options to be undefined', function () {
var Generator = Base.extend({
constructor: function () {
Base.apply(this, arguments);

this.option('undef', {type: Boolean});
}
});

var gen = new Generator({
env: this.env,
resolved: 'test'
});

assert.equal(gen.options.undef, undefined);
});
});

describe('#parseOptions()', function () {
Expand Down

0 comments on commit 244c92c

Please sign in to comment.