This repository was archived by the owner on Mar 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 509
/
Copy pathdisallow-padding-newlines-in-blocks.js
168 lines (150 loc) · 4.8 KB
/
disallow-padding-newlines-in-blocks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
* Disallows blocks from beginning or ending with 2 newlines.
*
* Type: `Boolean` or `Object`
*
* Values:
* - `true` validates all non-empty blocks.
* - `Object`:
* - `'open'`
* - `true` validates that there is a newline after the opening brace in a block
* - `false` ignores the newline validation after the opening brace in a block
* - `'close'`
* - `true` validates that there is a newline before the closing brace in a block
* - `false` ignores the newline validation before the closing brace in a block
* - `'allExcept'` array of exceptions:
* - `'conditionals'` ignores conditional (if, else if, else) blocks
* - `'functions'` ignores function blocks
*
* #### Example
*
* ```js
* "disallowPaddingNewlinesInBlocks": true
* "disallowPaddingNewlinesInBlocks": { "open": true, "close": false }
* "disallowPaddingNewlinesInBlocks": { "allExcept": [ "conditionals" ] }
* "disallowPaddingNewlinesInBlocks": { "open": true, "close": false, allExcept: ['conditionals'] }
* ```
*
* ##### Valid for `true`
*
* ```js
* if (true) {
* doSomething();
* }
* if (true) {doSomething();}
* var abc = function() {};
* ```
*
* ##### Valid for mode `{ "open": true, "close": false }`
*
* ```js
* if (true) {
* doSomething();
*
* }
* ```
*
* ##### Valid for `{ allExcept: ['conditionals'] }`
*
* ```js
* if (true) {
*
* doSomething();
*
* }
*
* function (foo) {
* return bar;
* }
* ```
*
* ##### Valid for `{ "open": true, "close": false, allExcept: ['conditionals'] }`
*
* ```js
* function (foo) {
* return bar;
*
* }
*
* if (true) {
*
* doSomething();
*
* }
* ```
*
* ##### Invalid
*
* ```js
* if (true) {
*
* doSomething();
*
* }
* ```
*/
var assert = require('assert');
module.exports = function() {};
module.exports.prototype = {
configure: function(options) {
var optionName = this.getOptionName();
this._checkOpen = true;
this._checkClose = true;
if (typeof options === 'object') {
assert(options.allExcept || options.open || options.close,
optionName + 'option requires either "open", "close", "allExcept"');
if (options.allExcept) {
assert(Array.isArray(options.allExcept), optionName + ' option requires "allExcept" to be an array');
assert(options.allExcept.length > 0, optionName + ' option requires "allExcept" to have at least one ' +
'item or be set to `true`');
this._exceptConditionals = options.allExcept.indexOf('conditionals') > -1;
this._exceptFunctions = options.allExcept.indexOf('functions') > -1;
}
if (options.open || options.close) {
assert(typeof options.open === 'boolean' && typeof options.close === 'boolean',
this.getOptionName() + ' option requires the "open" and "close" ' +
'properties to be booleans');
this._checkOpen = options.open;
this._checkClose = options.close;
}
} else {
assert(options === true, this.getOptionName() + ' option requires either a true value, or an object');
}
},
getOptionName: function() {
return 'disallowPaddingNewlinesInBlocks';
},
check: function(file, errors) {
var exceptConditionals = this._exceptConditionals;
var exceptFunctions = this._exceptFunctions;
var checkOpen = this._checkOpen;
var checkClose = this._checkClose;
file.iterateNodesByType('BlockStatement', function(node) {
var openingBracket;
var closingBracket;
if (exceptConditionals && node.parentElement.type === 'IfStatement' ||
exceptFunctions && (node.parentElement.type === 'FunctionExpression' ||
node.parentElement.type === 'FunctionDeclaration')) {
return;
}
if (checkOpen === true) {
openingBracket = node.getFirstToken();
errors.assert.linesBetween({
token: openingBracket,
nextToken: file.getNextToken(openingBracket, {includeComments: true}),
atMost: 1,
message: 'Expected no padding newline after opening curly brace'
});
}
if (checkClose === true) {
closingBracket = file.getLastNodeToken(node);
errors.assert.linesBetween({
token: file.getPrevToken(closingBracket, {includeComments: true}),
nextToken: closingBracket,
atMost: 1,
message: 'Expected no padding newline before closing curly brace'
});
}
});
}
};