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

Commit 0feb327

Browse files
hzoomikesherov
authored andcommitted
disallowSpaceAfterObjectKeys: implement ignoreSingleLine and ignoreMultiLine options
Fixes #663 Closes gh-1017
1 parent ea38457 commit 0feb327

2 files changed

Lines changed: 84 additions & 26 deletions

File tree

lib/rules/disallow-space-after-object-keys.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
*
44
* Type: `Boolean`
55
*
6-
* Values: `true`
6+
* Values:
7+
* - `true`
8+
* - `"ignoreSingleLine"` ignores objects if the object only takes up a single line
9+
* - `"ignoreMultiLine"` ignores objects if the object takes up multiple lines
710
*
811
* #### Example
912
*
@@ -28,22 +31,36 @@ module.exports = function() {};
2831
module.exports.prototype = {
2932

3033
configure: function(disallow) {
34+
var modes = {
35+
'ignoreSingleLine': 'ignoreSingleLine',
36+
'ignoreMultiLine': 'ignoreMultiLine'
37+
};
3138
assert(
32-
disallow === true,
33-
this.getOptionName() + ' option requires true value or should be removed'
39+
disallow === true || typeof disallow === 'string' && modes[disallow],
40+
this.getOptionName() +
41+
' option requires true value requires one of the following values: ' +
42+
Object.keys(modes).join(', ')
3443
);
44+
this._mode = disallow === true ? true : modes[disallow];
3545
},
3646

3747
getOptionName: function() {
3848
return 'disallowSpaceAfterObjectKeys';
3949
},
4050

4151
check: function(file, errors) {
52+
var mode = this._mode;
4253
file.iterateNodesByType('ObjectExpression', function(node) {
4354
node.properties.forEach(function(property) {
4455
if (property.shorthand || property.method) {
4556
return;
4657
}
58+
if (mode === 'ignoreSingleLine' && node.loc.start.line === node.loc.end.line) {
59+
return;
60+
}
61+
if (mode === 'ignoreMultiLine' && node.loc.start.line !== node.loc.end.line) {
62+
return;
63+
}
4764

4865
var token = file.getFirstNodeToken(property.key);
4966
errors.assert.noWhitespaceBetween({

test/rules/disallow-space-after-object-keys.js

Lines changed: 64 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,78 @@ describe('rules/disallow-space-after-object-keys', function() {
77
beforeEach(function() {
88
checker = new Checker();
99
checker.registerDefaultRules();
10-
checker.configure({ disallowSpaceAfterObjectKeys: true });
1110
});
1211

13-
it('should report with space(s) after keys', function() {
14-
assert(checker.checkString('var x = { a : 1, b: 2 };').getErrorCount() === 1);
15-
assert(checker.checkString('var x = { abc : 1, b : 2 };').getErrorCount() === 2);
16-
});
12+
describe('true option', function() {
13+
beforeEach(function() {
14+
checker.configure({ disallowSpaceAfterObjectKeys: true });
15+
});
1716

18-
it('should report with end of line after keys', function() {
19-
assert(checker.checkString(
20-
'var x = {' +
21-
' a\n' +
22-
' :\n' +
23-
' 2\n' +
24-
'}'
25-
).getErrorCount() === 1);
26-
});
17+
it('should report with space(s) after keys', function() {
18+
assert(checker.checkString('var x = { a : 1, b: 2 };').getErrorCount() === 1);
19+
assert(checker.checkString('var x = { abc : 1, b : 2 };').getErrorCount() === 2);
20+
});
21+
22+
it('should report with end of line after keys', function() {
23+
assert(checker.checkString(
24+
'var x = {' +
25+
' a\n' +
26+
' :\n' +
27+
' 2\n' +
28+
'}'
29+
).getErrorCount() === 1);
30+
});
2731

28-
it('should not report without space after keys', function() {
29-
assert(checker.checkString('var x = { a: 1, bcd: 2 };').isEmpty());
32+
it('should not report without space after keys', function() {
33+
assert(checker.checkString('var x = { a: 1, bcd: 2 };').isEmpty());
34+
});
35+
36+
it('should not report shorthand object properties', function() {
37+
checker.configure({ esnext: true });
38+
assert(checker.checkString('var x = { a, b };').isEmpty());
39+
assert(checker.checkString('var x = {a, b};').isEmpty());
40+
});
41+
42+
it('should report mixed shorthand and normal object propertis', function() {
43+
checker.configure({ esnext: true });
44+
assert.equal(checker.checkString('var x = { a : 1, b };').getErrorCount(), 1);
45+
});
3046
});
3147

32-
it('should not report shorthand object properties', function() {
33-
checker.configure({ esnext: true });
34-
assert(checker.checkString('var x = { a, b };').isEmpty());
35-
assert(checker.checkString('var x = {a, b};').isEmpty());
48+
describe('ignoreSingleLine option', function() {
49+
beforeEach(function() {
50+
checker.configure({ disallowSpaceAfterObjectKeys: 'ignoreSingleLine' });
51+
});
52+
53+
it('should not report with an object that takes up a single line', function() {
54+
assert(checker.checkString('var x = {a : 1, bcd : 2};').isEmpty());
55+
});
56+
57+
it('should report with an object that takes up a multi line', function() {
58+
assert(checker.checkString(
59+
'var x = {\n' +
60+
'a : 1,\n' +
61+
'};'
62+
).getErrorCount() === 1);
63+
});
3664
});
3765

38-
it('should report mixed shorthand and normal object propertis', function() {
39-
checker.configure({ esnext: true });
40-
assert.equal(checker.checkString('var x = { a : 1, b };').getErrorCount(), 1);
66+
describe('ignoreMultiLine option', function() {
67+
beforeEach(function() {
68+
checker.configure({ disallowSpaceAfterObjectKeys: 'ignoreMultiLine' });
69+
});
70+
71+
it('should report with an object that takes up a single line', function() {
72+
assert(checker.checkString('var x = {a : 1, bcd : 2};').getErrorCount() === 2);
73+
});
74+
75+
it('should not report with an object that takes up a multi line', function() {
76+
assert(checker.checkString(
77+
'var x = {\n' +
78+
'a : 1,\n' +
79+
'};'
80+
).isEmpty());
81+
});
4182
});
4283

4384
it('should not report es6-methods. #1013', function() {

0 commit comments

Comments
 (0)