pasika 0.10.35 → 0.10.36
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/eslint/pasika/index.js +49 -19
- package/package.json +1 -1
|
@@ -3875,40 +3875,70 @@ var hookComplexityRule = {
|
|
|
3875
3875
|
if (relative.startsWith("..")) return {};
|
|
3876
3876
|
const segments = relative.split(path28.sep);
|
|
3877
3877
|
const sourceText = context.sourceCode.text;
|
|
3878
|
-
|
|
3879
|
-
|
|
3880
|
-
|
|
3881
|
-
|
|
3882
|
-
|
|
3878
|
+
const index = getProjectIndex(sourceRoot);
|
|
3879
|
+
const localHookCalls = /* @__PURE__ */ new Set();
|
|
3880
|
+
const hooks = [];
|
|
3881
|
+
function registerHook(node, name, body, exported) {
|
|
3882
|
+
if (!name || !isHookName3(name) || !body) return;
|
|
3883
|
+
hooks.push({ node, name, body, exported });
|
|
3884
|
+
}
|
|
3885
|
+
function reportHooks() {
|
|
3883
3886
|
const parentFolder = segments.length >= 2 ? segments[segments.length - 2] : void 0;
|
|
3884
3887
|
const inSupportFolder = parentFolder === "hooks";
|
|
3885
|
-
|
|
3886
|
-
|
|
3887
|
-
|
|
3888
|
-
|
|
3889
|
-
|
|
3890
|
-
|
|
3891
|
-
|
|
3892
|
-
|
|
3893
|
-
|
|
3894
|
-
|
|
3888
|
+
for (const { node, name, body, exported } of hooks) {
|
|
3889
|
+
const score = computeExtractionScore(body, sourceText);
|
|
3890
|
+
const hasLocalConsumer = localHookCalls.has(name);
|
|
3891
|
+
const externalConsumerCount = exported ? index?.symbolConsumers.get(symbolKey(filename, name))?.size ?? 0 : 0;
|
|
3892
|
+
if (score >= 2) {
|
|
3893
|
+
if (!inSupportFolder && (exported || hasLocalConsumer)) {
|
|
3894
|
+
context.report({
|
|
3895
|
+
node,
|
|
3896
|
+
message: `Hook "${name}" has an extraction score of ${String(score)} and must be extracted to a hooks/ folder. See docs/next-codebase-guide/rules/hook-extraction-rule.md`
|
|
3897
|
+
});
|
|
3898
|
+
}
|
|
3899
|
+
continue;
|
|
3900
|
+
}
|
|
3901
|
+
const consumerCount = externalConsumerCount + (hasLocalConsumer ? 1 : 0);
|
|
3902
|
+
if (inSupportFolder && exported) {
|
|
3903
|
+
if (consumerCount < 2) {
|
|
3904
|
+
context.report({
|
|
3905
|
+
node,
|
|
3906
|
+
message: `Hook "${name}" has an extraction score below two and must be inlined directly into its sole consumer instead of remaining a custom hook. See docs/next-codebase-guide/rules/hook-extraction-rule.md`
|
|
3907
|
+
});
|
|
3908
|
+
}
|
|
3909
|
+
continue;
|
|
3910
|
+
}
|
|
3911
|
+
const isSingleLocalConsumer = hasLocalConsumer && externalConsumerCount === 0;
|
|
3912
|
+
if (isSingleLocalConsumer) {
|
|
3913
|
+
context.report({
|
|
3914
|
+
node,
|
|
3915
|
+
message: `Hook "${name}" has one local consumer and an extraction score below two; inline its React primitives and handlers directly into the consumer instead of keeping a custom hook. See docs/next-codebase-guide/rules/hook-extraction-rule.md`
|
|
3916
|
+
});
|
|
3917
|
+
}
|
|
3895
3918
|
}
|
|
3896
3919
|
}
|
|
3897
3920
|
return {
|
|
3898
3921
|
FunctionDeclaration(node) {
|
|
3899
3922
|
const exported = node.parent?.type === "ExportNamedDeclaration";
|
|
3900
|
-
|
|
3923
|
+
registerHook(node, node.id?.name, node.body, exported);
|
|
3901
3924
|
},
|
|
3902
3925
|
VariableDeclarator(node) {
|
|
3903
3926
|
if (node.id.type !== "Identifier") return;
|
|
3904
|
-
const exported = node.parent.parent?.type === "ExportNamedDeclaration";
|
|
3905
|
-
if (!exported) return;
|
|
3906
3927
|
const init = node.init;
|
|
3907
3928
|
if (!init || init.type !== "ArrowFunctionExpression" && init.type !== "FunctionExpression") {
|
|
3908
3929
|
return;
|
|
3909
3930
|
}
|
|
3910
3931
|
if (init.body.type !== "BlockStatement") return;
|
|
3911
|
-
|
|
3932
|
+
const exported = node.parent.parent?.type === "ExportNamedDeclaration";
|
|
3933
|
+
registerHook(node, node.id.name, init.body, exported);
|
|
3934
|
+
},
|
|
3935
|
+
CallExpression(node) {
|
|
3936
|
+
if (node.callee.type === "Identifier" && isHookName3(node.callee.name)) {
|
|
3937
|
+
localHookCalls.add(node.callee.name);
|
|
3938
|
+
}
|
|
3939
|
+
},
|
|
3940
|
+
"Program:exit"() {
|
|
3941
|
+
reportHooks();
|
|
3912
3942
|
}
|
|
3913
3943
|
};
|
|
3914
3944
|
}
|