123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471 |
- "use strict";
- const astUtils = require("./utils/ast-utils");
- function getEmptyLineNums(lines) {
- const emptyLines = lines.map((line, i) => ({
- code: line.trim(),
- num: i + 1
- })).filter(line => !line.code).map(line => line.num);
- return emptyLines;
- }
- function getCommentLineNums(comments) {
- const lines = [];
- comments.forEach(token => {
- const start = token.loc.start.line;
- const end = token.loc.end.line;
- lines.push(start, end);
- });
- return lines;
- }
- module.exports = {
- meta: {
- deprecated: true,
- replacedBy: [],
- type: "layout",
- docs: {
- description: "Require empty lines around comments",
- recommended: false,
- url: "https://eslint.org/docs/latest/rules/lines-around-comment"
- },
- fixable: "whitespace",
- schema: [
- {
- type: "object",
- properties: {
- beforeBlockComment: {
- type: "boolean",
- default: true
- },
- afterBlockComment: {
- type: "boolean",
- default: false
- },
- beforeLineComment: {
- type: "boolean",
- default: false
- },
- afterLineComment: {
- type: "boolean",
- default: false
- },
- allowBlockStart: {
- type: "boolean",
- default: false
- },
- allowBlockEnd: {
- type: "boolean",
- default: false
- },
- allowClassStart: {
- type: "boolean"
- },
- allowClassEnd: {
- type: "boolean"
- },
- allowObjectStart: {
- type: "boolean"
- },
- allowObjectEnd: {
- type: "boolean"
- },
- allowArrayStart: {
- type: "boolean"
- },
- allowArrayEnd: {
- type: "boolean"
- },
- ignorePattern: {
- type: "string"
- },
- applyDefaultIgnorePatterns: {
- type: "boolean"
- },
- afterHashbangComment: {
- type: "boolean",
- default: false
- }
- },
- additionalProperties: false
- }
- ],
- messages: {
- after: "Expected line after comment.",
- before: "Expected line before comment."
- }
- },
- create(context) {
- const options = Object.assign({}, context.options[0]);
- const ignorePattern = options.ignorePattern;
- const defaultIgnoreRegExp = astUtils.COMMENTS_IGNORE_PATTERN;
- const customIgnoreRegExp = new RegExp(ignorePattern, "u");
- const applyDefaultIgnorePatterns = options.applyDefaultIgnorePatterns !== false;
- options.beforeBlockComment = typeof options.beforeBlockComment !== "undefined" ? options.beforeBlockComment : true;
- const sourceCode = context.sourceCode;
- const lines = sourceCode.lines,
- numLines = lines.length + 1,
- comments = sourceCode.getAllComments(),
- commentLines = getCommentLineNums(comments),
- emptyLines = getEmptyLineNums(lines),
- commentAndEmptyLines = new Set(commentLines.concat(emptyLines));
-
- function codeAroundComment(token) {
- let currentToken = token;
- do {
- currentToken = sourceCode.getTokenBefore(currentToken, { includeComments: true });
- } while (currentToken && astUtils.isCommentToken(currentToken));
- if (currentToken && astUtils.isTokenOnSameLine(currentToken, token)) {
- return true;
- }
- currentToken = token;
- do {
- currentToken = sourceCode.getTokenAfter(currentToken, { includeComments: true });
- } while (currentToken && astUtils.isCommentToken(currentToken));
- if (currentToken && astUtils.isTokenOnSameLine(token, currentToken)) {
- return true;
- }
- return false;
- }
-
- function isParentNodeType(parent, nodeType) {
- return parent.type === nodeType ||
- (parent.body && parent.body.type === nodeType) ||
- (parent.consequent && parent.consequent.type === nodeType);
- }
-
- function getParentNodeOfToken(token) {
- const node = sourceCode.getNodeByRangeIndex(token.range[0]);
-
- if (node && node.type === "StaticBlock") {
- const openingBrace = sourceCode.getFirstToken(node, { skip: 1 });
- return token.range[0] >= openingBrace.range[0]
- ? node
- : null;
- }
- return node;
- }
-
- function isCommentAtParentStart(token, nodeType) {
- const parent = getParentNodeOfToken(token);
- if (parent && isParentNodeType(parent, nodeType)) {
- let parentStartNodeOrToken = parent;
- if (parent.type === "StaticBlock") {
- parentStartNodeOrToken = sourceCode.getFirstToken(parent, { skip: 1 });
- } else if (parent.type === "SwitchStatement") {
- parentStartNodeOrToken = sourceCode.getTokenAfter(parent.discriminant, {
- filter: astUtils.isOpeningBraceToken
- });
- }
- return token.loc.start.line - parentStartNodeOrToken.loc.start.line === 1;
- }
- return false;
- }
-
- function isCommentAtParentEnd(token, nodeType) {
- const parent = getParentNodeOfToken(token);
- return !!parent && isParentNodeType(parent, nodeType) &&
- parent.loc.end.line - token.loc.end.line === 1;
- }
-
- function isCommentAtBlockStart(token) {
- return (
- isCommentAtParentStart(token, "ClassBody") ||
- isCommentAtParentStart(token, "BlockStatement") ||
- isCommentAtParentStart(token, "StaticBlock") ||
- isCommentAtParentStart(token, "SwitchCase") ||
- isCommentAtParentStart(token, "SwitchStatement")
- );
- }
-
- function isCommentAtBlockEnd(token) {
- return (
- isCommentAtParentEnd(token, "ClassBody") ||
- isCommentAtParentEnd(token, "BlockStatement") ||
- isCommentAtParentEnd(token, "StaticBlock") ||
- isCommentAtParentEnd(token, "SwitchCase") ||
- isCommentAtParentEnd(token, "SwitchStatement")
- );
- }
-
- function isCommentAtClassStart(token) {
- return isCommentAtParentStart(token, "ClassBody");
- }
-
- function isCommentAtClassEnd(token) {
- return isCommentAtParentEnd(token, "ClassBody");
- }
-
- function isCommentAtObjectStart(token) {
- return isCommentAtParentStart(token, "ObjectExpression") || isCommentAtParentStart(token, "ObjectPattern");
- }
-
- function isCommentAtObjectEnd(token) {
- return isCommentAtParentEnd(token, "ObjectExpression") || isCommentAtParentEnd(token, "ObjectPattern");
- }
-
- function isCommentAtArrayStart(token) {
- return isCommentAtParentStart(token, "ArrayExpression") || isCommentAtParentStart(token, "ArrayPattern");
- }
-
- function isCommentAtArrayEnd(token) {
- return isCommentAtParentEnd(token, "ArrayExpression") || isCommentAtParentEnd(token, "ArrayPattern");
- }
-
- function checkForEmptyLine(token, opts) {
- if (applyDefaultIgnorePatterns && defaultIgnoreRegExp.test(token.value)) {
- return;
- }
- if (ignorePattern && customIgnoreRegExp.test(token.value)) {
- return;
- }
- let after = opts.after,
- before = opts.before;
- const prevLineNum = token.loc.start.line - 1,
- nextLineNum = token.loc.end.line + 1,
- commentIsNotAlone = codeAroundComment(token);
- const blockStartAllowed = options.allowBlockStart &&
- isCommentAtBlockStart(token) &&
- !(options.allowClassStart === false &&
- isCommentAtClassStart(token)),
- blockEndAllowed = options.allowBlockEnd && isCommentAtBlockEnd(token) && !(options.allowClassEnd === false && isCommentAtClassEnd(token)),
- classStartAllowed = options.allowClassStart && isCommentAtClassStart(token),
- classEndAllowed = options.allowClassEnd && isCommentAtClassEnd(token),
- objectStartAllowed = options.allowObjectStart && isCommentAtObjectStart(token),
- objectEndAllowed = options.allowObjectEnd && isCommentAtObjectEnd(token),
- arrayStartAllowed = options.allowArrayStart && isCommentAtArrayStart(token),
- arrayEndAllowed = options.allowArrayEnd && isCommentAtArrayEnd(token);
- const exceptionStartAllowed = blockStartAllowed || classStartAllowed || objectStartAllowed || arrayStartAllowed;
- const exceptionEndAllowed = blockEndAllowed || classEndAllowed || objectEndAllowed || arrayEndAllowed;
-
- if (prevLineNum < 1) {
- before = false;
- }
- if (nextLineNum >= numLines) {
- after = false;
- }
-
- if (commentIsNotAlone) {
- return;
- }
- const previousTokenOrComment = sourceCode.getTokenBefore(token, { includeComments: true });
- const nextTokenOrComment = sourceCode.getTokenAfter(token, { includeComments: true });
-
- if (!exceptionStartAllowed && before && !commentAndEmptyLines.has(prevLineNum) &&
- !(astUtils.isCommentToken(previousTokenOrComment) && astUtils.isTokenOnSameLine(previousTokenOrComment, token))) {
- const lineStart = token.range[0] - token.loc.start.column;
- const range = [lineStart, lineStart];
- context.report({
- node: token,
- messageId: "before",
- fix(fixer) {
- return fixer.insertTextBeforeRange(range, "\n");
- }
- });
- }
-
- if (!exceptionEndAllowed && after && !commentAndEmptyLines.has(nextLineNum) &&
- !(astUtils.isCommentToken(nextTokenOrComment) && astUtils.isTokenOnSameLine(token, nextTokenOrComment))) {
- context.report({
- node: token,
- messageId: "after",
- fix(fixer) {
- return fixer.insertTextAfter(token, "\n");
- }
- });
- }
- }
-
-
-
- return {
- Program() {
- comments.forEach(token => {
- if (token.type === "Line") {
- if (options.beforeLineComment || options.afterLineComment) {
- checkForEmptyLine(token, {
- after: options.afterLineComment,
- before: options.beforeLineComment
- });
- }
- } else if (token.type === "Block") {
- if (options.beforeBlockComment || options.afterBlockComment) {
- checkForEmptyLine(token, {
- after: options.afterBlockComment,
- before: options.beforeBlockComment
- });
- }
- } else if (token.type === "Shebang") {
- if (options.afterHashbangComment) {
- checkForEmptyLine(token, {
- after: options.afterHashbangComment,
- before: false
- });
- }
- }
- });
- }
- };
- }
- };
|