@autotelic/plumb 0.5.0 → 0.5.2
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/react/role.d.ts +23 -0
- package/dist/react/role.js +30 -0
- package/dist/react/rules/require-dot-notation-exports.d.ts +13 -6
- package/dist/react/rules/require-dot-notation-exports.js +38 -28
- package/dist/react/rules/require-guarded-context-hook.js +2 -9
- package/dist/react/rules/require-tripartite-context.js +2 -0
- package/dist/react/rules/use-encapsulation.d.ts +5 -2
- package/dist/react/rules/use-encapsulation.js +8 -4
- package/package.json +1 -1
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether a binding name denotes a context Provider (the generic Provider or a
|
|
3
|
+
* family-scoped XProvider). A Provider is the stateful root of a context family
|
|
4
|
+
* and the only component legitimately allowed to use React hooks inline.
|
|
5
|
+
*
|
|
6
|
+
* @param {string} name - The binding name.
|
|
7
|
+
* @returns {boolean} True when the name is `Provider` or ends with `Provider`.
|
|
8
|
+
*/
|
|
9
|
+
export declare function isProviderName(name: string): boolean;
|
|
10
|
+
/**
|
|
11
|
+
* Family name contributed by a useXxx export binding.
|
|
12
|
+
*
|
|
13
|
+
* @param {string} name - The exported binding name.
|
|
14
|
+
* @returns {string | null} The family name, or null when not a useXxx hook.
|
|
15
|
+
*/
|
|
16
|
+
export declare function hookFamily(name: string): string | null;
|
|
17
|
+
/**
|
|
18
|
+
* Whether a binding name denotes a custom hook (use + an uppercase letter).
|
|
19
|
+
*
|
|
20
|
+
* @param {string | null} name - The binding name, if known.
|
|
21
|
+
* @returns {name is string} True when the name starts with `use` + a capital.
|
|
22
|
+
*/
|
|
23
|
+
export declare function isHookName(name: string | null): name is string;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether a binding name denotes a context Provider (the generic Provider or a
|
|
3
|
+
* family-scoped XProvider). A Provider is the stateful root of a context family
|
|
4
|
+
* and the only component legitimately allowed to use React hooks inline.
|
|
5
|
+
*
|
|
6
|
+
* @param {string} name - The binding name.
|
|
7
|
+
* @returns {boolean} True when the name is `Provider` or ends with `Provider`.
|
|
8
|
+
*/
|
|
9
|
+
export function isProviderName(name) {
|
|
10
|
+
return name === "Provider" || name.endsWith("Provider");
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Family name contributed by a useXxx export binding.
|
|
14
|
+
*
|
|
15
|
+
* @param {string} name - The exported binding name.
|
|
16
|
+
* @returns {string | null} The family name, or null when not a useXxx hook.
|
|
17
|
+
*/
|
|
18
|
+
export function hookFamily(name) {
|
|
19
|
+
const match = /^use([A-Z]\w*)$/u.exec(name);
|
|
20
|
+
return match === null ? null : (match[1] ?? null);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Whether a binding name denotes a custom hook (use + an uppercase letter).
|
|
24
|
+
*
|
|
25
|
+
* @param {string | null} name - The binding name, if known.
|
|
26
|
+
* @returns {name is string} True when the name starts with `use` + a capital.
|
|
27
|
+
*/
|
|
28
|
+
export function isHookName(name) {
|
|
29
|
+
return name !== null && /^use[A-Z]/u.test(name);
|
|
30
|
+
}
|
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Whether a component family (a Provider
|
|
3
|
-
* must carry a dot-notation aggregate export.
|
|
2
|
+
* Whether a component family (a Provider plus a useXxx hook sharing one family
|
|
3
|
+
* name) must carry a dot-notation aggregate export. The useXxx hook is the
|
|
4
|
+
* family's consumption API and is exported separately by convention, so
|
|
5
|
+
* `aggregateOk` only asks whether the aggregate (export const Family = {
|
|
6
|
+
* Provider, ... }) exists and carries its Provider.
|
|
4
7
|
*
|
|
5
|
-
* @param {{
|
|
6
|
-
* @returns {boolean} True when an aggregate is required but absent
|
|
8
|
+
* @param {{ providerFamily: string | null; hookFamily: string; singleFamily: boolean; aggregateOk: boolean }} input - The family's export facts.
|
|
9
|
+
* @returns {boolean} True when an aggregate is required but absent.
|
|
7
10
|
*/
|
|
8
11
|
export declare function familyRequiresAggregate(input: {
|
|
9
|
-
|
|
10
|
-
|
|
12
|
+
providerFamily: string | null;
|
|
13
|
+
hookFamily: string;
|
|
14
|
+
singleFamily: boolean;
|
|
11
15
|
aggregateOk: boolean;
|
|
12
16
|
}): boolean;
|
|
13
17
|
/**
|
|
@@ -16,5 +20,8 @@ export declare function familyRequiresAggregate(input: {
|
|
|
16
20
|
* so consumers compose `Family.Provider` / `Family.Header` and the blocks are
|
|
17
21
|
* discoverable. Bare re-exports of `Provider`/`useXxx` without the aggregate
|
|
18
22
|
* hide the family boundary.
|
|
23
|
+
*
|
|
24
|
+
* Uses createOnce; per-file state is reset in before() (which oxlint calls per
|
|
25
|
+
* file), so a family seen in one file cannot leak into another.
|
|
19
26
|
*/
|
|
20
27
|
export declare const requireDotNotationExportsRule: import("@oxlint/plugins").Rule;
|
|
@@ -1,15 +1,21 @@
|
|
|
1
1
|
import { defineRule } from "@oxlint/plugins";
|
|
2
2
|
import { isString } from "../../shared/structural.js";
|
|
3
|
+
import { hookFamily, isProviderName } from "../role.js";
|
|
3
4
|
const TEST_FILE = /.(?:test|spec).[cm]?[jt]sx?$/u;
|
|
4
5
|
/**
|
|
5
|
-
* Whether a component family (a Provider
|
|
6
|
-
* must carry a dot-notation aggregate export.
|
|
6
|
+
* Whether a component family (a Provider plus a useXxx hook sharing one family
|
|
7
|
+
* name) must carry a dot-notation aggregate export. The useXxx hook is the
|
|
8
|
+
* family's consumption API and is exported separately by convention, so
|
|
9
|
+
* `aggregateOk` only asks whether the aggregate (export const Family = {
|
|
10
|
+
* Provider, ... }) exists and carries its Provider.
|
|
7
11
|
*
|
|
8
|
-
* @param {{
|
|
9
|
-
* @returns {boolean} True when an aggregate is required but absent
|
|
12
|
+
* @param {{ providerFamily: string | null; hookFamily: string; singleFamily: boolean; aggregateOk: boolean }} input - The family's export facts.
|
|
13
|
+
* @returns {boolean} True when an aggregate is required but absent.
|
|
10
14
|
*/
|
|
11
15
|
export function familyRequiresAggregate(input) {
|
|
12
|
-
|
|
16
|
+
const providerPresent = input.providerFamily === input.hookFamily ||
|
|
17
|
+
(input.providerFamily === "" && input.singleFamily);
|
|
18
|
+
return providerPresent && !input.aggregateOk;
|
|
13
19
|
}
|
|
14
20
|
/**
|
|
15
21
|
* Property key names of an object literal.
|
|
@@ -33,14 +39,18 @@ function objectPropKeys(node) {
|
|
|
33
39
|
return keys;
|
|
34
40
|
}
|
|
35
41
|
/**
|
|
36
|
-
*
|
|
42
|
+
* Whether an aggregate's keys include the Provider (the family's stateful root).
|
|
43
|
+
* The useXxx hook is the family's consumption API and is exported separately by
|
|
44
|
+
* convention, so it need not appear as a key of the aggregate.
|
|
37
45
|
*
|
|
38
|
-
* @param {string}
|
|
39
|
-
* @returns {
|
|
46
|
+
* @param {Set<string>} keys - The aggregate object's property keys.
|
|
47
|
+
* @returns {boolean} True when both the Provider and the hook are present.
|
|
40
48
|
*/
|
|
41
|
-
function
|
|
42
|
-
const
|
|
43
|
-
|
|
49
|
+
function hasProviderKey(keys) {
|
|
50
|
+
for (const k of keys)
|
|
51
|
+
if (k === "Provider" || /Provider$/u.test(k))
|
|
52
|
+
return true;
|
|
53
|
+
return false;
|
|
44
54
|
}
|
|
45
55
|
/**
|
|
46
56
|
* A component family (a Provider plus a `useXxx` consumer hook) must be exported
|
|
@@ -48,6 +58,9 @@ function hookFamily(name) {
|
|
|
48
58
|
* so consumers compose `Family.Provider` / `Family.Header` and the blocks are
|
|
49
59
|
* discoverable. Bare re-exports of `Provider`/`useXxx` without the aggregate
|
|
50
60
|
* hide the family boundary.
|
|
61
|
+
*
|
|
62
|
+
* Uses createOnce; per-file state is reset in before() (which oxlint calls per
|
|
63
|
+
* file), so a family seen in one file cannot leak into another.
|
|
51
64
|
*/
|
|
52
65
|
export const requireDotNotationExportsRule = defineRule({
|
|
53
66
|
meta: {
|
|
@@ -60,33 +73,26 @@ export const requireDotNotationExportsRule = defineRule({
|
|
|
60
73
|
},
|
|
61
74
|
},
|
|
62
75
|
createOnce(context) {
|
|
63
|
-
const
|
|
76
|
+
const providerBases = new Set();
|
|
64
77
|
const hookFamilies = new Set();
|
|
65
78
|
const aggregates = new Map();
|
|
66
79
|
let reportNode = null;
|
|
67
80
|
const recordName = (entry) => {
|
|
68
81
|
if (reportNode === null)
|
|
69
82
|
reportNode = entry.node;
|
|
70
|
-
if (entry.name
|
|
71
|
-
|
|
83
|
+
if (isProviderName(entry.name)) {
|
|
84
|
+
providerBases.add(entry.name === "Provider" ? "" : entry.name.slice(0, -"Provider".length));
|
|
85
|
+
}
|
|
72
86
|
const hf = hookFamily(entry.name);
|
|
73
87
|
if (hf !== null)
|
|
74
88
|
hookFamilies.add(hf);
|
|
75
89
|
};
|
|
76
|
-
/**
|
|
77
|
-
* Whether an aggregate's keys include the Provider and the useXxx hook.
|
|
78
|
-
*
|
|
79
|
-
* @param {Set<string>} keys - The aggregate object's property keys.
|
|
80
|
-
* @returns {boolean} True when both the Provider and the hook are present.
|
|
81
|
-
*/
|
|
82
|
-
function hasProviderKey(keys) {
|
|
83
|
-
for (const k of keys)
|
|
84
|
-
if (k === "Provider" || /Provider$/u.test(k))
|
|
85
|
-
return true;
|
|
86
|
-
return false;
|
|
87
|
-
}
|
|
88
90
|
return {
|
|
89
91
|
before() {
|
|
92
|
+
providerBases.clear();
|
|
93
|
+
hookFamilies.clear();
|
|
94
|
+
aggregates.clear();
|
|
95
|
+
reportNode = null;
|
|
90
96
|
if (TEST_FILE.test(context.filename.replaceAll("\\", "/")))
|
|
91
97
|
return false;
|
|
92
98
|
},
|
|
@@ -116,14 +122,18 @@ export const requireDotNotationExportsRule = defineRule({
|
|
|
116
122
|
}
|
|
117
123
|
},
|
|
118
124
|
"Program:exit"() {
|
|
119
|
-
if (
|
|
125
|
+
if (hookFamilies.size === 0)
|
|
120
126
|
return;
|
|
121
127
|
const node = reportNode;
|
|
122
128
|
if (node === null)
|
|
123
129
|
return;
|
|
130
|
+
const singleFamily = hookFamilies.size === 1;
|
|
124
131
|
for (const family of hookFamilies) {
|
|
132
|
+
const providerPresent = providerBases.has(family) || (providerBases.has("") && singleFamily);
|
|
133
|
+
if (!providerPresent)
|
|
134
|
+
continue;
|
|
125
135
|
const agg = aggregates.get(family);
|
|
126
|
-
const ok = agg !== undefined && hasProviderKey(agg)
|
|
136
|
+
const ok = agg !== undefined && hasProviderKey(agg);
|
|
127
137
|
if (ok)
|
|
128
138
|
continue;
|
|
129
139
|
context.report({ node, messageId: "missingAggregate", data: { family } });
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { defineRule } from "@oxlint/plugins";
|
|
2
2
|
import { ancestorsOf } from "../../shared/ancestors.js";
|
|
3
|
+
import { isHookName } from "../role.js";
|
|
3
4
|
const TEST_FILE = /.(?:test|spec).[cm]?[jt]sx?$/u;
|
|
4
5
|
/**
|
|
5
6
|
* Whether a custom context hook that reads context must be reported for
|
|
@@ -11,15 +12,6 @@ const TEST_FILE = /.(?:test|spec).[cm]?[jt]sx?$/u;
|
|
|
11
12
|
export function requiresContextGuard(input) {
|
|
12
13
|
return input.isHook && input.callsUseContext && !input.guarded;
|
|
13
14
|
}
|
|
14
|
-
/**
|
|
15
|
-
* Whether a function name denotes a custom hook.
|
|
16
|
-
*
|
|
17
|
-
* @param {string | null} name - The function's name, if known.
|
|
18
|
-
* @returns {name is string} True when the name starts with "use" + a capital.
|
|
19
|
-
*/
|
|
20
|
-
function isHookName(name) {
|
|
21
|
-
return name !== null && /^use[A-Z]/u.test(name);
|
|
22
|
-
}
|
|
23
15
|
/**
|
|
24
16
|
* The variable name an `if` test guards against a nullish value.
|
|
25
17
|
*
|
|
@@ -118,6 +110,7 @@ export const requireGuardedContextHookRule = defineRule({
|
|
|
118
110
|
}
|
|
119
111
|
return {
|
|
120
112
|
before() {
|
|
113
|
+
frames.length = 0;
|
|
121
114
|
if (TEST_FILE.test(context.filename.replaceAll("\\", "/")))
|
|
122
115
|
return false;
|
|
123
116
|
},
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Whether a React hook used at `hookName` inside a function named `parentName`
|
|
3
|
-
* violates the encapsulation rule
|
|
3
|
+
* violates the encapsulation rule. Provider components (XProvider) are the
|
|
4
|
+
* stateful root of a context family and legitimately use hooks inline, so they
|
|
5
|
+
* are exempt.
|
|
4
6
|
*
|
|
5
7
|
* @param {{ hookName: string; parentName: string }} input - The usage facts.
|
|
6
8
|
* @returns {boolean} True when the usage should be reported.
|
|
@@ -14,6 +16,7 @@ export declare function isEncapsulationViolation(input: {
|
|
|
14
16
|
* functionality into a custom hook (`useXxx`) and call that instead - the
|
|
15
17
|
* "useEncapsulation" pattern. A component that reaches for `useState`/`useEffect`
|
|
16
18
|
* inline hides its state and effects; a custom hook names them and can be tested
|
|
17
|
-
* and composed in isolation.
|
|
19
|
+
* and composed in isolation. Provider components (XProvider) are exempt: they are
|
|
20
|
+
* the stateful root of a context family and must use hooks inline.
|
|
18
21
|
*/
|
|
19
22
|
export declare const useEncapsulationRule: import("@oxlint/plugins").Rule;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { defineRule } from "@oxlint/plugins";
|
|
2
2
|
import { ancestorsOf } from "../../shared/ancestors.js";
|
|
3
3
|
import { readField } from "../../shared/structural.js";
|
|
4
|
+
import { isProviderName } from "../role.js";
|
|
4
5
|
const TEST_FILE = /.(?:test|spec).[cm]?[jt]sx?$/u;
|
|
5
6
|
/** Pattern identifying custom hooks (names starting with `use`). */
|
|
6
7
|
const HOOK_PATTERN = /^use/u;
|
|
@@ -24,20 +25,23 @@ const REACT_HOOKS = new Set([
|
|
|
24
25
|
]);
|
|
25
26
|
/**
|
|
26
27
|
* Whether a React hook used at `hookName` inside a function named `parentName`
|
|
27
|
-
* violates the encapsulation rule
|
|
28
|
+
* violates the encapsulation rule. Provider components (XProvider) are the
|
|
29
|
+
* stateful root of a context family and legitimately use hooks inline, so they
|
|
30
|
+
* are exempt.
|
|
28
31
|
*
|
|
29
32
|
* @param {{ hookName: string; parentName: string }} input - The usage facts.
|
|
30
33
|
* @returns {boolean} True when the usage should be reported.
|
|
31
34
|
*/
|
|
32
35
|
export function isEncapsulationViolation(input) {
|
|
33
|
-
return REACT_HOOKS.has(input.hookName) && !HOOK_PATTERN.test(input.parentName);
|
|
36
|
+
return REACT_HOOKS.has(input.hookName) && !HOOK_PATTERN.test(input.parentName) && !isProviderName(input.parentName);
|
|
34
37
|
}
|
|
35
38
|
/**
|
|
36
39
|
* Do not call React's hooks directly inside a component. Abstract the
|
|
37
40
|
* functionality into a custom hook (`useXxx`) and call that instead - the
|
|
38
41
|
* "useEncapsulation" pattern. A component that reaches for `useState`/`useEffect`
|
|
39
42
|
* inline hides its state and effects; a custom hook names them and can be tested
|
|
40
|
-
* and composed in isolation.
|
|
43
|
+
* and composed in isolation. Provider components (XProvider) are exempt: they are
|
|
44
|
+
* the stateful root of a context family and must use hooks inline.
|
|
41
45
|
*/
|
|
42
46
|
export const useEncapsulationRule = defineRule({
|
|
43
47
|
meta: {
|
|
@@ -84,7 +88,7 @@ export const useEncapsulationRule = defineRule({
|
|
|
84
88
|
if (!hooksToCheck.has(node.name))
|
|
85
89
|
return;
|
|
86
90
|
const parent = nearestFunctionName(node);
|
|
87
|
-
if (parent !== null && !HOOK_PATTERN.test(parent)) {
|
|
91
|
+
if (parent !== null && !HOOK_PATTERN.test(parent) && !isProviderName(parent)) {
|
|
88
92
|
context.report({ node, messageId: "noDirectHooks", data: { hook: node.name, parent } });
|
|
89
93
|
}
|
|
90
94
|
},
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@autotelic/plumb",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.2",
|
|
4
4
|
"description": "Opinionated Oxlint rules that keep TypeScript true: generic and Effect-specific patterns, maintained as one source of truth for the autotelic repos.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"files": [
|