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.

wrap-iife.js 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /**
  2. * @fileoverview Rule to flag when IIFE is not wrapped in parens
  3. * @author Ilya Volodin
  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 immediate `function` invocations",
  18. category: "Best Practices",
  19. recommended: false,
  20. url: "https://eslint.org/docs/rules/wrap-iife"
  21. },
  22. schema: [
  23. {
  24. enum: ["outside", "inside", "any"]
  25. },
  26. {
  27. type: "object",
  28. properties: {
  29. functionPrototypeMethods: {
  30. type: "boolean"
  31. }
  32. },
  33. additionalProperties: false
  34. }
  35. ],
  36. fixable: "code"
  37. },
  38. create(context) {
  39. const style = context.options[0] || "outside";
  40. const includeFunctionPrototypeMethods = (context.options[1] && context.options[1].functionPrototypeMethods) || false;
  41. const sourceCode = context.getSourceCode();
  42. /**
  43. * Check if the node is wrapped in ()
  44. * @param {ASTNode} node node to evaluate
  45. * @returns {boolean} True if it is wrapped
  46. * @private
  47. */
  48. function wrapped(node) {
  49. return astUtils.isParenthesised(sourceCode, node);
  50. }
  51. /**
  52. * Get the function node from an IIFE
  53. * @param {ASTNode} node node to evaluate
  54. * @returns {ASTNode} node that is the function expression of the given IIFE, or null if none exist
  55. */
  56. function getFunctionNodeFromIIFE(node) {
  57. const callee = node.callee;
  58. if (callee.type === "FunctionExpression") {
  59. return callee;
  60. }
  61. if (includeFunctionPrototypeMethods &&
  62. callee.type === "MemberExpression" &&
  63. callee.object.type === "FunctionExpression" &&
  64. (astUtils.getStaticPropertyName(callee) === "call" || astUtils.getStaticPropertyName(callee) === "apply")
  65. ) {
  66. return callee.object;
  67. }
  68. return null;
  69. }
  70. return {
  71. CallExpression(node) {
  72. const innerNode = getFunctionNodeFromIIFE(node);
  73. if (!innerNode) {
  74. return;
  75. }
  76. const callExpressionWrapped = wrapped(node),
  77. functionExpressionWrapped = wrapped(innerNode);
  78. if (!callExpressionWrapped && !functionExpressionWrapped) {
  79. context.report({
  80. node,
  81. message: "Wrap an immediate function invocation in parentheses.",
  82. fix(fixer) {
  83. const nodeToSurround = style === "inside" ? innerNode : node;
  84. return fixer.replaceText(nodeToSurround, `(${sourceCode.getText(nodeToSurround)})`);
  85. }
  86. });
  87. } else if (style === "inside" && !functionExpressionWrapped) {
  88. context.report({
  89. node,
  90. message: "Wrap only the function expression in parens.",
  91. fix(fixer) {
  92. /*
  93. * The outer call expression will always be wrapped at this point.
  94. * Replace the range between the end of the function expression and the end of the call expression.
  95. * for example, in `(function(foo) {}(bar))`, the range `(bar))` should get replaced with `)(bar)`.
  96. * Replace the parens from the outer expression, and parenthesize the function expression.
  97. */
  98. const parenAfter = sourceCode.getTokenAfter(node);
  99. return fixer.replaceTextRange(
  100. [innerNode.range[1], parenAfter.range[1]],
  101. `)${sourceCode.getText().slice(innerNode.range[1], parenAfter.range[0])}`
  102. );
  103. }
  104. });
  105. } else if (style === "outside" && !callExpressionWrapped) {
  106. context.report({
  107. node,
  108. message: "Move the invocation into the parens that contain the function.",
  109. fix(fixer) {
  110. /*
  111. * The inner function expression will always be wrapped at this point.
  112. * It's only necessary to replace the range between the end of the function expression
  113. * and the call expression. For example, in `(function(foo) {})(bar)`, the range `)(bar)`
  114. * should get replaced with `(bar))`.
  115. */
  116. const parenAfter = sourceCode.getTokenAfter(innerNode);
  117. return fixer.replaceTextRange(
  118. [parenAfter.range[0], node.range[1]],
  119. `${sourceCode.getText().slice(parenAfter.range[1], node.range[1])})`
  120. );
  121. }
  122. });
  123. }
  124. }
  125. };
  126. }
  127. };