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.

function-paren-newline.js 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /**
  2. * @fileoverview enforce consistent line breaks inside function parentheses
  3. * @author Teddy Katz
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const astUtils = require("../util/ast-utils");
  10. //------------------------------------------------------------------------------
  11. // Rule Definition
  12. //------------------------------------------------------------------------------
  13. module.exports = {
  14. meta: {
  15. type: "layout",
  16. docs: {
  17. description: "enforce consistent line breaks inside function parentheses",
  18. category: "Stylistic Issues",
  19. recommended: false,
  20. url: "https://eslint.org/docs/rules/function-paren-newline"
  21. },
  22. fixable: "whitespace",
  23. schema: [
  24. {
  25. oneOf: [
  26. {
  27. enum: ["always", "never", "consistent", "multiline"]
  28. },
  29. {
  30. type: "object",
  31. properties: {
  32. minItems: {
  33. type: "integer",
  34. minimum: 0
  35. }
  36. },
  37. additionalProperties: false
  38. }
  39. ]
  40. }
  41. ],
  42. messages: {
  43. expectedBefore: "Expected newline before ')'.",
  44. expectedAfter: "Expected newline after '('.",
  45. unexpectedBefore: "Unexpected newline before '('.",
  46. unexpectedAfter: "Unexpected newline after ')'."
  47. }
  48. },
  49. create(context) {
  50. const sourceCode = context.getSourceCode();
  51. const rawOption = context.options[0] || "multiline";
  52. const multilineOption = rawOption === "multiline";
  53. const consistentOption = rawOption === "consistent";
  54. let minItems;
  55. if (typeof rawOption === "object") {
  56. minItems = rawOption.minItems;
  57. } else if (rawOption === "always") {
  58. minItems = 0;
  59. } else if (rawOption === "never") {
  60. minItems = Infinity;
  61. } else {
  62. minItems = null;
  63. }
  64. //----------------------------------------------------------------------
  65. // Helpers
  66. //----------------------------------------------------------------------
  67. /**
  68. * Determines whether there should be newlines inside function parens
  69. * @param {ASTNode[]} elements The arguments or parameters in the list
  70. * @param {boolean} hasLeftNewline `true` if the left paren has a newline in the current code.
  71. * @returns {boolean} `true` if there should be newlines inside the function parens
  72. */
  73. function shouldHaveNewlines(elements, hasLeftNewline) {
  74. if (multilineOption) {
  75. return elements.some((element, index) => index !== elements.length - 1 && element.loc.end.line !== elements[index + 1].loc.start.line);
  76. }
  77. if (consistentOption) {
  78. return hasLeftNewline;
  79. }
  80. return elements.length >= minItems;
  81. }
  82. /**
  83. * Validates a list of arguments or parameters
  84. * @param {Object} parens An object with keys `leftParen` for the left paren token, and `rightParen` for the right paren token
  85. * @param {ASTNode[]} elements The arguments or parameters in the list
  86. * @returns {void}
  87. */
  88. function validateParens(parens, elements) {
  89. const leftParen = parens.leftParen;
  90. const rightParen = parens.rightParen;
  91. const tokenAfterLeftParen = sourceCode.getTokenAfter(leftParen);
  92. const tokenBeforeRightParen = sourceCode.getTokenBefore(rightParen);
  93. const hasLeftNewline = !astUtils.isTokenOnSameLine(leftParen, tokenAfterLeftParen);
  94. const hasRightNewline = !astUtils.isTokenOnSameLine(tokenBeforeRightParen, rightParen);
  95. const needsNewlines = shouldHaveNewlines(elements, hasLeftNewline);
  96. if (hasLeftNewline && !needsNewlines) {
  97. context.report({
  98. node: leftParen,
  99. messageId: "unexpectedAfter",
  100. fix(fixer) {
  101. return sourceCode.getText().slice(leftParen.range[1], tokenAfterLeftParen.range[0]).trim()
  102. // If there is a comment between the ( and the first element, don't do a fix.
  103. ? null
  104. : fixer.removeRange([leftParen.range[1], tokenAfterLeftParen.range[0]]);
  105. }
  106. });
  107. } else if (!hasLeftNewline && needsNewlines) {
  108. context.report({
  109. node: leftParen,
  110. messageId: "expectedAfter",
  111. fix: fixer => fixer.insertTextAfter(leftParen, "\n")
  112. });
  113. }
  114. if (hasRightNewline && !needsNewlines) {
  115. context.report({
  116. node: rightParen,
  117. messageId: "unexpectedBefore",
  118. fix(fixer) {
  119. return sourceCode.getText().slice(tokenBeforeRightParen.range[1], rightParen.range[0]).trim()
  120. // If there is a comment between the last element and the ), don't do a fix.
  121. ? null
  122. : fixer.removeRange([tokenBeforeRightParen.range[1], rightParen.range[0]]);
  123. }
  124. });
  125. } else if (!hasRightNewline && needsNewlines) {
  126. context.report({
  127. node: rightParen,
  128. messageId: "expectedBefore",
  129. fix: fixer => fixer.insertTextBefore(rightParen, "\n")
  130. });
  131. }
  132. }
  133. /**
  134. * Gets the left paren and right paren tokens of a node.
  135. * @param {ASTNode} node The node with parens
  136. * @returns {Object} An object with keys `leftParen` for the left paren token, and `rightParen` for the right paren token.
  137. * Can also return `null` if an expression has no parens (e.g. a NewExpression with no arguments, or an ArrowFunctionExpression
  138. * with a single parameter)
  139. */
  140. function getParenTokens(node) {
  141. switch (node.type) {
  142. case "NewExpression":
  143. if (!node.arguments.length && !(
  144. astUtils.isOpeningParenToken(sourceCode.getLastToken(node, { skip: 1 })) &&
  145. astUtils.isClosingParenToken(sourceCode.getLastToken(node))
  146. )) {
  147. // If the NewExpression does not have parens (e.g. `new Foo`), return null.
  148. return null;
  149. }
  150. // falls through
  151. case "CallExpression":
  152. return {
  153. leftParen: sourceCode.getTokenAfter(node.callee, astUtils.isOpeningParenToken),
  154. rightParen: sourceCode.getLastToken(node)
  155. };
  156. case "FunctionDeclaration":
  157. case "FunctionExpression": {
  158. const leftParen = sourceCode.getFirstToken(node, astUtils.isOpeningParenToken);
  159. const rightParen = node.params.length
  160. ? sourceCode.getTokenAfter(node.params[node.params.length - 1], astUtils.isClosingParenToken)
  161. : sourceCode.getTokenAfter(leftParen);
  162. return { leftParen, rightParen };
  163. }
  164. case "ArrowFunctionExpression": {
  165. const firstToken = sourceCode.getFirstToken(node);
  166. if (!astUtils.isOpeningParenToken(firstToken)) {
  167. // If the ArrowFunctionExpression has a single param without parens, return null.
  168. return null;
  169. }
  170. return {
  171. leftParen: firstToken,
  172. rightParen: sourceCode.getTokenBefore(node.body, astUtils.isClosingParenToken)
  173. };
  174. }
  175. default:
  176. throw new TypeError(`unexpected node with type ${node.type}`);
  177. }
  178. }
  179. /**
  180. * Validates the parentheses for a node
  181. * @param {ASTNode} node The node with parens
  182. * @returns {void}
  183. */
  184. function validateNode(node) {
  185. const parens = getParenTokens(node);
  186. if (parens) {
  187. validateParens(parens, astUtils.isFunction(node) ? node.params : node.arguments);
  188. }
  189. }
  190. //----------------------------------------------------------------------
  191. // Public
  192. //----------------------------------------------------------------------
  193. return {
  194. ArrowFunctionExpression: validateNode,
  195. CallExpression: validateNode,
  196. FunctionDeclaration: validateNode,
  197. FunctionExpression: validateNode,
  198. NewExpression: validateNode
  199. };
  200. }
  201. };