@@ -30,7 +30,9 @@ module.exports = {
30
30
} ,
31
31
additionalProperties : false
32
32
}
33
- ]
33
+ ] ,
34
+
35
+ fixable : "whitespace"
34
36
} ,
35
37
36
38
create ( context ) {
@@ -69,6 +71,28 @@ module.exports = {
69
71
return token . value === "{" || token . value === "}" ;
70
72
}
71
73
74
+ /**
75
+ * Reports a place where a newline unexpectedly appears
76
+ * @param {ASTNode } node The node to report
77
+ * @param {string } message The message to report
78
+ * @param {Token } firstToken The token before the unexpected newline
79
+ * @returns {void }
80
+ */
81
+ function reportExtraNewline ( node , message , firstToken ) {
82
+ context . report ( {
83
+ node,
84
+ message,
85
+ fix ( fixer ) {
86
+ const secondToken = sourceCode . getTokenAfter ( firstToken ) ;
87
+ const textBetween = sourceCode . getText ( ) . slice ( firstToken . range [ 1 ] , secondToken . range [ 0 ] ) ;
88
+ const NEWLINE_REGEX = / \r \n | \r | \n | \u2028 | \u2029 / g;
89
+
90
+ // Don't do a fix if there is a comment between the tokens.
91
+ return textBetween . trim ( ) ? null : fixer . replaceTextRange ( [ firstToken . range [ 1 ] , secondToken . range [ 0 ] ] , textBetween . replace ( NEWLINE_REGEX , "" ) ) ;
92
+ }
93
+ } ) ;
94
+ }
95
+
72
96
/**
73
97
* Binds a list of properties to a function that verifies that the opening
74
98
* curly brace is on the same line as its controlling statement of a given
@@ -98,21 +122,33 @@ module.exports = {
98
122
}
99
123
100
124
if ( style !== "allman" && previousToken . loc . start . line !== curlyToken . loc . start . line ) {
101
- context . report ( node , OPEN_MESSAGE ) ;
125
+ reportExtraNewline ( node , OPEN_MESSAGE , previousToken ) ;
102
126
} else if ( style === "allman" && previousToken . loc . start . line === curlyToken . loc . start . line ) {
103
- context . report ( node , OPEN_MESSAGE_ALLMAN ) ;
127
+ context . report ( {
128
+ node,
129
+ message : OPEN_MESSAGE_ALLMAN ,
130
+ fix : fixer => fixer . insertTextBefore ( curlyToken , "\n" )
131
+ } ) ;
104
132
}
105
133
106
134
if ( ! block . body . length ) {
107
135
return ;
108
136
}
109
137
110
138
if ( curlyToken . loc . start . line === block . body [ 0 ] . loc . start . line ) {
111
- context . report ( block . body [ 0 ] , BODY_MESSAGE ) ;
139
+ context . report ( {
140
+ node : block . body [ 0 ] ,
141
+ message : BODY_MESSAGE ,
142
+ fix : fixer => fixer . insertTextAfter ( curlyToken , "\n" )
143
+ } ) ;
112
144
}
113
145
114
146
if ( curlyTokenEnd . loc . start . line === block . body [ block . body . length - 1 ] . loc . start . line ) {
115
- context . report ( block . body [ block . body . length - 1 ] , CLOSE_MESSAGE_SINGLE ) ;
147
+ context . report ( {
148
+ node : block . body [ block . body . length - 1 ] ,
149
+ message : CLOSE_MESSAGE_SINGLE ,
150
+ fix : fixer => fixer . insertTextBefore ( curlyTokenEnd , "\n" )
151
+ } ) ;
116
152
}
117
153
} ) ;
118
154
} ;
@@ -135,10 +171,14 @@ module.exports = {
135
171
if ( tokens [ 0 ] . loc . start . line !== tokens [ 1 ] . loc . start . line &&
136
172
node . consequent . type === "BlockStatement" &&
137
173
isCurlyPunctuator ( tokens [ 0 ] ) ) {
138
- context . report ( node . alternate , CLOSE_MESSAGE ) ;
174
+ reportExtraNewline ( node . alternate , CLOSE_MESSAGE , tokens [ 0 ] ) ;
139
175
}
140
176
} else if ( tokens [ 0 ] . loc . start . line === tokens [ 1 ] . loc . start . line ) {
141
- context . report ( node . alternate , CLOSE_MESSAGE_STROUSTRUP_ALLMAN ) ;
177
+ context . report ( {
178
+ node : node . alternate ,
179
+ message : CLOSE_MESSAGE_STROUSTRUP_ALLMAN ,
180
+ fix : fixer => fixer . insertTextAfter ( tokens [ 0 ] , "\n" )
181
+ } ) ;
142
182
}
143
183
144
184
}
@@ -158,10 +198,14 @@ module.exports = {
158
198
159
199
if ( style === "1tbs" ) {
160
200
if ( tokens [ 0 ] . loc . start . line !== tokens [ 1 ] . loc . start . line ) {
161
- context . report ( node . finalizer , CLOSE_MESSAGE ) ;
201
+ reportExtraNewline ( node . finalizer , CLOSE_MESSAGE , tokens [ 0 ] ) ;
162
202
}
163
203
} else if ( tokens [ 0 ] . loc . start . line === tokens [ 1 ] . loc . start . line ) {
164
- context . report ( node . finalizer , CLOSE_MESSAGE_STROUSTRUP_ALLMAN ) ;
204
+ context . report ( {
205
+ node : node . finalizer ,
206
+ message : CLOSE_MESSAGE_STROUSTRUP_ALLMAN ,
207
+ fix : fixer => fixer . insertTextAfter ( tokens [ 0 ] , "\n" )
208
+ } ) ;
165
209
}
166
210
}
167
211
}
@@ -181,11 +225,15 @@ module.exports = {
181
225
if ( isBlock ( node . body ) ) {
182
226
if ( style === "1tbs" ) {
183
227
if ( previousToken . loc . start . line !== firstToken . loc . start . line ) {
184
- context . report ( node , CLOSE_MESSAGE ) ;
228
+ reportExtraNewline ( node , CLOSE_MESSAGE , previousToken ) ;
185
229
}
186
230
} else {
187
231
if ( previousToken . loc . start . line === firstToken . loc . start . line ) {
188
- context . report ( node , CLOSE_MESSAGE_STROUSTRUP_ALLMAN ) ;
232
+ context . report ( {
233
+ node,
234
+ message : CLOSE_MESSAGE_STROUSTRUP_ALLMAN ,
235
+ fix : fixer => fixer . insertTextAfter ( previousToken , "\n" )
236
+ } ) ;
189
237
}
190
238
}
191
239
}
@@ -207,9 +255,13 @@ module.exports = {
207
255
}
208
256
209
257
if ( style !== "allman" && tokens [ 0 ] . loc . start . line !== tokens [ 1 ] . loc . start . line ) {
210
- context . report ( node , OPEN_MESSAGE ) ;
258
+ reportExtraNewline ( node , OPEN_MESSAGE , tokens [ 0 ] ) ;
211
259
} else if ( style === "allman" && tokens [ 0 ] . loc . start . line === tokens [ 1 ] . loc . start . line ) {
212
- context . report ( node , OPEN_MESSAGE_ALLMAN ) ;
260
+ context . report ( {
261
+ node,
262
+ message : OPEN_MESSAGE_ALLMAN ,
263
+ fix : fixer => fixer . insertTextBefore ( tokens [ 1 ] , "\n" )
264
+ } ) ;
213
265
}
214
266
}
215
267
0 commit comments