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

Commit 997e0a9

Browse files
Andrey Ermakovhzoo
authored andcommitted
Fix: disallowSpaceAfterObjectKeys - Allow no space after key with align option.
Fixes #1818 Closes gh-1857
1 parent a04a3a9 commit 997e0a9

3 files changed

Lines changed: 38 additions & 2 deletions

File tree

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,26 @@ module.exports.prototype = {
163163
tokens.push(keyToken);
164164
});
165165

166+
var noSpace = true;
167+
if (exceptAligned) {
168+
var withoutSpace = 0;
169+
var alignedOnColon = 0;
170+
tokens.forEach(function(key) {
171+
var colon = file.getNextToken(key);
172+
var spaces = Math.abs(colon.range[0] - key.range[1]);
173+
if (spaces === 0) {
174+
withoutSpace++;
175+
} else if (spaces === maxKeyEndPos - key.loc.end.column) {
176+
alignedOnColon++;
177+
}
178+
});
179+
180+
noSpace = withoutSpace > alignedOnColon;
181+
}
182+
166183
tokens.forEach(function(key) {
167184
var colon = file.getNextToken(key);
168-
var spaces = exceptAligned ? maxKeyEndPos - key.loc.end.column : 0;
185+
var spaces = (exceptAligned && !noSpace) ? maxKeyEndPos - key.loc.end.column : 0;
169186
errors.assert.spacesBetween({
170187
token: key,
171188
nextToken: colon,

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,25 @@ describe('rules/disallow-space-after-object-keys', function() {
150150
'};'
151151
)).to.have.no.errors();
152152
});
153+
154+
it('should not report keys with no space after them #1818', function() {
155+
expect(checker.checkString(
156+
'var f = {\n' +
157+
' "name": 1,\n' +
158+
' "x": 2\n' +
159+
'};'
160+
)).to.have.no.errors();
161+
});
162+
163+
it('should report objects with both keys without spaces and aligned on colon #1818', function() {
164+
expect(checker.checkString(
165+
'var f = {\n' +
166+
' "n": 1,\n' +
167+
' "xasdf" : 2,\n' +
168+
' "fyfyasdf": 0\n' +
169+
'};'
170+
)).to.have.error.count.equal(1);
171+
});
153172
});
154173

155174
describe('with method value', function() {

test/specs/rules/require-aligned-object-values.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ describe('rules/require-aligned-object-values', function() {
157157
'bcd : 2\n' +
158158
'};',
159159
output: 'var x = {\n' +
160-
'a : 1,\n' +
160+
'a: 1,\n' +
161161
'foo: function() {},\n' +
162162
'bcd: 2\n' +
163163
'};'

0 commit comments

Comments
 (0)