space-before-function-paren.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /**
  2. * @fileoverview Rule to validate spacing before function paren.
  3. * @author Mathias Schreck <https://github.com/lo1tuma>
  4. * @deprecated in ESLint v8.53.0
  5. */
  6. "use strict";
  7. //------------------------------------------------------------------------------
  8. // Requirements
  9. //------------------------------------------------------------------------------
  10. const astUtils = require("./utils/ast-utils");
  11. //------------------------------------------------------------------------------
  12. // Rule Definition
  13. //------------------------------------------------------------------------------
  14. /** @type {import('../types').Rule.RuleModule} */
  15. module.exports = {
  16. meta: {
  17. deprecated: {
  18. message: "Formatting rules are being moved out of ESLint core.",
  19. url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/",
  20. deprecatedSince: "8.53.0",
  21. availableUntil: "11.0.0",
  22. replacedBy: [
  23. {
  24. message:
  25. "ESLint Stylistic now maintains deprecated stylistic core rules.",
  26. url: "https://eslint.style/guide/migration",
  27. plugin: {
  28. name: "@stylistic/eslint-plugin",
  29. url: "https://eslint.style",
  30. },
  31. rule: {
  32. name: "space-before-function-paren",
  33. url: "https://eslint.style/rules/space-before-function-paren",
  34. },
  35. },
  36. ],
  37. },
  38. type: "layout",
  39. docs: {
  40. description:
  41. "Enforce consistent spacing before `function` definition opening parenthesis",
  42. recommended: false,
  43. url: "https://eslint.org/docs/latest/rules/space-before-function-paren",
  44. },
  45. fixable: "whitespace",
  46. schema: [
  47. {
  48. oneOf: [
  49. {
  50. enum: ["always", "never"],
  51. },
  52. {
  53. type: "object",
  54. properties: {
  55. anonymous: {
  56. enum: ["always", "never", "ignore"],
  57. },
  58. named: {
  59. enum: ["always", "never", "ignore"],
  60. },
  61. asyncArrow: {
  62. enum: ["always", "never", "ignore"],
  63. },
  64. },
  65. additionalProperties: false,
  66. },
  67. ],
  68. },
  69. ],
  70. messages: {
  71. unexpectedSpace: "Unexpected space before function parentheses.",
  72. missingSpace: "Missing space before function parentheses.",
  73. },
  74. },
  75. create(context) {
  76. const sourceCode = context.sourceCode;
  77. const baseConfig =
  78. typeof context.options[0] === "string"
  79. ? context.options[0]
  80. : "always";
  81. const overrideConfig =
  82. typeof context.options[0] === "object" ? context.options[0] : {};
  83. /**
  84. * Determines whether a function has a name.
  85. * @param {ASTNode} node The function node.
  86. * @returns {boolean} Whether the function has a name.
  87. */
  88. function isNamedFunction(node) {
  89. if (node.id) {
  90. return true;
  91. }
  92. const parent = node.parent;
  93. return (
  94. parent.type === "MethodDefinition" ||
  95. (parent.type === "Property" &&
  96. (parent.kind === "get" ||
  97. parent.kind === "set" ||
  98. parent.method))
  99. );
  100. }
  101. /**
  102. * Gets the config for a given function
  103. * @param {ASTNode} node The function node
  104. * @returns {string} "always", "never", or "ignore"
  105. */
  106. function getConfigForFunction(node) {
  107. if (node.type === "ArrowFunctionExpression") {
  108. // Always ignore non-async functions and arrow functions without parens, e.g. async foo => bar
  109. if (
  110. node.async &&
  111. astUtils.isOpeningParenToken(
  112. sourceCode.getFirstToken(node, { skip: 1 }),
  113. )
  114. ) {
  115. return overrideConfig.asyncArrow || baseConfig;
  116. }
  117. } else if (isNamedFunction(node)) {
  118. return overrideConfig.named || baseConfig;
  119. // `generator-star-spacing` should warn anonymous generators. E.g. `function* () {}`
  120. } else if (!node.generator) {
  121. return overrideConfig.anonymous || baseConfig;
  122. }
  123. return "ignore";
  124. }
  125. /**
  126. * Checks the parens of a function node
  127. * @param {ASTNode} node A function node
  128. * @returns {void}
  129. */
  130. function checkFunction(node) {
  131. const functionConfig = getConfigForFunction(node);
  132. if (functionConfig === "ignore") {
  133. return;
  134. }
  135. const rightToken = sourceCode.getFirstToken(
  136. node,
  137. astUtils.isOpeningParenToken,
  138. );
  139. const leftToken = sourceCode.getTokenBefore(rightToken);
  140. const hasSpacing = sourceCode.isSpaceBetweenTokens(
  141. leftToken,
  142. rightToken,
  143. );
  144. if (hasSpacing && functionConfig === "never") {
  145. context.report({
  146. node,
  147. loc: {
  148. start: leftToken.loc.end,
  149. end: rightToken.loc.start,
  150. },
  151. messageId: "unexpectedSpace",
  152. fix(fixer) {
  153. const comments =
  154. sourceCode.getCommentsBefore(rightToken);
  155. // Don't fix anything if there's a single line comment between the left and the right token
  156. if (comments.some(comment => comment.type === "Line")) {
  157. return null;
  158. }
  159. return fixer.replaceTextRange(
  160. [leftToken.range[1], rightToken.range[0]],
  161. comments.reduce(
  162. (text, comment) =>
  163. text + sourceCode.getText(comment),
  164. "",
  165. ),
  166. );
  167. },
  168. });
  169. } else if (!hasSpacing && functionConfig === "always") {
  170. context.report({
  171. node,
  172. loc: rightToken.loc,
  173. messageId: "missingSpace",
  174. fix: fixer => fixer.insertTextAfter(leftToken, " "),
  175. });
  176. }
  177. }
  178. return {
  179. ArrowFunctionExpression: checkFunction,
  180. FunctionDeclaration: checkFunction,
  181. FunctionExpression: checkFunction,
  182. };
  183. },
  184. };