]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | /** |
2 | * @fileoverview Rule to check empty newline after "var" statement | |
3 | * @author Gopal Venkatesan | |
609c276f | 4 | * @deprecated in ESLint v4.0.0 |
eb39fafa DC |
5 | */ |
6 | ||
7 | "use strict"; | |
8 | ||
9 | //------------------------------------------------------------------------------ | |
10 | // Requirements | |
11 | //------------------------------------------------------------------------------ | |
12 | ||
13 | const astUtils = require("./utils/ast-utils"); | |
14 | ||
15 | //------------------------------------------------------------------------------ | |
16 | // Rule Definition | |
17 | //------------------------------------------------------------------------------ | |
18 | ||
19 | module.exports = { | |
20 | meta: { | |
21 | type: "layout", | |
22 | ||
23 | docs: { | |
24 | description: "require or disallow an empty line after variable declarations", | |
eb39fafa DC |
25 | recommended: false, |
26 | url: "https://eslint.org/docs/rules/newline-after-var" | |
27 | }, | |
28 | schema: [ | |
29 | { | |
30 | enum: ["never", "always"] | |
31 | } | |
32 | ], | |
33 | fixable: "whitespace", | |
34 | messages: { | |
35 | expected: "Expected blank line after variable declarations.", | |
36 | unexpected: "Unexpected blank line after variable declarations." | |
37 | }, | |
38 | ||
39 | deprecated: true, | |
40 | ||
41 | replacedBy: ["padding-line-between-statements"] | |
42 | }, | |
43 | ||
44 | create(context) { | |
45 | const sourceCode = context.getSourceCode(); | |
46 | ||
47 | // Default `mode` to "always". | |
48 | const mode = context.options[0] === "never" ? "never" : "always"; | |
49 | ||
50 | // Cache starting and ending line numbers of comments for faster lookup | |
51 | const commentEndLine = sourceCode.getAllComments().reduce((result, token) => { | |
52 | result[token.loc.start.line] = token.loc.end.line; | |
53 | return result; | |
54 | }, {}); | |
55 | ||
56 | ||
57 | //-------------------------------------------------------------------------- | |
58 | // Helpers | |
59 | //-------------------------------------------------------------------------- | |
60 | ||
61 | /** | |
62 | * Gets a token from the given node to compare line to the next statement. | |
63 | * | |
64 | * In general, the token is the last token of the node. However, the token is the second last token if the following conditions satisfy. | |
65 | * | |
66 | * - The last token is semicolon. | |
67 | * - The semicolon is on a different line from the previous token of the semicolon. | |
68 | * | |
69 | * This behavior would address semicolon-less style code. e.g.: | |
70 | * | |
71 | * var foo = 1 | |
72 | * | |
73 | * ;(a || b).doSomething() | |
74 | * @param {ASTNode} node The node to get. | |
75 | * @returns {Token} The token to compare line to the next statement. | |
76 | */ | |
77 | function getLastToken(node) { | |
78 | const lastToken = sourceCode.getLastToken(node); | |
79 | ||
80 | if (lastToken.type === "Punctuator" && lastToken.value === ";") { | |
81 | const prevToken = sourceCode.getTokenBefore(lastToken); | |
82 | ||
83 | if (prevToken.loc.end.line !== lastToken.loc.start.line) { | |
84 | return prevToken; | |
85 | } | |
86 | } | |
87 | ||
88 | return lastToken; | |
89 | } | |
90 | ||
91 | /** | |
92 | * Determine if provided keyword is a variable declaration | |
93 | * @private | |
94 | * @param {string} keyword keyword to test | |
95 | * @returns {boolean} True if `keyword` is a type of var | |
96 | */ | |
97 | function isVar(keyword) { | |
98 | return keyword === "var" || keyword === "let" || keyword === "const"; | |
99 | } | |
100 | ||
101 | /** | |
102 | * Determine if provided keyword is a variant of for specifiers | |
103 | * @private | |
104 | * @param {string} keyword keyword to test | |
105 | * @returns {boolean} True if `keyword` is a variant of for specifier | |
106 | */ | |
107 | function isForTypeSpecifier(keyword) { | |
108 | return keyword === "ForStatement" || keyword === "ForInStatement" || keyword === "ForOfStatement"; | |
109 | } | |
110 | ||
111 | /** | |
112 | * Determine if provided keyword is an export specifiers | |
113 | * @private | |
114 | * @param {string} nodeType nodeType to test | |
115 | * @returns {boolean} True if `nodeType` is an export specifier | |
116 | */ | |
117 | function isExportSpecifier(nodeType) { | |
118 | return nodeType === "ExportNamedDeclaration" || nodeType === "ExportSpecifier" || | |
119 | nodeType === "ExportDefaultDeclaration" || nodeType === "ExportAllDeclaration"; | |
120 | } | |
121 | ||
122 | /** | |
123 | * Determine if provided node is the last of their parent block. | |
124 | * @private | |
125 | * @param {ASTNode} node node to test | |
126 | * @returns {boolean} True if `node` is last of their parent block. | |
127 | */ | |
128 | function isLastNode(node) { | |
129 | const token = sourceCode.getTokenAfter(node); | |
130 | ||
131 | return !token || (token.type === "Punctuator" && token.value === "}"); | |
132 | } | |
133 | ||
134 | /** | |
135 | * Gets the last line of a group of consecutive comments | |
136 | * @param {number} commentStartLine The starting line of the group | |
137 | * @returns {number} The number of the last comment line of the group | |
138 | */ | |
139 | function getLastCommentLineOfBlock(commentStartLine) { | |
140 | const currentCommentEnd = commentEndLine[commentStartLine]; | |
141 | ||
142 | return commentEndLine[currentCommentEnd + 1] ? getLastCommentLineOfBlock(currentCommentEnd + 1) : currentCommentEnd; | |
143 | } | |
144 | ||
145 | /** | |
146 | * Determine if a token starts more than one line after a comment ends | |
609c276f TL |
147 | * @param {token} token The token being checked |
148 | * @param {integer} commentStartLine The line number on which the comment starts | |
149 | * @returns {boolean} True if `token` does not start immediately after a comment | |
eb39fafa DC |
150 | */ |
151 | function hasBlankLineAfterComment(token, commentStartLine) { | |
152 | return token.loc.start.line > getLastCommentLineOfBlock(commentStartLine) + 1; | |
153 | } | |
154 | ||
155 | /** | |
156 | * Checks that a blank line exists after a variable declaration when mode is | |
157 | * set to "always", or checks that there is no blank line when mode is set | |
158 | * to "never" | |
159 | * @private | |
160 | * @param {ASTNode} node `VariableDeclaration` node to test | |
161 | * @returns {void} | |
162 | */ | |
163 | function checkForBlankLine(node) { | |
164 | ||
165 | /* | |
166 | * lastToken is the last token on the node's line. It will usually also be the last token of the node, but it will | |
167 | * sometimes be second-last if there is a semicolon on a different line. | |
168 | */ | |
169 | const lastToken = getLastToken(node), | |
170 | ||
171 | /* | |
172 | * If lastToken is the last token of the node, nextToken should be the token after the node. Otherwise, nextToken | |
173 | * is the last token of the node. | |
174 | */ | |
175 | nextToken = lastToken === sourceCode.getLastToken(node) ? sourceCode.getTokenAfter(node) : sourceCode.getLastToken(node), | |
176 | nextLineNum = lastToken.loc.end.line + 1; | |
177 | ||
178 | // Ignore if there is no following statement | |
179 | if (!nextToken) { | |
180 | return; | |
181 | } | |
182 | ||
183 | // Ignore if parent of node is a for variant | |
184 | if (isForTypeSpecifier(node.parent.type)) { | |
185 | return; | |
186 | } | |
187 | ||
188 | // Ignore if parent of node is an export specifier | |
189 | if (isExportSpecifier(node.parent.type)) { | |
190 | return; | |
191 | } | |
192 | ||
193 | /* | |
194 | * Some coding styles use multiple `var` statements, so do nothing if | |
195 | * the next token is a `var` statement. | |
196 | */ | |
197 | if (nextToken.type === "Keyword" && isVar(nextToken.value)) { | |
198 | return; | |
199 | } | |
200 | ||
201 | // Ignore if it is last statement in a block | |
202 | if (isLastNode(node)) { | |
203 | return; | |
204 | } | |
205 | ||
206 | // Next statement is not a `var`... | |
207 | const noNextLineToken = nextToken.loc.start.line > nextLineNum; | |
208 | const hasNextLineComment = (typeof commentEndLine[nextLineNum] !== "undefined"); | |
209 | ||
210 | if (mode === "never" && noNextLineToken && !hasNextLineComment) { | |
211 | context.report({ | |
212 | node, | |
213 | messageId: "unexpected", | |
214 | data: { identifier: node.name }, | |
215 | fix(fixer) { | |
216 | const linesBetween = sourceCode.getText().slice(lastToken.range[1], nextToken.range[0]).split(astUtils.LINEBREAK_MATCHER); | |
217 | ||
218 | return fixer.replaceTextRange([lastToken.range[1], nextToken.range[0]], `${linesBetween.slice(0, -1).join("")}\n${linesBetween[linesBetween.length - 1]}`); | |
219 | } | |
220 | }); | |
221 | } | |
222 | ||
223 | // Token on the next line, or comment without blank line | |
224 | if ( | |
225 | mode === "always" && ( | |
226 | !noNextLineToken || | |
227 | hasNextLineComment && !hasBlankLineAfterComment(nextToken, nextLineNum) | |
228 | ) | |
229 | ) { | |
230 | context.report({ | |
231 | node, | |
232 | messageId: "expected", | |
233 | data: { identifier: node.name }, | |
234 | fix(fixer) { | |
235 | if ((noNextLineToken ? getLastCommentLineOfBlock(nextLineNum) : lastToken.loc.end.line) === nextToken.loc.start.line) { | |
236 | return fixer.insertTextBefore(nextToken, "\n\n"); | |
237 | } | |
238 | ||
239 | return fixer.insertTextBeforeRange([nextToken.range[0] - nextToken.loc.start.column, nextToken.range[1]], "\n"); | |
240 | } | |
241 | }); | |
242 | } | |
243 | } | |
244 | ||
245 | //-------------------------------------------------------------------------- | |
246 | // Public | |
247 | //-------------------------------------------------------------------------- | |
248 | ||
249 | return { | |
250 | VariableDeclaration: checkForBlankLine | |
251 | }; | |
252 | ||
253 | } | |
254 | }; |