Skip to content
This repository has been archived by the owner on Mar 23, 2024. It is now read-only.

Commit

Permalink
requireSpaceBeforeKeywords: added a new rule option
Browse files Browse the repository at this point in the history
Ignore `function` keyword by default

Fixes #2041
Closes gh-2063
  • Loading branch information
gpiress authored and markelog committed Feb 1, 2016
1 parent 17daa29 commit c9fab40
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
22 changes: 16 additions & 6 deletions lib/rules/require-space-before-keywords.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
/**
* Requires space before keyword.
*
* Types: `Array` or `Boolean`
* Types: `Array`, `Boolean` or `Object`
*
* Values: Array of quoted keywords or `true` to require all possible keywords to have a preceding space.
* Values: `true` to require all possible keywords to have a preceding space (except `function`),
* Array of quoted keywords
* or an Object with the `allExcept` property set with an Array of quoted keywords.
*
* #### Example
*
Expand Down Expand Up @@ -35,20 +37,28 @@
var assert = require('assert');

var defaultKeywords = require('../utils').spacedKeywords;
var ignoredKeywords = ['function'];

module.exports = function() {};

module.exports.prototype = {

configure: function(keywords) {
var isValidObject = (keywords === Object(keywords) && keywords.hasOwnProperty('allExcept'));

assert(
Array.isArray(keywords) || keywords === true,
this.getOptionName() + ' option requires array or true value');
Array.isArray(keywords) || keywords === true || isValidObject,
this.getOptionName() + ' option requires array, object with "allExcept" property or true value');

if (keywords === true) {
keywords = defaultKeywords;
var excludedKeywords = ignoredKeywords;
if (isValidObject) {
excludedKeywords = keywords.allExcept;
}

keywords = defaultKeywords.filter(function(keyword) {
return (excludedKeywords.indexOf(keyword) === -1);
});

this._keywords = keywords;
},

Expand Down
42 changes: 42 additions & 0 deletions test/specs/rules/require-space-before-keywords.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ describe('rules/require-space-before-keywords', function() {
expect(checker.checkString('if (true) x++;else x--;')).to.have.no.errors();
});

it('should not report missing space on function by default', function() {
checker.configure({ requireSpaceBeforeKeywords: true });

expect(checker.checkString(
'[].forEach(function (arg) {\n' +
'console.log(arg);\n' +
'});'
)).to.have.no.errors();
});

it('should report on all possible ES3 keywords if a value of true is supplied', function() {
checker.configure({ requireSpaceBeforeKeywords: true });

Expand All @@ -79,4 +89,36 @@ describe('rules/require-space-before-keywords', function() {
expect(errors).to.have.one.validation.error.from('requireSpaceBeforeKeywords');
expect(errors.explainError(error)).to.have.string('Missing space before "catch" keyword');
});

it('should not report missing space on excluded keyword', function() {
checker.configure({ requireSpaceBeforeKeywords: { allExcept: ['else'] } });

expect(checker.checkString(
'if (true) {\n}else { x++; }'
)).to.have.no.errors();
});

it('should report missing space in not excluded keywords', function() {
checker.configure({ requireSpaceBeforeKeywords: { allExcept: ['function'] } });

var errors = checker.checkString('if (true) {\n}else { x++; }');
var error = errors.getErrorList()[0];

expect(errors).to.have.one.validation.error.from('requireSpaceBeforeKeywords');
expect(errors.explainError(error)).to.have.string('Missing space before "else" keyword');
});

it('should report missing space on function when not specified in allExcept array', function() {
checker.configure({ requireSpaceBeforeKeywords: { allExcept: ['else'] } });

var errors = checker.checkString(
'[].forEach(function (arg) {\n' +
'console.log(arg);\n' +
'});'
);
var error = errors.getErrorList()[0];

expect(errors).to.have.one.validation.error.from('requireSpaceBeforeKeywords');
expect(errors.explainError(error)).to.have.string('Missing space before "function" keyword');
});
});

0 comments on commit c9fab40

Please sign in to comment.