Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lines-around-comment.js 16KB

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