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

Commit e5059c3

Browse files
bjdixonmarkelog
authored andcommitted
Fix: Sparse arrays in rules with spacing and commas
For `disallowCommaBeforeLineBreak` and `requireCommaBeforeLineBreak` Fixes #1909 Closes gh-1969
1 parent 0ed74ea commit e5059c3

4 files changed

Lines changed: 18 additions & 3 deletions

File tree

lib/rules/disallow-comma-before-line-break.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,15 @@ module.exports.prototype = {
104104
}
105105

106106
file.iterateTokensByTypeAndValue('Punctuator', ',', function(token) {
107-
if (canSkip(token)) {
107+
var nextToken = file.getNextToken(token);
108+
109+
if (canSkip(token) || nextToken.value === ',') {
108110
return;
109111
}
110112

111113
errors.assert.sameLine({
112114
token: token,
113-
nextToken: file.getNextToken(token),
115+
nextToken: nextToken,
114116
message: 'Commas should be placed on the same line as value'
115117
});
116118

lib/rules/require-comma-before-line-break.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,13 @@ module.exports.prototype = {
5252

5353
check: function(file, errors) {
5454
file.iterateTokensByTypeAndValue('Punctuator', ',', function(token) {
55+
var prevToken = file.getPrevToken(token);
56+
57+
if (prevToken.value === ',') {
58+
return;
59+
}
5560
errors.assert.sameLine({
56-
token: file.getPrevToken(token),
61+
token: prevToken,
5762
nextToken: token,
5863
message: 'Commas should not be placed on new line'
5964
});

test/specs/rules/disallow-comma-before-line-break.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ describe('rules/disallow-comma-before-line-break', function() {
4444
expect(checker.checkString('var a = [1\n,2];')).to.have.no.errors();
4545
});
4646

47+
it('should not report legal comma placement in multiline sparse array declaration', function() {
48+
expect(checker.checkString('var a = [1\n,\n,\n,2];')).to.have.no.errors();
49+
});
50+
4751
it('should not report legal comma placement in multiline object declaration', function() {
4852
expect(checker.checkString('var a = {a:1\n,c:3};')).to.have.no.errors();
4953
});

test/specs/rules/require-comma-before-line-break.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ describe('rules/require-comma-before-line-break', function() {
3232
expect(checker.checkString('var a = [1,\n2];')).to.have.no.errors();
3333
});
3434

35+
it('should not report legal comma placement in multiline sparse array declaration', function() {
36+
expect(checker.checkString('var a = [1,\n,\n,\n2];')).to.have.no.errors();
37+
});
38+
3539
it('should not report legal comma placement in multiline object declaration', function() {
3640
expect(checker.checkString('var a = {a:1,\nc:3};')).to.have.no.errors();
3741
});

0 commit comments

Comments
 (0)