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.

arrow-body-style.js 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /**
  2. * @fileoverview Rule to require braces in arrow function body.
  3. * @author Alberto Rodríguez
  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: "suggestion",
  16. docs: {
  17. description: "require braces around arrow function bodies",
  18. category: "ECMAScript 6",
  19. recommended: false,
  20. url: "https://eslint.org/docs/rules/arrow-body-style"
  21. },
  22. schema: {
  23. anyOf: [
  24. {
  25. type: "array",
  26. items: [
  27. {
  28. enum: ["always", "never"]
  29. }
  30. ],
  31. minItems: 0,
  32. maxItems: 1
  33. },
  34. {
  35. type: "array",
  36. items: [
  37. {
  38. enum: ["as-needed"]
  39. },
  40. {
  41. type: "object",
  42. properties: {
  43. requireReturnForObjectLiteral: { type: "boolean" }
  44. },
  45. additionalProperties: false
  46. }
  47. ],
  48. minItems: 0,
  49. maxItems: 2
  50. }
  51. ]
  52. },
  53. fixable: "code",
  54. messages: {
  55. unexpectedOtherBlock: "Unexpected block statement surrounding arrow body.",
  56. unexpectedEmptyBlock: "Unexpected block statement surrounding arrow body; put a value of `undefined` immediately after the `=>`.",
  57. unexpectedObjectBlock: "Unexpected block statement surrounding arrow body; parenthesize the returned value and move it immediately after the `=>`.",
  58. unexpectedSingleBlock: "Unexpected block statement surrounding arrow body; move the returned value immediately after the `=>`.",
  59. expectedBlock: "Expected block statement surrounding arrow body."
  60. }
  61. },
  62. create(context) {
  63. const options = context.options;
  64. const always = options[0] === "always";
  65. const asNeeded = !options[0] || options[0] === "as-needed";
  66. const never = options[0] === "never";
  67. const requireReturnForObjectLiteral = options[1] && options[1].requireReturnForObjectLiteral;
  68. const sourceCode = context.getSourceCode();
  69. /**
  70. * Checks whether the given node has ASI problem or not.
  71. * @param {Token} token The token to check.
  72. * @returns {boolean} `true` if it changes semantics if `;` or `}` followed by the token are removed.
  73. */
  74. function hasASIProblem(token) {
  75. return token && token.type === "Punctuator" && /^[([/`+-]/.test(token.value);
  76. }
  77. /**
  78. * Gets the closing parenthesis which is the pair of the given opening parenthesis.
  79. * @param {Token} token The opening parenthesis token to get.
  80. * @returns {Token} The found closing parenthesis token.
  81. */
  82. function findClosingParen(token) {
  83. let node = sourceCode.getNodeByRangeIndex(token.range[1]);
  84. while (!astUtils.isParenthesised(sourceCode, node)) {
  85. node = node.parent;
  86. }
  87. return sourceCode.getTokenAfter(node);
  88. }
  89. /**
  90. * Determines whether a arrow function body needs braces
  91. * @param {ASTNode} node The arrow function node.
  92. * @returns {void}
  93. */
  94. function validate(node) {
  95. const arrowBody = node.body;
  96. if (arrowBody.type === "BlockStatement") {
  97. const blockBody = arrowBody.body;
  98. if (blockBody.length !== 1 && !never) {
  99. return;
  100. }
  101. if (asNeeded && requireReturnForObjectLiteral && blockBody[0].type === "ReturnStatement" &&
  102. blockBody[0].argument && blockBody[0].argument.type === "ObjectExpression") {
  103. return;
  104. }
  105. if (never || asNeeded && blockBody[0].type === "ReturnStatement") {
  106. let messageId;
  107. if (blockBody.length === 0) {
  108. messageId = "unexpectedEmptyBlock";
  109. } else if (blockBody.length > 1) {
  110. messageId = "unexpectedOtherBlock";
  111. } else if (blockBody[0].argument === null) {
  112. messageId = "unexpectedSingleBlock";
  113. } else if (astUtils.isOpeningBraceToken(sourceCode.getFirstToken(blockBody[0], { skip: 1 }))) {
  114. messageId = "unexpectedObjectBlock";
  115. } else {
  116. messageId = "unexpectedSingleBlock";
  117. }
  118. context.report({
  119. node,
  120. loc: arrowBody.loc.start,
  121. messageId,
  122. fix(fixer) {
  123. const fixes = [];
  124. if (blockBody.length !== 1 ||
  125. blockBody[0].type !== "ReturnStatement" ||
  126. !blockBody[0].argument ||
  127. hasASIProblem(sourceCode.getTokenAfter(arrowBody))
  128. ) {
  129. return fixes;
  130. }
  131. const openingBrace = sourceCode.getFirstToken(arrowBody);
  132. const closingBrace = sourceCode.getLastToken(arrowBody);
  133. const firstValueToken = sourceCode.getFirstToken(blockBody[0], 1);
  134. const lastValueToken = sourceCode.getLastToken(blockBody[0]);
  135. const commentsExist =
  136. sourceCode.commentsExistBetween(openingBrace, firstValueToken) ||
  137. sourceCode.commentsExistBetween(lastValueToken, closingBrace);
  138. /*
  139. * Remove tokens around the return value.
  140. * If comments don't exist, remove extra spaces as well.
  141. */
  142. if (commentsExist) {
  143. fixes.push(
  144. fixer.remove(openingBrace),
  145. fixer.remove(closingBrace),
  146. fixer.remove(sourceCode.getTokenAfter(openingBrace)) // return keyword
  147. );
  148. } else {
  149. fixes.push(
  150. fixer.removeRange([openingBrace.range[0], firstValueToken.range[0]]),
  151. fixer.removeRange([lastValueToken.range[1], closingBrace.range[1]])
  152. );
  153. }
  154. /*
  155. * If the first token of the reutrn value is `{`,
  156. * enclose the return value by parentheses to avoid syntax error.
  157. */
  158. if (astUtils.isOpeningBraceToken(firstValueToken)) {
  159. fixes.push(
  160. fixer.insertTextBefore(firstValueToken, "("),
  161. fixer.insertTextAfter(lastValueToken, ")")
  162. );
  163. }
  164. /*
  165. * If the last token of the return statement is semicolon, remove it.
  166. * Non-block arrow body is an expression, not a statement.
  167. */
  168. if (astUtils.isSemicolonToken(lastValueToken)) {
  169. fixes.push(fixer.remove(lastValueToken));
  170. }
  171. return fixes;
  172. }
  173. });
  174. }
  175. } else {
  176. if (always || (asNeeded && requireReturnForObjectLiteral && arrowBody.type === "ObjectExpression")) {
  177. context.report({
  178. node,
  179. loc: arrowBody.loc.start,
  180. messageId: "expectedBlock",
  181. fix(fixer) {
  182. const fixes = [];
  183. const arrowToken = sourceCode.getTokenBefore(arrowBody, astUtils.isArrowToken);
  184. const firstBodyToken = sourceCode.getTokenAfter(arrowToken);
  185. const lastBodyToken = sourceCode.getLastToken(node);
  186. const isParenthesisedObjectLiteral =
  187. astUtils.isOpeningParenToken(firstBodyToken) &&
  188. astUtils.isOpeningBraceToken(sourceCode.getTokenAfter(firstBodyToken));
  189. // Wrap the value by a block and a return statement.
  190. fixes.push(
  191. fixer.insertTextBefore(firstBodyToken, "{return "),
  192. fixer.insertTextAfter(lastBodyToken, "}")
  193. );
  194. // If the value is object literal, remove parentheses which were forced by syntax.
  195. if (isParenthesisedObjectLiteral) {
  196. fixes.push(
  197. fixer.remove(firstBodyToken),
  198. fixer.remove(findClosingParen(firstBodyToken))
  199. );
  200. }
  201. return fixes;
  202. }
  203. });
  204. }
  205. }
  206. }
  207. return {
  208. "ArrowFunctionExpression:exit": validate
  209. };
  210. }
  211. };