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

Commit cd8ce2e

Browse files
dreef3mrjoelkemp
authored andcommitted
disallowCommaBeforeLineBreak: added allExcept function
Added supoort to ignore object expressions with function properties and to place a comma on the new line when fixing. "lineBreak" option changed into default behavior for multiline expressions Closes gh-1410
1 parent 5d8efd0 commit cd8ce2e

File tree

2 files changed

+87
-10
lines changed

2 files changed

+87
-10
lines changed

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

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
/**
22
* Disallows commas as last token on a line in lists.
33
*
4-
* Type: `Boolean`
4+
* Type: `Boolean`|`Object`
55
*
6-
* Value: `true`
6+
* Values:
7+
* - `true` for default behavior (strict mode, comma on the same line)
8+
* - `Object`:
9+
* - `'allExcept'` array of exceptions:
10+
* - `'function'` ignores objects if one of their values is a function expression
711
*
812
* JSHint: [`laxcomma`](http://www.jshint.com/docs/options/#laxcomma)
913
*
@@ -13,7 +17,7 @@
1317
* "disallowCommaBeforeLineBreak": true
1418
* ```
1519
*
16-
* ##### Valid
20+
* ##### Valid for `true`
1721
*
1822
* ```js
1923
* var x = {
@@ -31,6 +35,16 @@
3135
* two: 2
3236
* };
3337
* ```
38+
*
39+
* ##### Valid for `{"allExcept": ["function"]}`
40+
*
41+
* ```js
42+
* var x = {
43+
* one: 1,
44+
* two: function() {}
45+
* };
46+
* ```
47+
*
3448
*/
3549

3650
var assert = require('assert');
@@ -40,21 +54,53 @@ module.exports = function() {};
4054
module.exports.prototype = {
4155

4256
configure: function(options) {
43-
assert(
44-
options === true,
45-
this.getOptionName() + ' option requires a true value or should be removed'
46-
);
57+
if (typeof options !== 'object') {
58+
assert(
59+
options === true,
60+
this.getOptionName() + ' option requires either a true value or an object'
61+
);
62+
63+
var _options = {allExcept: []};
64+
return this.configure(_options);
65+
}
66+
67+
if (Array.isArray(options.allExcept)) {
68+
this._exceptFunction = options.allExcept.indexOf('function') > -1;
69+
}
4770
},
4871

4972
getOptionName: function() {
5073
return 'disallowCommaBeforeLineBreak';
5174
},
5275

5376
check: function(file, errors) {
77+
var exceptFunction = this._exceptFunction;
78+
79+
function canSkip(token) {
80+
var node = file.getNodeByRange(token.range[0]);
81+
if (node.loc.start.line === node.loc.end.line) {
82+
return true;
83+
}
84+
85+
return exceptFunction && node.properties.some(function(property) {
86+
return exceptFunction && property.value.type === 'FunctionExpression';
87+
});
88+
}
89+
5490
file.iterateTokensByTypeAndValue('Punctuator', ',', function(token) {
91+
if (canSkip(token)) {
92+
return;
93+
}
94+
5595
errors.assert.sameLine({
5696
token: token,
5797
nextToken: file.getNextToken(token),
98+
message: 'Commas should be placed on the same line as value'
99+
});
100+
101+
errors.assert.differentLine({
102+
token: file.getPrevToken(token),
103+
nextToken: token,
58104
message: 'Commas should be placed on new line'
59105
});
60106
});

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

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,25 @@ describe('rules/disallow-comma-before-line-break', function() {
1515
reportAndFix({
1616
name: 'illegal comma placement in multiline var declaration',
1717
rules: rules,
18+
errors: 2,
1819
input: 'var a,\nb;',
19-
output: 'var a, b;'
20+
output: 'var a\n, b;'
2021
});
2122

2223
reportAndFix({
2324
name: 'illegal comma placement in multiline array declaration',
2425
rules: rules,
26+
errors: 2,
2527
input: 'var a = [1,\n2];',
26-
output: 'var a = [1, 2];'
28+
output: 'var a = [1\n, 2];'
2729
});
2830

2931
reportAndFix({
3032
name: 'illegal comma placement in multiline object declaration',
3133
rules: rules,
34+
errors: 2,
3235
input: 'var a = {a:1,\nc:3};',
33-
output: 'var a = {a:1, c:3};'
36+
output: 'var a = {a:1\n, c:3};'
3437
});
3538

3639
it('should not report legal comma placement in multiline var declaration', function() {
@@ -60,4 +63,32 @@ describe('rules/disallow-comma-before-line-break', function() {
6063
it('should not report comma placement in single line object declaration', function() {
6164
assert(checker.checkString('var a = {a:1, c:3};').isEmpty());
6265
});
66+
67+
describe('options as object', function() {
68+
describe('allExcept as option', function() {
69+
describe('with value `function`', function() {
70+
beforeEach(function() {
71+
checker = new Checker();
72+
checker.registerDefaultRules();
73+
checker.configure({disallowCommaBeforeLineBreak: {allExcept: ['function']}});
74+
});
75+
it('should not report objects with function values', function() {
76+
assert(
77+
checker.checkString(
78+
'var x = {\n' +
79+
'a : 1,\n' +
80+
'foo : function() {},\n' +
81+
'bcd : 2\n' +
82+
'};'
83+
).isEmpty()
84+
);
85+
});
86+
it('should report objects without function values', function() {
87+
assert(
88+
checker.checkString('var a = {a:1,\nc:3};').getErrorCount() === 2
89+
);
90+
});
91+
});
92+
});
93+
});
6394
});

0 commit comments

Comments
 (0)