2 * @fileoverview Rule to check empty newline after "var" statement
3 * @author Gopal Venkatesan
9 //------------------------------------------------------------------------------
11 //------------------------------------------------------------------------------
13 const astUtils = require("./utils/ast-utils");
15 //------------------------------------------------------------------------------
17 //------------------------------------------------------------------------------
24 description: "require or disallow an empty line after variable declarations",
25 category: "Stylistic Issues",
27 url: "https://eslint.org/docs/rules/newline-after-var"
31 enum: ["never", "always"]
34 fixable: "whitespace",
36 expected: "Expected blank line after variable declarations.",
37 unexpected: "Unexpected blank line after variable declarations."
42 replacedBy: ["padding-line-between-statements"]
46 const sourceCode = context.getSourceCode();
48 // Default `mode` to "always".
49 const mode = context.options[0] === "never" ? "never" : "always";
51 // Cache starting and ending line numbers of comments for faster lookup
52 const commentEndLine = sourceCode.getAllComments().reduce((result, token) => {
53 result[token.loc.start.line] = token.loc.end.line;
58 //--------------------------------------------------------------------------
60 //--------------------------------------------------------------------------
63 * Gets a token from the given node to compare line to the next statement.
65 * In general, the token is the last token of the node. However, the token is the second last token if the following conditions satisfy.
67 * - The last token is semicolon.
68 * - The semicolon is on a different line from the previous token of the semicolon.
70 * This behavior would address semicolon-less style code. e.g.:
74 * ;(a || b).doSomething()
75 * @param {ASTNode} node The node to get.
76 * @returns {Token} The token to compare line to the next statement.
78 function getLastToken(node) {
79 const lastToken = sourceCode.getLastToken(node);
81 if (lastToken.type === "Punctuator" && lastToken.value === ";") {
82 const prevToken = sourceCode.getTokenBefore(lastToken);
84 if (prevToken.loc.end.line !== lastToken.loc.start.line) {
93 * Determine if provided keyword is a variable declaration
95 * @param {string} keyword keyword to test
96 * @returns {boolean} True if `keyword` is a type of var
98 function isVar(keyword) {
99 return keyword === "var" || keyword === "let" || keyword === "const";
103 * Determine if provided keyword is a variant of for specifiers
105 * @param {string} keyword keyword to test
106 * @returns {boolean} True if `keyword` is a variant of for specifier
108 function isForTypeSpecifier(keyword) {
109 return keyword === "ForStatement" || keyword === "ForInStatement" || keyword === "ForOfStatement";
113 * Determine if provided keyword is an export specifiers
115 * @param {string} nodeType nodeType to test
116 * @returns {boolean} True if `nodeType` is an export specifier
118 function isExportSpecifier(nodeType) {
119 return nodeType === "ExportNamedDeclaration" || nodeType === "ExportSpecifier" ||
120 nodeType === "ExportDefaultDeclaration" || nodeType === "ExportAllDeclaration";
124 * Determine if provided node is the last of their parent block.
126 * @param {ASTNode} node node to test
127 * @returns {boolean} True if `node` is last of their parent block.
129 function isLastNode(node) {
130 const token = sourceCode.getTokenAfter(node);
132 return !token || (token.type === "Punctuator" && token.value === "}");
136 * Gets the last line of a group of consecutive comments
137 * @param {number} commentStartLine The starting line of the group
138 * @returns {number} The number of the last comment line of the group
140 function getLastCommentLineOfBlock(commentStartLine) {
141 const currentCommentEnd = commentEndLine[commentStartLine];
143 return commentEndLine[currentCommentEnd + 1] ? getLastCommentLineOfBlock(currentCommentEnd + 1) : currentCommentEnd;
147 * Determine if a token starts more than one line after a comment ends
148 * @param {token} token The token being checked
149 * @param {integer} commentStartLine The line number on which the comment starts
150 * @returns {boolean} True if `token` does not start immediately after a comment
152 function hasBlankLineAfterComment(token, commentStartLine) {
153 return token.loc.start.line > getLastCommentLineOfBlock(commentStartLine) + 1;
157 * Checks that a blank line exists after a variable declaration when mode is
158 * set to "always", or checks that there is no blank line when mode is set
161 * @param {ASTNode} node `VariableDeclaration` node to test
164 function checkForBlankLine(node) {
167 * lastToken is the last token on the node's line. It will usually also be the last token of the node, but it will
168 * sometimes be second-last if there is a semicolon on a different line.
170 const lastToken = getLastToken(node),
173 * If lastToken is the last token of the node, nextToken should be the token after the node. Otherwise, nextToken
174 * is the last token of the node.
176 nextToken = lastToken === sourceCode.getLastToken(node) ? sourceCode.getTokenAfter(node) : sourceCode.getLastToken(node),
177 nextLineNum = lastToken.loc.end.line + 1;
179 // Ignore if there is no following statement
184 // Ignore if parent of node is a for variant
185 if (isForTypeSpecifier(node.parent.type)) {
189 // Ignore if parent of node is an export specifier
190 if (isExportSpecifier(node.parent.type)) {
195 * Some coding styles use multiple `var` statements, so do nothing if
196 * the next token is a `var` statement.
198 if (nextToken.type === "Keyword" && isVar(nextToken.value)) {
202 // Ignore if it is last statement in a block
203 if (isLastNode(node)) {
207 // Next statement is not a `var`...
208 const noNextLineToken = nextToken.loc.start.line > nextLineNum;
209 const hasNextLineComment = (typeof commentEndLine[nextLineNum] !== "undefined");
211 if (mode === "never" && noNextLineToken && !hasNextLineComment) {
214 messageId: "unexpected",
215 data: { identifier: node.name },
217 const linesBetween = sourceCode.getText().slice(lastToken.range[1], nextToken.range[0]).split(astUtils.LINEBREAK_MATCHER);
219 return fixer.replaceTextRange([lastToken.range[1], nextToken.range[0]], `${linesBetween.slice(0, -1).join("")}\n${linesBetween[linesBetween.length - 1]}`);
224 // Token on the next line, or comment without blank line
226 mode === "always" && (
228 hasNextLineComment && !hasBlankLineAfterComment(nextToken, nextLineNum)
233 messageId: "expected",
234 data: { identifier: node.name },
236 if ((noNextLineToken ? getLastCommentLineOfBlock(nextLineNum) : lastToken.loc.end.line) === nextToken.loc.start.line) {
237 return fixer.insertTextBefore(nextToken, "\n\n");
240 return fixer.insertTextBeforeRange([nextToken.range[0] - nextToken.loc.start.column, nextToken.range[1]], "\n");
246 //--------------------------------------------------------------------------
248 //--------------------------------------------------------------------------
251 VariableDeclaration: checkForBlankLine