项目原始demo,不改动
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
Repozitorijs ir arhivēts. Tam var aplūkot failus un to var klonēt, bet nevar iesūtīt jaunas izmaiņas, kā arī atvērt jaunas problēmas/izmaiņu pieprasījumus.

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