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-parens.js 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /**
  2. * @fileoverview Rule to require parens in arrow function arguments.
  3. * @author Jxck
  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: "require parentheses around arrow function arguments",
  18. category: "ECMAScript 6",
  19. recommended: false,
  20. url: "https://eslint.org/docs/rules/arrow-parens"
  21. },
  22. fixable: "code",
  23. schema: [
  24. {
  25. enum: ["always", "as-needed"]
  26. },
  27. {
  28. type: "object",
  29. properties: {
  30. requireForBlockBody: {
  31. type: "boolean"
  32. }
  33. },
  34. additionalProperties: false
  35. }
  36. ],
  37. messages: {
  38. unexpectedParens: "Unexpected parentheses around single function argument.",
  39. expectedParens: "Expected parentheses around arrow function argument.",
  40. unexpectedParensInline: "Unexpected parentheses around single function argument having a body with no curly braces.",
  41. expectedParensBlock: "Expected parentheses around arrow function argument having a body with curly braces."
  42. }
  43. },
  44. create(context) {
  45. const asNeeded = context.options[0] === "as-needed";
  46. const requireForBlockBody = asNeeded && context.options[1] && context.options[1].requireForBlockBody === true;
  47. const sourceCode = context.getSourceCode();
  48. /**
  49. * Determines whether a arrow function argument end with `)`
  50. * @param {ASTNode} node The arrow function node.
  51. * @returns {void}
  52. */
  53. function parens(node) {
  54. const isAsync = node.async;
  55. const firstTokenOfParam = sourceCode.getFirstToken(node, isAsync ? 1 : 0);
  56. /**
  57. * Remove the parenthesis around a parameter
  58. * @param {Fixer} fixer Fixer
  59. * @returns {string} fixed parameter
  60. */
  61. function fixParamsWithParenthesis(fixer) {
  62. const paramToken = sourceCode.getTokenAfter(firstTokenOfParam);
  63. /*
  64. * ES8 allows Trailing commas in function parameter lists and calls
  65. * https://github.com/eslint/eslint/issues/8834
  66. */
  67. const closingParenToken = sourceCode.getTokenAfter(paramToken, astUtils.isClosingParenToken);
  68. const asyncToken = isAsync ? sourceCode.getTokenBefore(firstTokenOfParam) : null;
  69. const shouldAddSpaceForAsync = asyncToken && (asyncToken.range[1] === firstTokenOfParam.range[0]);
  70. return fixer.replaceTextRange([
  71. firstTokenOfParam.range[0],
  72. closingParenToken.range[1]
  73. ], `${shouldAddSpaceForAsync ? " " : ""}${paramToken.value}`);
  74. }
  75. // "as-needed", { "requireForBlockBody": true }: x => x
  76. if (
  77. requireForBlockBody &&
  78. node.params.length === 1 &&
  79. node.params[0].type === "Identifier" &&
  80. !node.params[0].typeAnnotation &&
  81. node.body.type !== "BlockStatement" &&
  82. !node.returnType
  83. ) {
  84. if (astUtils.isOpeningParenToken(firstTokenOfParam)) {
  85. context.report({
  86. node,
  87. messageId: "unexpectedParensInline",
  88. fix: fixParamsWithParenthesis
  89. });
  90. }
  91. return;
  92. }
  93. if (
  94. requireForBlockBody &&
  95. node.body.type === "BlockStatement"
  96. ) {
  97. if (!astUtils.isOpeningParenToken(firstTokenOfParam)) {
  98. context.report({
  99. node,
  100. messageId: "expectedParensBlock",
  101. fix(fixer) {
  102. return fixer.replaceText(firstTokenOfParam, `(${firstTokenOfParam.value})`);
  103. }
  104. });
  105. }
  106. return;
  107. }
  108. // "as-needed": x => x
  109. if (asNeeded &&
  110. node.params.length === 1 &&
  111. node.params[0].type === "Identifier" &&
  112. !node.params[0].typeAnnotation &&
  113. !node.returnType
  114. ) {
  115. if (astUtils.isOpeningParenToken(firstTokenOfParam)) {
  116. context.report({
  117. node,
  118. messageId: "unexpectedParens",
  119. fix: fixParamsWithParenthesis
  120. });
  121. }
  122. return;
  123. }
  124. if (firstTokenOfParam.type === "Identifier") {
  125. const after = sourceCode.getTokenAfter(firstTokenOfParam);
  126. // (x) => x
  127. if (after.value !== ")") {
  128. context.report({
  129. node,
  130. messageId: "expectedParens",
  131. fix(fixer) {
  132. return fixer.replaceText(firstTokenOfParam, `(${firstTokenOfParam.value})`);
  133. }
  134. });
  135. }
  136. }
  137. }
  138. return {
  139. ArrowFunctionExpression: parens
  140. };
  141. }
  142. };