2 * @fileoverview Rule to flag when IIFE is not wrapped in parens
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const astUtils
= require("./utils/ast-utils");
13 const eslintUtils
= require("eslint-utils");
15 //----------------------------------------------------------------------
17 //----------------------------------------------------------------------
20 * Check if the given node is callee of a `NewExpression` node
21 * @param {ASTNode} node node to check
22 * @returns {boolean} True if the node is callee of a `NewExpression` node
25 function isCalleeOfNewExpression(node
) {
26 const maybeCallee
= node
.parent
.type
=== "ChainExpression"
31 maybeCallee
.parent
.type
=== "NewExpression" &&
32 maybeCallee
.parent
.callee
=== maybeCallee
36 //------------------------------------------------------------------------------
38 //------------------------------------------------------------------------------
40 /** @type {import('../shared/types').Rule} */
46 description
: "require parentheses around immediate `function` invocations",
48 url
: "https://eslint.org/docs/rules/wrap-iife"
53 enum: ["outside", "inside", "any"]
58 functionPrototypeMethods
: {
63 additionalProperties
: false
69 wrapInvocation
: "Wrap an immediate function invocation in parentheses.",
70 wrapExpression
: "Wrap only the function expression in parens.",
71 moveInvocation
: "Move the invocation into the parens that contain the function."
77 const style
= context
.options
[0] || "outside";
78 const includeFunctionPrototypeMethods
= context
.options
[1] && context
.options
[1].functionPrototypeMethods
;
80 const sourceCode
= context
.getSourceCode();
83 * Check if the node is wrapped in any (). All parens count: grouping parens and parens for constructs such as if()
84 * @param {ASTNode} node node to evaluate
85 * @returns {boolean} True if it is wrapped in any parens
88 function isWrappedInAnyParens(node
) {
89 return astUtils
.isParenthesised(sourceCode
, node
);
93 * Check if the node is wrapped in grouping (). Parens for constructs such as if() don't count
94 * @param {ASTNode} node node to evaluate
95 * @returns {boolean} True if it is wrapped in grouping parens
98 function isWrappedInGroupingParens(node
) {
99 return eslintUtils
.isParenthesized(1, node
, sourceCode
);
103 * Get the function node from an IIFE
104 * @param {ASTNode} node node to evaluate
105 * @returns {ASTNode} node that is the function expression of the given IIFE, or null if none exist
107 function getFunctionNodeFromIIFE(node
) {
108 const callee
= astUtils
.skipChainExpression(node
.callee
);
110 if (callee
.type
=== "FunctionExpression") {
114 if (includeFunctionPrototypeMethods
&&
115 callee
.type
=== "MemberExpression" &&
116 callee
.object
.type
=== "FunctionExpression" &&
117 (astUtils
.getStaticPropertyName(callee
) === "call" || astUtils
.getStaticPropertyName(callee
) === "apply")
119 return callee
.object
;
127 CallExpression(node
) {
128 const innerNode
= getFunctionNodeFromIIFE(node
);
134 const isCallExpressionWrapped
= isWrappedInAnyParens(node
),
135 isFunctionExpressionWrapped
= isWrappedInAnyParens(innerNode
);
137 if (!isCallExpressionWrapped
&& !isFunctionExpressionWrapped
) {
140 messageId
: "wrapInvocation",
142 const nodeToSurround
= style
=== "inside" ? innerNode
: node
;
144 return fixer
.replaceText(nodeToSurround
, `(${sourceCode.getText(nodeToSurround)})`);
147 } else if (style
=== "inside" && !isFunctionExpressionWrapped
) {
150 messageId
: "wrapExpression",
153 // The outer call expression will always be wrapped at this point.
155 if (isWrappedInGroupingParens(node
) && !isCalleeOfNewExpression(node
)) {
158 * Parenthesize the function expression and remove unnecessary grouping parens around the call expression.
159 * Replace the range between the end of the function expression and the end of the call expression.
160 * for example, in `(function(foo) {}(bar))`, the range `(bar))` should get replaced with `)(bar)`.
163 const parenAfter
= sourceCode
.getTokenAfter(node
);
165 return fixer
.replaceTextRange(
166 [innerNode
.range
[1], parenAfter
.range
[1]],
167 `)${sourceCode.getText().slice(innerNode.range[1], parenAfter.range[0])}`
172 * Call expression is wrapped in mandatory parens such as if(), or in necessary grouping parens.
173 * These parens cannot be removed, so just parenthesize the function expression.
176 return fixer
.replaceText(innerNode
, `(${sourceCode.getText(innerNode)})`);
179 } else if (style
=== "outside" && !isCallExpressionWrapped
) {
182 messageId
: "moveInvocation",
186 * The inner function expression will always be wrapped at this point.
187 * It's only necessary to replace the range between the end of the function expression
188 * and the call expression. For example, in `(function(foo) {})(bar)`, the range `)(bar)`
189 * should get replaced with `(bar))`.
191 const parenAfter
= sourceCode
.getTokenAfter(innerNode
);
193 return fixer
.replaceTextRange(
194 [parenAfter
.range
[0], node
.range
[1]],
195 `${sourceCode.getText().slice(parenAfter.range[1], node.range[1])})`