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.

newline-before-return.js 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /**
  2. * @fileoverview Rule to require newlines before `return` statement
  3. * @author Kai Cataldo
  4. * @deprecated
  5. */
  6. "use strict";
  7. //------------------------------------------------------------------------------
  8. // Rule Definition
  9. //------------------------------------------------------------------------------
  10. module.exports = {
  11. meta: {
  12. type: "layout",
  13. docs: {
  14. description: "require an empty line before `return` statements",
  15. category: "Stylistic Issues",
  16. recommended: false,
  17. url: "https://eslint.org/docs/rules/newline-before-return"
  18. },
  19. fixable: "whitespace",
  20. schema: [],
  21. messages: {
  22. expected: "Expected newline before return statement."
  23. },
  24. deprecated: true,
  25. replacedBy: ["padding-line-between-statements"]
  26. },
  27. create(context) {
  28. const sourceCode = context.getSourceCode();
  29. //--------------------------------------------------------------------------
  30. // Helpers
  31. //--------------------------------------------------------------------------
  32. /**
  33. * Tests whether node is preceded by supplied tokens
  34. * @param {ASTNode} node node to check
  35. * @param {Array} testTokens array of tokens to test against
  36. * @returns {boolean} Whether or not the node is preceded by one of the supplied tokens
  37. * @private
  38. */
  39. function isPrecededByTokens(node, testTokens) {
  40. const tokenBefore = sourceCode.getTokenBefore(node);
  41. return testTokens.some(token => tokenBefore.value === token);
  42. }
  43. /**
  44. * Checks whether node is the first node after statement or in block
  45. * @param {ASTNode} node node to check
  46. * @returns {boolean} Whether or not the node is the first node after statement or in block
  47. * @private
  48. */
  49. function isFirstNode(node) {
  50. const parentType = node.parent.type;
  51. if (node.parent.body) {
  52. return Array.isArray(node.parent.body)
  53. ? node.parent.body[0] === node
  54. : node.parent.body === node;
  55. }
  56. if (parentType === "IfStatement") {
  57. return isPrecededByTokens(node, ["else", ")"]);
  58. }
  59. if (parentType === "DoWhileStatement") {
  60. return isPrecededByTokens(node, ["do"]);
  61. }
  62. if (parentType === "SwitchCase") {
  63. return isPrecededByTokens(node, [":"]);
  64. }
  65. return isPrecededByTokens(node, [")"]);
  66. }
  67. /**
  68. * Returns the number of lines of comments that precede the node
  69. * @param {ASTNode} node node to check for overlapping comments
  70. * @param {number} lineNumTokenBefore line number of previous token, to check for overlapping comments
  71. * @returns {number} Number of lines of comments that precede the node
  72. * @private
  73. */
  74. function calcCommentLines(node, lineNumTokenBefore) {
  75. const comments = sourceCode.getCommentsBefore(node);
  76. let numLinesComments = 0;
  77. if (!comments.length) {
  78. return numLinesComments;
  79. }
  80. comments.forEach(comment => {
  81. numLinesComments++;
  82. if (comment.type === "Block") {
  83. numLinesComments += comment.loc.end.line - comment.loc.start.line;
  84. }
  85. // avoid counting lines with inline comments twice
  86. if (comment.loc.start.line === lineNumTokenBefore) {
  87. numLinesComments--;
  88. }
  89. if (comment.loc.end.line === node.loc.start.line) {
  90. numLinesComments--;
  91. }
  92. });
  93. return numLinesComments;
  94. }
  95. /**
  96. * Returns the line number of the token before the node that is passed in as an argument
  97. * @param {ASTNode} node The node to use as the start of the calculation
  98. * @returns {number} Line number of the token before `node`
  99. * @private
  100. */
  101. function getLineNumberOfTokenBefore(node) {
  102. const tokenBefore = sourceCode.getTokenBefore(node);
  103. let lineNumTokenBefore;
  104. /**
  105. * Global return (at the beginning of a script) is a special case.
  106. * If there is no token before `return`, then we expect no line
  107. * break before the return. Comments are allowed to occupy lines
  108. * before the global return, just no blank lines.
  109. * Setting lineNumTokenBefore to zero in that case results in the
  110. * desired behavior.
  111. */
  112. if (tokenBefore) {
  113. lineNumTokenBefore = tokenBefore.loc.end.line;
  114. } else {
  115. lineNumTokenBefore = 0; // global return at beginning of script
  116. }
  117. return lineNumTokenBefore;
  118. }
  119. /**
  120. * Checks whether node is preceded by a newline
  121. * @param {ASTNode} node node to check
  122. * @returns {boolean} Whether or not the node is preceded by a newline
  123. * @private
  124. */
  125. function hasNewlineBefore(node) {
  126. const lineNumNode = node.loc.start.line;
  127. const lineNumTokenBefore = getLineNumberOfTokenBefore(node);
  128. const commentLines = calcCommentLines(node, lineNumTokenBefore);
  129. return (lineNumNode - lineNumTokenBefore - commentLines) > 1;
  130. }
  131. /**
  132. * Checks whether it is safe to apply a fix to a given return statement.
  133. *
  134. * The fix is not considered safe if the given return statement has leading comments,
  135. * as we cannot safely determine if the newline should be added before or after the comments.
  136. * For more information, see: https://github.com/eslint/eslint/issues/5958#issuecomment-222767211
  137. * @param {ASTNode} node The return statement node to check.
  138. * @returns {boolean} `true` if it can fix the node.
  139. * @private
  140. */
  141. function canFix(node) {
  142. const leadingComments = sourceCode.getCommentsBefore(node);
  143. const lastLeadingComment = leadingComments[leadingComments.length - 1];
  144. const tokenBefore = sourceCode.getTokenBefore(node);
  145. if (leadingComments.length === 0) {
  146. return true;
  147. }
  148. /*
  149. * if the last leading comment ends in the same line as the previous token and
  150. * does not share a line with the `return` node, we can consider it safe to fix.
  151. * Example:
  152. * function a() {
  153. * var b; //comment
  154. * return;
  155. * }
  156. */
  157. if (lastLeadingComment.loc.end.line === tokenBefore.loc.end.line &&
  158. lastLeadingComment.loc.end.line !== node.loc.start.line) {
  159. return true;
  160. }
  161. return false;
  162. }
  163. //--------------------------------------------------------------------------
  164. // Public
  165. //--------------------------------------------------------------------------
  166. return {
  167. ReturnStatement(node) {
  168. if (!isFirstNode(node) && !hasNewlineBefore(node)) {
  169. context.report({
  170. node,
  171. messageId: "expected",
  172. fix(fixer) {
  173. if (canFix(node)) {
  174. const tokenBefore = sourceCode.getTokenBefore(node);
  175. const newlines = node.loc.start.line === tokenBefore.loc.end.line ? "\n\n" : "\n";
  176. return fixer.insertTextBefore(node, newlines);
  177. }
  178. return null;
  179. }
  180. });
  181. }
  182. }
  183. };
  184. }
  185. };