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-before-keywords.js
98 lines (90 loc) · 1.95 KB
/
disallow-padding-newlines-before-keywords.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
/**
* Disallow an empty line above the specified keywords.
*
* Types: `Array` or `Boolean`
*
* Values: Array of quoted types or `true` to disallow padding new lines after all of the keywords below.
*
* #### Example
*
* ```js
* "disallowPaddingNewlinesBeforeKeywords": [
* "do",
* "for",
* "if",
* "else",
* "switch",
* "case",
* "try",
* "catch",
* "void",
* "while",
* "with",
* "return",
* "typeof",
* "function"
* ]
* ```
*
* ##### Valid
*
* ```js
* function(a) {
* if (!a) {
* return false;
* }
* for (var i = 0; i < b; i++) {
* if (!a[i]) {
* return false;
* }
* }
* return true;
* }
* ```
*
* ##### Invalid
*
* ```js
* function(a) {
* if (!a) {
*
* return false;
* }
*
* for (var i = 0; i < b; i++) {
* if (!a[i]) {
*
* return false;
* }
* }
*
* return true;
* }
* ```
*/
var assert = require('assert');
var defaultKeywords = require('../utils').spacedKeywords;
module.exports = function() { };
module.exports.prototype = {
configure: function(keywords) {
assert(Array.isArray(keywords) || keywords === true,
this.getOptionName() + ' option requires array or true value');
if (keywords === true) {
keywords = defaultKeywords;
}
this._keywords = keywords;
},
getOptionName: function() {
return 'disallowPaddingNewlinesBeforeKeywords';
},
check: function(file, errors) {
file.iterateTokensByTypeAndValue('Keyword', this._keywords, function(token) {
errors.assert.linesBetween({
token: file.getPrevToken(token, { includeComments: true }),
nextToken: token,
atMost: 1,
message: 'Keyword `' + token.value + '` should not have an empty line above it'
});
});
}
};