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

Commit c9fab40

Browse files
gpiressmarkelog
authored andcommitted
requireSpaceBeforeKeywords: added a new rule option
Ignore `function` keyword by default Fixes #2041 Closes gh-2063
1 parent 17daa29 commit c9fab40

File tree

2 files changed

+58
-6
lines changed

2 files changed

+58
-6
lines changed

lib/rules/require-space-before-keywords.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
/**
22
* Requires space before keyword.
33
*
4-
* Types: `Array` or `Boolean`
4+
* Types: `Array`, `Boolean` or `Object`
55
*
6-
* Values: Array of quoted keywords or `true` to require all possible keywords to have a preceding space.
6+
* Values: `true` to require all possible keywords to have a preceding space (except `function`),
7+
* Array of quoted keywords
8+
* or an Object with the `allExcept` property set with an Array of quoted keywords.
79
*
810
* #### Example
911
*
@@ -35,20 +37,28 @@
3537
var assert = require('assert');
3638

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

3942
module.exports = function() {};
4043

4144
module.exports.prototype = {
4245

4346
configure: function(keywords) {
47+
var isValidObject = (keywords === Object(keywords) && keywords.hasOwnProperty('allExcept'));
48+
4449
assert(
45-
Array.isArray(keywords) || keywords === true,
46-
this.getOptionName() + ' option requires array or true value');
50+
Array.isArray(keywords) || keywords === true || isValidObject,
51+
this.getOptionName() + ' option requires array, object with "allExcept" property or true value');
4752

48-
if (keywords === true) {
49-
keywords = defaultKeywords;
53+
var excludedKeywords = ignoredKeywords;
54+
if (isValidObject) {
55+
excludedKeywords = keywords.allExcept;
5056
}
5157

58+
keywords = defaultKeywords.filter(function(keyword) {
59+
return (excludedKeywords.indexOf(keyword) === -1);
60+
});
61+
5262
this._keywords = keywords;
5363
},
5464

test/specs/rules/require-space-before-keywords.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ describe('rules/require-space-before-keywords', function() {
5656
expect(checker.checkString('if (true) x++;else x--;')).to.have.no.errors();
5757
});
5858

59+
it('should not report missing space on function by default', function() {
60+
checker.configure({ requireSpaceBeforeKeywords: true });
61+
62+
expect(checker.checkString(
63+
'[].forEach(function (arg) {\n' +
64+
'console.log(arg);\n' +
65+
'});'
66+
)).to.have.no.errors();
67+
});
68+
5969
it('should report on all possible ES3 keywords if a value of true is supplied', function() {
6070
checker.configure({ requireSpaceBeforeKeywords: true });
6171

@@ -79,4 +89,36 @@ describe('rules/require-space-before-keywords', function() {
7989
expect(errors).to.have.one.validation.error.from('requireSpaceBeforeKeywords');
8090
expect(errors.explainError(error)).to.have.string('Missing space before "catch" keyword');
8191
});
92+
93+
it('should not report missing space on excluded keyword', function() {
94+
checker.configure({ requireSpaceBeforeKeywords: { allExcept: ['else'] } });
95+
96+
expect(checker.checkString(
97+
'if (true) {\n}else { x++; }'
98+
)).to.have.no.errors();
99+
});
100+
101+
it('should report missing space in not excluded keywords', function() {
102+
checker.configure({ requireSpaceBeforeKeywords: { allExcept: ['function'] } });
103+
104+
var errors = checker.checkString('if (true) {\n}else { x++; }');
105+
var error = errors.getErrorList()[0];
106+
107+
expect(errors).to.have.one.validation.error.from('requireSpaceBeforeKeywords');
108+
expect(errors.explainError(error)).to.have.string('Missing space before "else" keyword');
109+
});
110+
111+
it('should report missing space on function when not specified in allExcept array', function() {
112+
checker.configure({ requireSpaceBeforeKeywords: { allExcept: ['else'] } });
113+
114+
var errors = checker.checkString(
115+
'[].forEach(function (arg) {\n' +
116+
'console.log(arg);\n' +
117+
'});'
118+
);
119+
var error = errors.getErrorList()[0];
120+
121+
expect(errors).to.have.one.validation.error.from('requireSpaceBeforeKeywords');
122+
expect(errors.explainError(error)).to.have.string('Missing space before "function" keyword');
123+
});
82124
});

0 commit comments

Comments
 (0)