Skip to content

Commit

Permalink
Improve option name validation message
Browse files Browse the repository at this point in the history
- Also bump eslint to allow es6
  • Loading branch information
SBoudrias committed Feb 11, 2017
1 parent 67b90f4 commit 0b06786
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": 6
},
"env": {
"node": true,
"mocha": true
Expand Down
14 changes: 9 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,15 @@ Base.prototype.option = function option(name, config) {
});

// 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.'));
var boolOptionRegex = /^no-/;
if (config.type === Boolean && name.match(boolOptionRegex)) {
let simpleName = name.replace(boolOptionRegex, '');
return this.emit('error', new Error([
`Option name ${chalk.yellow(name)} cannot start with ${chalk.red('no-')}\n`,
`Option name prefixed by ${chalk.yellow('--no')} are parsed as implicit`,
` boolean. To use ${chalk.yellow('--' + name)} as an option, use\n`,
chalk.cyan(` this.option('${simpleName}', {type: Boolean})`)
].join('')));
}

if (this._options[name] == null) {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@
"devDependencies": {
"gulp": "^3.6.0",
"gulp-coveralls": "^0.1.0",
"gulp-eslint": "^2.0.0",
"gulp-eslint": "^3.0.1",
"gulp-exclude-gitignore": "^1.0.0",
"gulp-istanbul": "^1.0.0",
"gulp-mocha": "^2.0.0",
"gulp-mocha": "^3.0.1",
"gulp-nsp": "^2.1.0",
"gulp-plumber": "^1.0.0",
"inquirer": "^1.0.2",
Expand Down
2 changes: 1 addition & 1 deletion test/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ describe('Base', function () {
var addWrongOp = function() {
generator.option('no-op');
};
assert.throws(addWrongOp, Error);
assert.throws(addWrongOp, /this\.option\('op', \{type: Boolean\}\)/);
});

});
Expand Down

0 comments on commit 0b06786

Please sign in to comment.