UNPKG

2.05 kBJavaScriptView Raw
1/**
2 * Requires placing line feed after assigning a variable.
3 *
4 * Type: `Boolean`
5 *
6 * Value: `true`
7 *
8 * #### Example
9 *
10 * ```js
11 * "requireLineBreakAfterVariableAssignment": true
12 * ```
13 *
14 * ##### Valid
15 *
16 * ```js
17 * var abc = 8;
18 * var foo = 5;
19 *
20 * var a, b, c,
21 * foo = 7,
22 * bar = 8;
23 *
24 * var a,
25 * foo = 7,
26 * a, b, c,
27 * bar = 8;
28 * ```
29 *
30 * ##### Invalid
31 *
32 * ```js
33 * var abc = 8; var foo = 5;
34 *
35 * var a, b, c,
36 * foo = 7, bar = 8;
37 * ```
38 */
39
40var assert = require('assert');
41
42module.exports = function() {};
43
44module.exports.prototype = {
45
46 configure: function(options) {
47 assert(
48 options === true,
49 this.getOptionName() + ' option requires a true value or should be removed'
50 );
51 },
52
53 getOptionName: function() {
54 return 'requireLineBreakAfterVariableAssignment';
55 },
56
57 check: function(file, errors) {
58 var lastDeclaration;
59 file.iterateNodesByType('VariableDeclaration', function(node) {
60 if (node.parentElement.type === 'ForStatement' ||
61 node.parentElement.type === 'ForInStatement' ||
62 node.parentElement.type === 'ForOfStatement') {
63 return;
64 }
65
66 for (var i = 0; i < node.declarations.length; i++) {
67 var thisDeclaration = node.declarations[i];
68 if (thisDeclaration.parentElement.kind === 'var' ||
69 thisDeclaration.parentElement.kind === 'let' ||
70 thisDeclaration.parentElement.kind === 'const') {
71 if (lastDeclaration && lastDeclaration.init) {
72 errors.assert.differentLine({
73 token: lastDeclaration,
74 nextToken: thisDeclaration,
75 message: 'Variable assignments should be followed by new line'
76 });
77 }
78 lastDeclaration = thisDeclaration;
79 }
80 }
81 });
82 }
83
84};