123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- "use strict";
- const astUtils = require("./utils/ast-utils");
- module.exports = {
- meta: {
- type: "layout",
- docs: {
- description: "require or disallow an empty line after variable declarations",
- category: "Stylistic Issues",
- recommended: false,
- url: "https://eslint.org/docs/rules/newline-after-var"
- },
- schema: [
- {
- enum: ["never", "always"]
- }
- ],
- fixable: "whitespace",
- messages: {
- expected: "Expected blank line after variable declarations.",
- unexpected: "Unexpected blank line after variable declarations."
- },
- deprecated: true,
- replacedBy: ["padding-line-between-statements"]
- },
- create(context) {
- const sourceCode = context.getSourceCode();
-
- const mode = context.options[0] === "never" ? "never" : "always";
-
- const commentEndLine = sourceCode.getAllComments().reduce((result, token) => {
- result[token.loc.start.line] = token.loc.end.line;
- return result;
- }, {});
-
-
-
-
- function getLastToken(node) {
- const lastToken = sourceCode.getLastToken(node);
- if (lastToken.type === "Punctuator" && lastToken.value === ";") {
- const prevToken = sourceCode.getTokenBefore(lastToken);
- if (prevToken.loc.end.line !== lastToken.loc.start.line) {
- return prevToken;
- }
- }
- return lastToken;
- }
-
- function isVar(keyword) {
- return keyword === "var" || keyword === "let" || keyword === "const";
- }
-
- function isForTypeSpecifier(keyword) {
- return keyword === "ForStatement" || keyword === "ForInStatement" || keyword === "ForOfStatement";
- }
-
- function isExportSpecifier(nodeType) {
- return nodeType === "ExportNamedDeclaration" || nodeType === "ExportSpecifier" ||
- nodeType === "ExportDefaultDeclaration" || nodeType === "ExportAllDeclaration";
- }
-
- function isLastNode(node) {
- const token = sourceCode.getTokenAfter(node);
- return !token || (token.type === "Punctuator" && token.value === "}");
- }
-
- function getLastCommentLineOfBlock(commentStartLine) {
- const currentCommentEnd = commentEndLine[commentStartLine];
- return commentEndLine[currentCommentEnd + 1] ? getLastCommentLineOfBlock(currentCommentEnd + 1) : currentCommentEnd;
- }
-
- function hasBlankLineAfterComment(token, commentStartLine) {
- return token.loc.start.line > getLastCommentLineOfBlock(commentStartLine) + 1;
- }
-
- function checkForBlankLine(node) {
-
- const lastToken = getLastToken(node),
-
- nextToken = lastToken === sourceCode.getLastToken(node) ? sourceCode.getTokenAfter(node) : sourceCode.getLastToken(node),
- nextLineNum = lastToken.loc.end.line + 1;
-
- if (!nextToken) {
- return;
- }
-
- if (isForTypeSpecifier(node.parent.type)) {
- return;
- }
-
- if (isExportSpecifier(node.parent.type)) {
- return;
- }
-
- if (nextToken.type === "Keyword" && isVar(nextToken.value)) {
- return;
- }
-
- if (isLastNode(node)) {
- return;
- }
-
- const noNextLineToken = nextToken.loc.start.line > nextLineNum;
- const hasNextLineComment = (typeof commentEndLine[nextLineNum] !== "undefined");
- if (mode === "never" && noNextLineToken && !hasNextLineComment) {
- context.report({
- node,
- messageId: "unexpected",
- data: { identifier: node.name },
- fix(fixer) {
- const linesBetween = sourceCode.getText().slice(lastToken.range[1], nextToken.range[0]).split(astUtils.LINEBREAK_MATCHER);
- return fixer.replaceTextRange([lastToken.range[1], nextToken.range[0]], `${linesBetween.slice(0, -1).join("")}\n${linesBetween[linesBetween.length - 1]}`);
- }
- });
- }
-
- if (
- mode === "always" && (
- !noNextLineToken ||
- hasNextLineComment && !hasBlankLineAfterComment(nextToken, nextLineNum)
- )
- ) {
- context.report({
- node,
- messageId: "expected",
- data: { identifier: node.name },
- fix(fixer) {
- if ((noNextLineToken ? getLastCommentLineOfBlock(nextLineNum) : lastToken.loc.end.line) === nextToken.loc.start.line) {
- return fixer.insertTextBefore(nextToken, "\n\n");
- }
- return fixer.insertTextBeforeRange([nextToken.range[0] - nextToken.loc.start.column, nextToken.range[1]], "\n");
- }
- });
- }
- }
-
-
-
- return {
- VariableDeclaration: checkForBlankLine
- };
- }
- };
|