Files
SnowLuma/.pnpm-store/v11/files/b5/33075a01d2efecd4a466ffab91dae3a2812d03f114a1ffc87e3a567ff0be3f54084cf2cd0159ff9a9c7f3d346695ef402123e1d8e9f4ba760411c39bc2298f
T
TanTan 2a69b23b47
Dev Build / cleanup (push) Waiting to run
Dev Build / typecheck (push) Waiting to run
docs: 仓库链接指向本镜像仓库
2026-11-13 09:27:07 +08:00

82 lines
3.3 KiB
Plaintext

import { A as getStaticPropertyName, U as isParenthesised, Z as skipChainExpression, f as createRule, g as import_ast_utils } from "../utils.js";
function isCalleeOfNewExpression(node) {
const maybeCallee = node.parent?.type === "ChainExpression" ? node.parent : node;
return maybeCallee.parent?.type === "NewExpression" && maybeCallee.parent.callee === maybeCallee;
}
var wrap_iife_default = createRule({
name: "wrap-iife",
meta: {
type: "layout",
docs: { description: "Require parentheses around immediate `function` invocations" },
fixable: "code",
schema: [{
type: "string",
enum: [
"outside",
"inside",
"any"
]
}, {
type: "object",
properties: { functionPrototypeMethods: { type: "boolean" } },
additionalProperties: false
}],
defaultOptions: ["outside", { functionPrototypeMethods: false }],
messages: {
wrapInvocation: "Wrap an immediate function invocation in parentheses.",
wrapExpression: "Wrap only the function expression in parens.",
moveInvocation: "Move the invocation into the parens that contain the function."
}
},
create(context, [style, options]) {
const includeFunctionPrototypeMethods = options.functionPrototypeMethods;
const sourceCode = context.sourceCode;
function isWrappedInAnyParens(node) {
return isParenthesised(sourceCode, node);
}
function isWrappedInGroupingParens(node) {
return (0, import_ast_utils.isParenthesized)(node, sourceCode);
}
function getFunctionNodeFromIIFE(node) {
const callee = skipChainExpression(node.callee);
if (callee.type === "FunctionExpression") return callee;
if (includeFunctionPrototypeMethods && callee.type === "MemberExpression" && callee.object.type === "FunctionExpression" && (getStaticPropertyName(callee) === "call" || getStaticPropertyName(callee) === "apply")) return callee.object;
return null;
}
return { CallExpression(node) {
const innerNode = getFunctionNodeFromIIFE(node);
if (!innerNode) return;
const isCallExpressionWrapped = isWrappedInAnyParens(node);
const isFunctionExpressionWrapped = isWrappedInAnyParens(innerNode);
if (!isCallExpressionWrapped && !isFunctionExpressionWrapped) context.report({
node,
messageId: "wrapInvocation",
fix(fixer) {
const nodeToSurround = style === "inside" ? innerNode : node;
return fixer.replaceText(nodeToSurround, `(${sourceCode.getText(nodeToSurround)})`);
}
});
else if (style === "inside" && !isFunctionExpressionWrapped) context.report({
node,
messageId: "wrapExpression",
fix(fixer) {
if (isWrappedInGroupingParens(node) && !isCalleeOfNewExpression(node)) {
const parenAfter = sourceCode.getTokenAfter(node);
return fixer.replaceTextRange([innerNode.range[1], parenAfter.range[1]], `)${sourceCode.getText().slice(innerNode.range[1], parenAfter.range[0])}`);
}
return fixer.replaceText(innerNode, `(${sourceCode.getText(innerNode)})`);
}
});
else if (style === "outside" && !isCallExpressionWrapped) context.report({
node,
messageId: "moveInvocation",
fix(fixer) {
const parenAfter = sourceCode.getTokenAfter(innerNode);
return fixer.replaceTextRange([parenAfter.range[0], node.range[1]], `${sourceCode.getText().slice(parenAfter.range[1], node.range[1])})`);
}
});
} };
}
});
export { wrap_iife_default as t };