Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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 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 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. module.exports = {
  43. meta: {
  44. type: "layout",
  45. docs: {
  46. description: "require empty lines around comments",
  47. category: "Stylistic Issues",
  48. recommended: false,
  49. url: "https://eslint.org/docs/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. },
  105. additionalProperties: false
  106. }
  107. ],
  108. messages: {
  109. after: "Expected line after comment.",
  110. before: "Expected line before comment."
  111. }
  112. },
  113. create(context) {
  114. const options = Object.assign({}, context.options[0]);
  115. const ignorePattern = options.ignorePattern;
  116. const defaultIgnoreRegExp = astUtils.COMMENTS_IGNORE_PATTERN;
  117. const customIgnoreRegExp = new RegExp(ignorePattern, "u");
  118. const applyDefaultIgnorePatterns = options.applyDefaultIgnorePatterns !== false;
  119. options.beforeBlockComment = typeof options.beforeBlockComment !== "undefined" ? options.beforeBlockComment : true;
  120. const sourceCode = context.getSourceCode();
  121. const lines = sourceCode.lines,
  122. numLines = lines.length + 1,
  123. comments = sourceCode.getAllComments(),
  124. commentLines = getCommentLineNums(comments),
  125. emptyLines = getEmptyLineNums(lines),
  126. commentAndEmptyLines = commentLines.concat(emptyLines);
  127. /**
  128. * Returns whether or not comments are on lines starting with or ending with code
  129. * @param {token} token The comment token to check.
  130. * @returns {boolean} True if the comment is not alone.
  131. */
  132. function codeAroundComment(token) {
  133. let currentToken = token;
  134. do {
  135. currentToken = sourceCode.getTokenBefore(currentToken, { includeComments: true });
  136. } while (currentToken && astUtils.isCommentToken(currentToken));
  137. if (currentToken && astUtils.isTokenOnSameLine(currentToken, token)) {
  138. return true;
  139. }
  140. currentToken = token;
  141. do {
  142. currentToken = sourceCode.getTokenAfter(currentToken, { includeComments: true });
  143. } while (currentToken && astUtils.isCommentToken(currentToken));
  144. if (currentToken && astUtils.isTokenOnSameLine(token, currentToken)) {
  145. return true;
  146. }
  147. return false;
  148. }
  149. /**
  150. * Returns whether or not comments are inside a node type or not.
  151. * @param {ASTNode} parent The Comment parent node.
  152. * @param {string} nodeType The parent type to check against.
  153. * @returns {boolean} True if the comment is inside nodeType.
  154. */
  155. function isParentNodeType(parent, nodeType) {
  156. return parent.type === nodeType ||
  157. (parent.body && parent.body.type === nodeType) ||
  158. (parent.consequent && parent.consequent.type === nodeType);
  159. }
  160. /**
  161. * Returns the parent node that contains the given token.
  162. * @param {token} token The token to check.
  163. * @returns {ASTNode} The parent node that contains the given token.
  164. */
  165. function getParentNodeOfToken(token) {
  166. return sourceCode.getNodeByRangeIndex(token.range[0]);
  167. }
  168. /**
  169. * Returns whether or not comments are at the parent start or not.
  170. * @param {token} token The Comment token.
  171. * @param {string} nodeType The parent type to check against.
  172. * @returns {boolean} True if the comment is at parent start.
  173. */
  174. function isCommentAtParentStart(token, nodeType) {
  175. const parent = getParentNodeOfToken(token);
  176. return parent && isParentNodeType(parent, nodeType) &&
  177. token.loc.start.line - parent.loc.start.line === 1;
  178. }
  179. /**
  180. * Returns whether or not comments are at the parent end or not.
  181. * @param {token} token The Comment token.
  182. * @param {string} nodeType The parent type to check against.
  183. * @returns {boolean} True if the comment is at parent end.
  184. */
  185. function isCommentAtParentEnd(token, nodeType) {
  186. const parent = getParentNodeOfToken(token);
  187. return parent && isParentNodeType(parent, nodeType) &&
  188. parent.loc.end.line - token.loc.end.line === 1;
  189. }
  190. /**
  191. * Returns whether or not comments are at the block start or not.
  192. * @param {token} token The Comment token.
  193. * @returns {boolean} True if the comment is at block start.
  194. */
  195. function isCommentAtBlockStart(token) {
  196. return isCommentAtParentStart(token, "ClassBody") || isCommentAtParentStart(token, "BlockStatement") || isCommentAtParentStart(token, "SwitchCase");
  197. }
  198. /**
  199. * Returns whether or not comments are at the block end or not.
  200. * @param {token} token The Comment token.
  201. * @returns {boolean} True if the comment is at block end.
  202. */
  203. function isCommentAtBlockEnd(token) {
  204. return isCommentAtParentEnd(token, "ClassBody") || isCommentAtParentEnd(token, "BlockStatement") || isCommentAtParentEnd(token, "SwitchCase") || isCommentAtParentEnd(token, "SwitchStatement");
  205. }
  206. /**
  207. * Returns whether or not comments are at the class start or not.
  208. * @param {token} token The Comment token.
  209. * @returns {boolean} True if the comment is at class start.
  210. */
  211. function isCommentAtClassStart(token) {
  212. return isCommentAtParentStart(token, "ClassBody");
  213. }
  214. /**
  215. * Returns whether or not comments are at the class end or not.
  216. * @param {token} token The Comment token.
  217. * @returns {boolean} True if the comment is at class end.
  218. */
  219. function isCommentAtClassEnd(token) {
  220. return isCommentAtParentEnd(token, "ClassBody");
  221. }
  222. /**
  223. * Returns whether or not comments are at the object start or not.
  224. * @param {token} token The Comment token.
  225. * @returns {boolean} True if the comment is at object start.
  226. */
  227. function isCommentAtObjectStart(token) {
  228. return isCommentAtParentStart(token, "ObjectExpression") || isCommentAtParentStart(token, "ObjectPattern");
  229. }
  230. /**
  231. * Returns whether or not comments are at the object end or not.
  232. * @param {token} token The Comment token.
  233. * @returns {boolean} True if the comment is at object end.
  234. */
  235. function isCommentAtObjectEnd(token) {
  236. return isCommentAtParentEnd(token, "ObjectExpression") || isCommentAtParentEnd(token, "ObjectPattern");
  237. }
  238. /**
  239. * Returns whether or not comments are at the array start or not.
  240. * @param {token} token The Comment token.
  241. * @returns {boolean} True if the comment is at array start.
  242. */
  243. function isCommentAtArrayStart(token) {
  244. return isCommentAtParentStart(token, "ArrayExpression") || isCommentAtParentStart(token, "ArrayPattern");
  245. }
  246. /**
  247. * Returns whether or not comments are at the array end or not.
  248. * @param {token} token The Comment token.
  249. * @returns {boolean} True if the comment is at array end.
  250. */
  251. function isCommentAtArrayEnd(token) {
  252. return isCommentAtParentEnd(token, "ArrayExpression") || isCommentAtParentEnd(token, "ArrayPattern");
  253. }
  254. /**
  255. * Checks if a comment token has lines around it (ignores inline comments)
  256. * @param {token} token The Comment token.
  257. * @param {Object} opts Options to determine the newline.
  258. * @param {boolean} opts.after Should have a newline after this line.
  259. * @param {boolean} opts.before Should have a newline before this line.
  260. * @returns {void}
  261. */
  262. function checkForEmptyLine(token, opts) {
  263. if (applyDefaultIgnorePatterns && defaultIgnoreRegExp.test(token.value)) {
  264. return;
  265. }
  266. if (ignorePattern && customIgnoreRegExp.test(token.value)) {
  267. return;
  268. }
  269. let after = opts.after,
  270. before = opts.before;
  271. const prevLineNum = token.loc.start.line - 1,
  272. nextLineNum = token.loc.end.line + 1,
  273. commentIsNotAlone = codeAroundComment(token);
  274. const blockStartAllowed = options.allowBlockStart &&
  275. isCommentAtBlockStart(token) &&
  276. !(options.allowClassStart === false &&
  277. isCommentAtClassStart(token)),
  278. blockEndAllowed = options.allowBlockEnd && isCommentAtBlockEnd(token) && !(options.allowClassEnd === false && isCommentAtClassEnd(token)),
  279. classStartAllowed = options.allowClassStart && isCommentAtClassStart(token),
  280. classEndAllowed = options.allowClassEnd && isCommentAtClassEnd(token),
  281. objectStartAllowed = options.allowObjectStart && isCommentAtObjectStart(token),
  282. objectEndAllowed = options.allowObjectEnd && isCommentAtObjectEnd(token),
  283. arrayStartAllowed = options.allowArrayStart && isCommentAtArrayStart(token),
  284. arrayEndAllowed = options.allowArrayEnd && isCommentAtArrayEnd(token);
  285. const exceptionStartAllowed = blockStartAllowed || classStartAllowed || objectStartAllowed || arrayStartAllowed;
  286. const exceptionEndAllowed = blockEndAllowed || classEndAllowed || objectEndAllowed || arrayEndAllowed;
  287. // ignore top of the file and bottom of the file
  288. if (prevLineNum < 1) {
  289. before = false;
  290. }
  291. if (nextLineNum >= numLines) {
  292. after = false;
  293. }
  294. // we ignore all inline comments
  295. if (commentIsNotAlone) {
  296. return;
  297. }
  298. const previousTokenOrComment = sourceCode.getTokenBefore(token, { includeComments: true });
  299. const nextTokenOrComment = sourceCode.getTokenAfter(token, { includeComments: true });
  300. // check for newline before
  301. if (!exceptionStartAllowed && before && !commentAndEmptyLines.includes(prevLineNum) &&
  302. !(astUtils.isCommentToken(previousTokenOrComment) && astUtils.isTokenOnSameLine(previousTokenOrComment, token))) {
  303. const lineStart = token.range[0] - token.loc.start.column;
  304. const range = [lineStart, lineStart];
  305. context.report({
  306. node: token,
  307. messageId: "before",
  308. fix(fixer) {
  309. return fixer.insertTextBeforeRange(range, "\n");
  310. }
  311. });
  312. }
  313. // check for newline after
  314. if (!exceptionEndAllowed && after && !commentAndEmptyLines.includes(nextLineNum) &&
  315. !(astUtils.isCommentToken(nextTokenOrComment) && astUtils.isTokenOnSameLine(token, nextTokenOrComment))) {
  316. context.report({
  317. node: token,
  318. messageId: "after",
  319. fix(fixer) {
  320. return fixer.insertTextAfter(token, "\n");
  321. }
  322. });
  323. }
  324. }
  325. //--------------------------------------------------------------------------
  326. // Public
  327. //--------------------------------------------------------------------------
  328. return {
  329. Program() {
  330. comments.forEach(token => {
  331. if (token.type === "Line") {
  332. if (options.beforeLineComment || options.afterLineComment) {
  333. checkForEmptyLine(token, {
  334. after: options.afterLineComment,
  335. before: options.beforeLineComment
  336. });
  337. }
  338. } else if (token.type === "Block") {
  339. if (options.beforeBlockComment || options.afterBlockComment) {
  340. checkForEmptyLine(token, {
  341. after: options.afterBlockComment,
  342. before: options.beforeBlockComment
  343. });
  344. }
  345. }
  346. });
  347. }
  348. };
  349. }
  350. };