1 | /**
|
2 | * Requires commas as last token on a line in lists.
|
3 | *
|
4 | * Type: `Boolean`
|
5 | *
|
6 | * Value: `true`
|
7 | *
|
8 | * JSHint: [`laxcomma`](http://www.jshint.com/docs/options/#laxcomma)
|
9 | *
|
10 | * #### Example
|
11 | *
|
12 | * ```js
|
13 | * "requireCommaBeforeLineBreak": true
|
14 | * ```
|
15 | *
|
16 | * ##### Valid
|
17 | *
|
18 | * ```js
|
19 | * var x = {
|
20 | * one: 1,
|
21 | * two: 2
|
22 | * };
|
23 | * var y = { three: 3, four: 4};
|
24 | * ```
|
25 | *
|
26 | * ##### Invalid
|
27 | *
|
28 | * ```js
|
29 | * var x = {
|
30 | * one: 1
|
31 | * , two: 2
|
32 | * };
|
33 | * ```
|
34 | */
|
35 |
|
36 | var assert = require('assert');
|
37 |
|
38 | module.exports = function() {};
|
39 |
|
40 | module.exports.prototype = {
|
41 |
|
42 | configure: function(options) {
|
43 | assert(
|
44 | options === true,
|
45 | this.getOptionName() + ' option requires a true value or should be removed'
|
46 | );
|
47 | },
|
48 |
|
49 | getOptionName: function() {
|
50 | return 'requireCommaBeforeLineBreak';
|
51 | },
|
52 |
|
53 | check: function(file, errors) {
|
54 | file.iterateTokensByTypeAndValue('Punctuator', ',', function(token) {
|
55 | var prevToken = token.getPreviousCodeToken();
|
56 |
|
57 | if (prevToken.value === ',') {
|
58 | return;
|
59 | }
|
60 | errors.assert.sameLine({
|
61 | token: prevToken,
|
62 | nextToken: token,
|
63 | message: 'Commas should not be placed on new line'
|
64 | });
|
65 | });
|
66 | }
|
67 |
|
68 | };
|