lines-around-comment.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /**
  2. * @fileoverview Enforces empty lines around comments.
  3. * @author Jamund Ferguson
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const astUtils = require("./utils/ast-utils");
  10. //------------------------------------------------------------------------------
  11. // Helpers
  12. //------------------------------------------------------------------------------
  13. /**
  14. * Return an array with any line numbers that are empty.
  15. * @param {Array} lines An array of each line of the file.
  16. * @returns {Array} An array of line numbers.
  17. */
  18. function getEmptyLineNums(lines) {
  19. const emptyLines = lines.map((line, i) => ({
  20. code: line.trim(),
  21. num: i + 1
  22. })).filter(line => !line.code).map(line => line.num);
  23. return emptyLines;
  24. }
  25. /**
  26. * Return an array with any line numbers that contain comments.
  27. * @param {Array} comments An array of comment tokens.
  28. * @returns {Array} An array of line numbers.
  29. */
  30. function getCommentLineNums(comments) {
  31. const lines = [];
  32. comments.forEach(token => {
  33. const start = token.loc.start.line;
  34. const end = token.loc.end.line;
  35. lines.push(start, end);
  36. });
  37. return lines;
  38. }
  39. //------------------------------------------------------------------------------
  40. // Rule Definition
  41. //------------------------------------------------------------------------------
  42. /** @type {import('../shared/types').Rule} */
  43. module.exports = {
  44. meta: {
  45. type: "layout",
  46. docs: {
  47. description: "Require empty lines around comments",
  48. recommended: false,
  49. url: "https://eslint.org/docs/latest/rules/lines-around-comment"
  50. },
  51. fixable: "whitespace",
  52. schema: [
  53. {
  54. type: "object",
  55. properties: {
  56. beforeBlockComment: {
  57. type: "boolean",
  58. default: true
  59. },
  60. afterBlockComment: {
  61. type: "boolean",
  62. default: false
  63. },
  64. beforeLineComment: {
  65. type: "boolean",
  66. default: false
  67. },
  68. afterLineComment: {
  69. type: "boolean",
  70. default: false
  71. },
  72. allowBlockStart: {
  73. type: "boolean",
  74. default: false
  75. },
  76. allowBlockEnd: {
  77. type: "boolean",
  78. default: false
  79. },
  80. allowClassStart: {
  81. type: "boolean"
  82. },
  83. allowClassEnd: {
  84. type: "boolean"
  85. },
  86. allowObjectStart: {
  87. type: "boolean"
  88. },
  89. allowObjectEnd: {
  90. type: "boolean"
  91. },
  92. allowArrayStart: {
  93. type: "boolean"
  94. },
  95. allowArrayEnd: {
  96. type: "boolean"
  97. },
  98. ignorePattern: {
  99. type: "string"
  100. },
  101. applyDefaultIgnorePatterns: {
  102. type: "boolean"
  103. },
  104. afterHashbangComment: {
  105. type: "boolean",
  106. default: false
  107. }
  108. },
  109. additionalProperties: false
  110. }
  111. ],
  112. messages: {
  113. after: "Expected line after comment.",
  114. before: "Expected line before comment."
  115. }
  116. },
  117. create(context) {
  118. const options = Object.assign({}, context.options[0]);
  119. const ignorePattern = options.ignorePattern;
  120. const defaultIgnoreRegExp = astUtils.COMMENTS_IGNORE_PATTERN;
  121. const customIgnoreRegExp = new RegExp(ignorePattern, "u");
  122. const applyDefaultIgnorePatterns = options.applyDefaultIgnorePatterns !== false;
  123. options.beforeBlockComment = typeof options.beforeBlockComment !== "undefined" ? options.beforeBlockComment : true;
  124. const sourceCode = context.sourceCode;
  125. const lines = sourceCode.lines,
  126. numLines = lines.length + 1,
  127. comments = sourceCode.getAllComments(),
  128. commentLines = getCommentLineNums(comments),
  129. emptyLines = getEmptyLineNums(lines),
  130. commentAndEmptyLines = new Set(commentLines.concat(emptyLines));
  131. /**
  132. * Returns whether or not comments are on lines starting with or ending with code
  133. * @param {token} token The comment token to check.
  134. * @returns {boolean} True if the comment is not alone.
  135. */
  136. function codeAroundComment(token) {
  137. let currentToken = token;
  138. do {
  139. currentToken = sourceCode.getTokenBefore(currentToken, { includeComments: true });
  140. } while (currentToken && astUtils.isCommentToken(currentToken));
  141. if (currentToken && astUtils.isTokenOnSameLine(currentToken, token)) {
  142. return true;
  143. }
  144. currentToken = token;
  145. do {
  146. currentToken = sourceCode.getTokenAfter(currentToken, { includeComments: true });
  147. } while (currentToken && astUtils.isCommentToken(currentToken));
  148. if (currentToken && astUtils.isTokenOnSameLine(token, currentToken)) {
  149. return true;
  150. }
  151. return false;
  152. }
  153. /**
  154. * Returns whether or not comments are inside a node type or not.
  155. * @param {ASTNode} parent The Comment parent node.
  156. * @param {string} nodeType The parent type to check against.
  157. * @returns {boolean} True if the comment is inside nodeType.
  158. */
  159. function isParentNodeType(parent, nodeType) {
  160. return parent.type === nodeType ||
  161. (parent.body && parent.body.type === nodeType) ||
  162. (parent.consequent && parent.consequent.type === nodeType);
  163. }
  164. /**
  165. * Returns the parent node that contains the given token.
  166. * @param {token} token The token to check.
  167. * @returns {ASTNode|null} The parent node that contains the given token.
  168. */
  169. function getParentNodeOfToken(token) {
  170. const node = sourceCode.getNodeByRangeIndex(token.range[0]);
  171. /*
  172. * For the purpose of this rule, the comment token is in a `StaticBlock` node only
  173. * if it's inside the braces of that `StaticBlock` node.
  174. *
  175. * Example where this function returns `null`:
  176. *
  177. * static
  178. * // comment
  179. * {
  180. * }
  181. *
  182. * Example where this function returns `StaticBlock` node:
  183. *
  184. * static
  185. * {
  186. * // comment
  187. * }
  188. *
  189. */
  190. if (node && node.type === "StaticBlock") {
  191. const openingBrace = sourceCode.getFirstToken(node, { skip: 1 }); // skip the `static` token
  192. return token.range[0] >= openingBrace.range[0]
  193. ? node
  194. : null;
  195. }
  196. return node;
  197. }
  198. /**
  199. * Returns whether or not comments are at the parent start or not.
  200. * @param {token} token The Comment token.
  201. * @param {string} nodeType The parent type to check against.
  202. * @returns {boolean} True if the comment is at parent start.
  203. */
  204. function isCommentAtParentStart(token, nodeType) {
  205. const parent = getParentNodeOfToken(token);
  206. if (parent && isParentNodeType(parent, nodeType)) {
  207. let parentStartNodeOrToken = parent;
  208. if (parent.type === "StaticBlock") {
  209. parentStartNodeOrToken = sourceCode.getFirstToken(parent, { skip: 1 }); // opening brace of the static block
  210. } else if (parent.type === "SwitchStatement") {
  211. parentStartNodeOrToken = sourceCode.getTokenAfter(parent.discriminant, {
  212. filter: astUtils.isOpeningBraceToken
  213. }); // opening brace of the switch statement
  214. }
  215. return token.loc.start.line - parentStartNodeOrToken.loc.start.line === 1;
  216. }
  217. return false;
  218. }
  219. /**
  220. * Returns whether or not comments are at the parent end or not.
  221. * @param {token} token The Comment token.
  222. * @param {string} nodeType The parent type to check against.
  223. * @returns {boolean} True if the comment is at parent end.
  224. */
  225. function isCommentAtParentEnd(token, nodeType) {
  226. const parent = getParentNodeOfToken(token);
  227. return !!parent && isParentNodeType(parent, nodeType) &&
  228. parent.loc.end.line - token.loc.end.line === 1;
  229. }
  230. /**
  231. * Returns whether or not comments are at the block start or not.
  232. * @param {token} token The Comment token.
  233. * @returns {boolean} True if the comment is at block start.
  234. */
  235. function isCommentAtBlockStart(token) {
  236. return (
  237. isCommentAtParentStart(token, "ClassBody") ||
  238. isCommentAtParentStart(token, "BlockStatement") ||
  239. isCommentAtParentStart(token, "StaticBlock") ||
  240. isCommentAtParentStart(token, "SwitchCase") ||
  241. isCommentAtParentStart(token, "SwitchStatement")
  242. );
  243. }
  244. /**
  245. * Returns whether or not comments are at the block end or not.
  246. * @param {token} token The Comment token.
  247. * @returns {boolean} True if the comment is at block end.
  248. */
  249. function isCommentAtBlockEnd(token) {
  250. return (
  251. isCommentAtParentEnd(token, "ClassBody") ||
  252. isCommentAtParentEnd(token, "BlockStatement") ||
  253. isCommentAtParentEnd(token, "StaticBlock") ||
  254. isCommentAtParentEnd(token, "SwitchCase") ||
  255. isCommentAtParentEnd(token, "SwitchStatement")
  256. );
  257. }
  258. /**
  259. * Returns whether or not comments are at the class start or not.
  260. * @param {token} token The Comment token.
  261. * @returns {boolean} True if the comment is at class start.
  262. */
  263. function isCommentAtClassStart(token) {
  264. return isCommentAtParentStart(token, "ClassBody");
  265. }
  266. /**
  267. * Returns whether or not comments are at the class end or not.
  268. * @param {token} token The Comment token.
  269. * @returns {boolean} True if the comment is at class end.
  270. */
  271. function isCommentAtClassEnd(token) {
  272. return isCommentAtParentEnd(token, "ClassBody");
  273. }
  274. /**
  275. * Returns whether or not comments are at the object start or not.
  276. * @param {token} token The Comment token.
  277. * @returns {boolean} True if the comment is at object start.
  278. */
  279. function isCommentAtObjectStart(token) {
  280. return isCommentAtParentStart(token, "ObjectExpression") || isCommentAtParentStart(token, "ObjectPattern");
  281. }
  282. /**
  283. * Returns whether or not comments are at the object end or not.
  284. * @param {token} token The Comment token.
  285. * @returns {boolean} True if the comment is at object end.
  286. */
  287. function isCommentAtObjectEnd(token) {
  288. return isCommentAtParentEnd(token, "ObjectExpression") || isCommentAtParentEnd(token, "ObjectPattern");
  289. }
  290. /**
  291. * Returns whether or not comments are at the array start or not.
  292. * @param {token} token The Comment token.
  293. * @returns {boolean} True if the comment is at array start.
  294. */
  295. function isCommentAtArrayStart(token) {
  296. return isCommentAtParentStart(token, "ArrayExpression") || isCommentAtParentStart(token, "ArrayPattern");
  297. }
  298. /**
  299. * Returns whether or not comments are at the array end or not.
  300. * @param {token} token The Comment token.
  301. * @returns {boolean} True if the comment is at array end.
  302. */
  303. function isCommentAtArrayEnd(token) {
  304. return isCommentAtParentEnd(token, "ArrayExpression") || isCommentAtParentEnd(token, "ArrayPattern");
  305. }
  306. /**
  307. * Checks if a comment token has lines around it (ignores inline comments)
  308. * @param {token} token The Comment token.
  309. * @param {Object} opts Options to determine the newline.
  310. * @param {boolean} opts.after Should have a newline after this line.
  311. * @param {boolean} opts.before Should have a newline before this line.
  312. * @returns {void}
  313. */
  314. function checkForEmptyLine(token, opts) {
  315. if (applyDefaultIgnorePatterns && defaultIgnoreRegExp.test(token.value)) {
  316. return;
  317. }
  318. if (ignorePattern && customIgnoreRegExp.test(token.value)) {
  319. return;
  320. }
  321. let after = opts.after,
  322. before = opts.before;
  323. const prevLineNum = token.loc.start.line - 1,
  324. nextLineNum = token.loc.end.line + 1,
  325. commentIsNotAlone = codeAroundComment(token);
  326. const blockStartAllowed = options.allowBlockStart &&
  327. isCommentAtBlockStart(token) &&
  328. !(options.allowClassStart === false &&
  329. isCommentAtClassStart(token)),
  330. blockEndAllowed = options.allowBlockEnd && isCommentAtBlockEnd(token) && !(options.allowClassEnd === false && isCommentAtClassEnd(token)),
  331. classStartAllowed = options.allowClassStart && isCommentAtClassStart(token),
  332. classEndAllowed = options.allowClassEnd && isCommentAtClassEnd(token),
  333. objectStartAllowed = options.allowObjectStart && isCommentAtObjectStart(token),
  334. objectEndAllowed = options.allowObjectEnd && isCommentAtObjectEnd(token),
  335. arrayStartAllowed = options.allowArrayStart && isCommentAtArrayStart(token),
  336. arrayEndAllowed = options.allowArrayEnd && isCommentAtArrayEnd(token);
  337. const exceptionStartAllowed = blockStartAllowed || classStartAllowed || objectStartAllowed || arrayStartAllowed;
  338. const exceptionEndAllowed = blockEndAllowed || classEndAllowed || objectEndAllowed || arrayEndAllowed;
  339. // ignore top of the file and bottom of the file
  340. if (prevLineNum < 1) {
  341. before = false;
  342. }
  343. if (nextLineNum >= numLines) {
  344. after = false;
  345. }
  346. // we ignore all inline comments
  347. if (commentIsNotAlone) {
  348. return;
  349. }
  350. const previousTokenOrComment = sourceCode.getTokenBefore(token, { includeComments: true });
  351. const nextTokenOrComment = sourceCode.getTokenAfter(token, { includeComments: true });
  352. // check for newline before
  353. if (!exceptionStartAllowed && before && !commentAndEmptyLines.has(prevLineNum) &&
  354. !(astUtils.isCommentToken(previousTokenOrComment) && astUtils.isTokenOnSameLine(previousTokenOrComment, token))) {
  355. const lineStart = token.range[0] - token.loc.start.column;
  356. const range = [lineStart, lineStart];
  357. context.report({
  358. node: token,
  359. messageId: "before",
  360. fix(fixer) {
  361. return fixer.insertTextBeforeRange(range, "\n");
  362. }
  363. });
  364. }
  365. // check for newline after
  366. if (!exceptionEndAllowed && after && !commentAndEmptyLines.has(nextLineNum) &&
  367. !(astUtils.isCommentToken(nextTokenOrComment) && astUtils.isTokenOnSameLine(token, nextTokenOrComment))) {
  368. context.report({
  369. node: token,
  370. messageId: "after",
  371. fix(fixer) {
  372. return fixer.insertTextAfter(token, "\n");
  373. }
  374. });
  375. }
  376. }
  377. //--------------------------------------------------------------------------
  378. // Public
  379. //--------------------------------------------------------------------------
  380. return {
  381. Program() {
  382. comments.forEach(token => {
  383. if (token.type === "Line") {
  384. if (options.beforeLineComment || options.afterLineComment) {
  385. checkForEmptyLine(token, {
  386. after: options.afterLineComment,
  387. before: options.beforeLineComment
  388. });
  389. }
  390. } else if (token.type === "Block") {
  391. if (options.beforeBlockComment || options.afterBlockComment) {
  392. checkForEmptyLine(token, {
  393. after: options.afterBlockComment,
  394. before: options.beforeBlockComment
  395. });
  396. }
  397. } else if (token.type === "Shebang") {
  398. if (options.afterHashbangComment) {
  399. checkForEmptyLine(token, {
  400. after: options.afterHashbangComment,
  401. before: false
  402. });
  403. }
  404. }
  405. });
  406. }
  407. };
  408. }
  409. };