From 0b0678685efa41f92078ac28bad64e141957ad5b Mon Sep 17 00:00:00 2001 From: Simon Boudrias Date: Sat, 11 Feb 2017 12:16:48 +0800 Subject: [PATCH] Improve option name validation message - Also bump eslint to allow es6 --- .eslintrc | 3 +++ lib/index.js | 14 +++++++++----- package.json | 4 ++-- test/base.js | 2 +- 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/.eslintrc b/.eslintrc index ba8c85cb..edb52720 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,5 +1,8 @@ { "extends": "eslint:recommended", + "parserOptions": { + "ecmaVersion": 6 + }, "env": { "node": true, "mocha": true diff --git a/lib/index.js b/lib/index.js index 3c44f45f..7aa4217d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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) { diff --git a/package.json b/package.json index 3c6e8192..8270385e 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/base.js b/test/base.js index 5035fffe..d6ef5f6e 100644 --- a/test/base.js +++ b/test/base.js @@ -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\}\)/); }); });