10
10
11
11
module . exports = function ( context ) {
12
12
var spaced = context . options [ 0 ] === 'always' ;
13
+ var multiline = context . options [ 1 ] ? context . options [ 1 ] . allowMultiline : true ;
13
14
14
15
// --------------------------------------------------------------------------
15
16
// Helpers
16
17
// --------------------------------------------------------------------------
17
18
18
19
/**
19
- * Determines whether two adjacent tokens are have whitespace between them.
20
+ * Determines whether two adjacent tokens have a newline between them.
21
+ * @param {Object } left - The left token object.
22
+ * @param {Object } right - The right token object.
23
+ * @returns {boolean } Whether or not there is a newline between the tokens.
24
+ */
25
+ function isMultiline ( left , right ) {
26
+ return left . loc . start . line !== right . loc . start . line ;
27
+ }
28
+
29
+ /**
30
+ * Determines whether two adjacent tokens have whitespace between them.
20
31
* @param {Object } left - The left token object.
21
32
* @param {Object } right - The right token object.
22
33
* @returns {boolean } Whether or not there is space between the tokens.
@@ -25,6 +36,28 @@ module.exports = function(context) {
25
36
return left . range [ 1 ] < right . range [ 0 ] ;
26
37
}
27
38
39
+ /**
40
+ * Reports that there shouldn't be a newline after the first token
41
+ * @param {ASTNode } node - The node to report in the event of an error.
42
+ * @param {Token } token - The token to use for the report.
43
+ * @returns {void }
44
+ */
45
+ function reportNoBeginningNewline ( node , token ) {
46
+ context . report ( node , token . loc . start ,
47
+ 'There should be no newline after \'' + token . value + '\'' ) ;
48
+ }
49
+
50
+ /**
51
+ * Reports that there shouldn't be a newline before the last token
52
+ * @param {ASTNode } node - The node to report in the event of an error.
53
+ * @param {Token } token - The token to use for the report.
54
+ * @returns {void }
55
+ */
56
+ function reportNoEndingNewline ( node , token ) {
57
+ context . report ( node , token . loc . start ,
58
+ 'There should be no newline before \'' + token . value + '\'' ) ;
59
+ }
60
+
28
61
/**
29
62
* Reports that there shouldn't be a space after the first token
30
63
* @param {ASTNode } node - The node to report in the event of an error.
@@ -79,16 +112,28 @@ module.exports = function(context) {
79
112
* @returns {void }
80
113
*/
81
114
function validateBraceSpacing ( node , first , second , penultimate , last ) {
82
- if ( spaced && ! isSpaced ( first , second ) ) {
83
- reportRequiredBeginningSpace ( node , first ) ;
115
+ if ( spaced ) {
116
+ if ( ! isSpaced ( first , second ) ) {
117
+ reportRequiredBeginningSpace ( node , first ) ;
118
+ } else if ( ! multiline && isMultiline ( first , second ) ) {
119
+ reportNoBeginningNewline ( node , first ) ;
120
+ }
121
+
122
+ if ( ! isSpaced ( penultimate , last ) ) {
123
+ reportRequiredEndingSpace ( node , last ) ;
124
+ } else if ( ! multiline && isMultiline ( penultimate , last ) ) {
125
+ reportNoEndingNewline ( node , last ) ;
126
+ }
127
+
128
+ return ;
84
129
}
85
- if ( ! spaced && isSpaced ( first , second ) ) {
130
+
131
+ // "never" setting if we get here.
132
+ if ( isSpaced ( first , second ) && ! ( multiline && isMultiline ( first , second ) ) ) {
86
133
reportNoBeginningSpace ( node , first ) ;
87
134
}
88
- if ( spaced && ! isSpaced ( penultimate , last ) ) {
89
- reportRequiredEndingSpace ( node , last ) ;
90
- }
91
- if ( ! spaced && isSpaced ( penultimate , last ) ) {
135
+
136
+ if ( isSpaced ( penultimate , last ) && ! ( multiline && isMultiline ( penultimate , last ) ) ) {
92
137
reportNoEndingSpace ( node , last ) ;
93
138
}
94
139
}
@@ -111,4 +156,12 @@ module.exports = function(context) {
111
156
112
157
module . exports . schema = [ {
113
158
enum : [ 'always' , 'never' ]
159
+ } , {
160
+ type : 'object' ,
161
+ properties : {
162
+ allowMultiline : {
163
+ type : 'boolean'
164
+ }
165
+ } ,
166
+ additionalProperties : false
114
167
} ] ;
0 commit comments