From 67b90f401871fa288aa406652c36e21096d48596 Mon Sep 17 00:00:00 2001 From: milindl Date: Wed, 8 Feb 2017 19:36:52 +0530 Subject: [PATCH] Add failing test for Boolean options starting with no- --- lib/index.js | 8 ++++++++ test/base.js | 12 ++++++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/index.js b/lib/index.js index 522c71ef..3c44f45f 100644 --- a/lib/index.js +++ b/lib/index.js @@ -209,6 +209,14 @@ Base.prototype.option = function option(name, config) { hide: false }); + // Check whether boolean option is invalid (starts with no-) + if (config.type === Boolean && name.match(/^no-.+/)) { + return this.emit('error', new Error('Option name ' + chalk.bold(name) + + ' cannot start with no-\nTo allow user to pass `--no-' + + name + '` use `this.option({' + + name + '}, {type: Boolean})` instead.')); + } + if (this._options[name] == null) { this._options[name] = config; } diff --git a/test/base.js b/test/base.js index 1a922841..5035fffe 100644 --- a/test/base.js +++ b/test/base.js @@ -701,6 +701,18 @@ describe('Base', function () { assert.equal(gen.options.undef, undefined); }); + + it('disallows Boolean options starting with no-', function () { + var generator = new this.Dummy([], { + env: this.env, + resolved: 'test' + }); + var addWrongOp = function() { + generator.option('no-op'); + }; + assert.throws(addWrongOp, Error); + }); + }); describe('#parseOptions()', function () {