pasika 0.3.1 → 0.3.3
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/README.md +67 -9
- package/dist/cli/index.js +34 -40
- package/dist/cli/output.d.ts +3 -0
- package/dist/cli/output.js +11 -0
- package/dist/enforcement/coverage.d.ts +5 -1
- package/dist/enforcement/coverage.js +68 -39
- package/dist/enforcement/doctor.d.ts +16 -0
- package/dist/enforcement/doctor.js +249 -0
- package/dist/enforcement/parse-docs.js +5 -1
- package/dist/enforcement/types.d.ts +0 -3
- package/dist/enforcement/types.js +4 -13
- package/dist/eslint/pasika/ast-types.d.ts +122 -0
- package/dist/eslint/pasika/ast-types.js +1 -0
- package/dist/eslint/pasika/index.d.ts +58 -3
- package/dist/eslint/pasika/index.js +61 -1
- package/dist/eslint/pasika/rules/application-structure.d.ts +2 -0
- package/dist/eslint/pasika/rules/application-structure.js +152 -0
- package/dist/eslint/pasika/rules/component-conventions.d.ts +16 -0
- package/dist/eslint/pasika/rules/component-conventions.js +145 -0
- package/dist/eslint/pasika/rules/component-nesting.d.ts +12 -0
- package/dist/eslint/pasika/rules/component-nesting.js +69 -0
- package/dist/eslint/pasika/rules/config-extraction.d.ts +14 -0
- package/dist/eslint/pasika/rules/config-extraction.js +74 -0
- package/dist/eslint/pasika/rules/cross-feature-import.d.ts +2 -0
- package/dist/eslint/pasika/rules/cross-feature-import.js +75 -0
- package/dist/eslint/pasika/rules/css/apply-usage.d.ts +10 -0
- package/dist/eslint/pasika/rules/css/apply-usage.js +45 -0
- package/dist/eslint/pasika/rules/css/base-layer-pair.d.ts +10 -0
- package/dist/eslint/pasika/rules/css/base-layer-pair.js +49 -0
- package/dist/eslint/pasika/rules/css/css-variable-naming.d.ts +10 -0
- package/dist/eslint/pasika/rules/css/css-variable-naming.js +39 -0
- package/dist/eslint/pasika/rules/css/custom-utility-apply.d.ts +11 -0
- package/dist/eslint/pasika/rules/css/custom-utility-apply.js +52 -0
- package/dist/eslint/pasika/rules/css/global-css-location.d.ts +10 -0
- package/dist/eslint/pasika/rules/css/global-css-location.js +42 -0
- package/dist/eslint/pasika/rules/css/helpers.d.ts +29 -0
- package/dist/eslint/pasika/rules/css/helpers.js +93 -0
- package/dist/eslint/pasika/rules/css/index.d.ts +13 -0
- package/dist/eslint/pasika/rules/css/index.js +25 -0
- package/dist/eslint/pasika/rules/css/root-variables.d.ts +11 -0
- package/dist/eslint/pasika/rules/css/root-variables.js +53 -0
- package/dist/eslint/pasika/rules/css/rule-tester.d.ts +11 -0
- package/dist/eslint/pasika/rules/css/rule-tester.js +22 -0
- package/dist/eslint/pasika/rules/css/stylesheet-ordering.d.ts +11 -0
- package/dist/eslint/pasika/rules/css/stylesheet-ordering.js +76 -0
- package/dist/eslint/pasika/rules/css/surface-utility.d.ts +10 -0
- package/dist/eslint/pasika/rules/css/surface-utility.js +41 -0
- package/dist/eslint/pasika/rules/css/theme-reset.d.ts +9 -0
- package/dist/eslint/pasika/rules/css/theme-reset.js +36 -0
- package/dist/eslint/pasika/rules/css/theme-variable-namespace.d.ts +11 -0
- package/dist/eslint/pasika/rules/css/theme-variable-namespace.js +53 -0
- package/dist/eslint/pasika/rules/cva-appearance-props.d.ts +10 -0
- package/dist/eslint/pasika/rules/cva-appearance-props.js +77 -0
- package/dist/eslint/pasika/rules/cva-boolean-variants.d.ts +11 -0
- package/dist/eslint/pasika/rules/cva-boolean-variants.js +97 -0
- package/dist/eslint/pasika/rules/data-testid-case.d.ts +2 -0
- package/dist/eslint/pasika/rules/data-testid-case.js +76 -0
- package/dist/eslint/pasika/rules/enforce-cn-merge.d.ts +13 -1
- package/dist/eslint/pasika/rules/enforce-cn-merge.js +46 -44
- package/dist/eslint/pasika/rules/enforce-cva-variant-props.js +53 -48
- package/dist/eslint/pasika/rules/filename-case.js +28 -5
- package/dist/eslint/pasika/rules/hook-complexity.d.ts +12 -0
- package/dist/eslint/pasika/rules/hook-complexity.js +114 -0
- package/dist/eslint/pasika/rules/hook-extraction.d.ts +11 -0
- package/dist/eslint/pasika/rules/hook-extraction.js +55 -0
- package/dist/eslint/pasika/rules/import-through-index.d.ts +2 -0
- package/dist/eslint/pasika/rules/import-through-index.js +52 -0
- package/dist/eslint/pasika/rules/interactive-component.d.ts +10 -0
- package/dist/eslint/pasika/rules/interactive-component.js +76 -0
- package/dist/eslint/pasika/rules/json/index.d.ts +5 -0
- package/dist/eslint/pasika/rules/json/index.js +9 -0
- package/dist/eslint/pasika/rules/json/no-cache-flag.d.ts +10 -0
- package/dist/eslint/pasika/rules/json/no-cache-flag.js +45 -0
- package/dist/eslint/pasika/rules/json/no-vulyk-dependency.d.ts +10 -0
- package/dist/eslint/pasika/rules/json/no-vulyk-dependency.js +41 -0
- package/dist/eslint/pasika/rules/json/rule-tester.d.ts +11 -0
- package/dist/eslint/pasika/rules/json/rule-tester.js +21 -0
- package/dist/eslint/pasika/rules/jsx-hygiene.d.ts +9 -0
- package/dist/eslint/pasika/rules/jsx-hygiene.js +108 -0
- package/dist/eslint/pasika/rules/locale-dotted-path.d.ts +10 -0
- package/dist/eslint/pasika/rules/locale-dotted-path.js +65 -0
- package/dist/eslint/pasika/rules/locale-placement.d.ts +13 -0
- package/dist/eslint/pasika/rules/locale-placement.js +163 -0
- package/dist/eslint/pasika/rules/locales-location.d.ts +11 -0
- package/dist/eslint/pasika/rules/locales-location.js +56 -0
- package/dist/eslint/pasika/rules/md/doc-kind-suffix.d.ts +5 -0
- package/dist/eslint/pasika/rules/md/doc-kind-suffix.js +29 -0
- package/dist/eslint/pasika/rules/md/example-heading-description.d.ts +5 -0
- package/dist/eslint/pasika/rules/md/example-heading-description.js +30 -0
- package/dist/eslint/pasika/rules/md/glossary-term-linking.d.ts +2 -0
- package/dist/eslint/pasika/rules/md/glossary-term-linking.js +99 -0
- package/dist/eslint/pasika/rules/md/guide-folder-entry-point.d.ts +2 -0
- package/dist/eslint/pasika/rules/md/guide-folder-entry-point.js +46 -0
- package/dist/eslint/pasika/rules/md/guide-link-anchors.d.ts +5 -0
- package/dist/eslint/pasika/rules/md/guide-link-anchors.js +50 -0
- package/dist/eslint/pasika/rules/md/guide-overview-no-links.d.ts +5 -0
- package/dist/eslint/pasika/rules/md/guide-overview-no-links.js +51 -0
- package/dist/eslint/pasika/rules/md/guide-states-no-requirement.d.ts +5 -0
- package/dist/eslint/pasika/rules/md/guide-states-no-requirement.js +26 -0
- package/dist/eslint/pasika/rules/md/guide-step-single-link.d.ts +5 -0
- package/dist/eslint/pasika/rules/md/guide-step-single-link.js +48 -0
- package/dist/eslint/pasika/rules/md/guide-step-single-sentence.d.ts +5 -0
- package/dist/eslint/pasika/rules/md/guide-step-single-sentence.js +42 -0
- package/dist/eslint/pasika/rules/md/helpers.d.ts +25 -0
- package/dist/eslint/pasika/rules/md/helpers.js +37 -0
- package/dist/eslint/pasika/rules/md/index.d.ts +27 -0
- package/dist/eslint/pasika/rules/md/index.js +53 -0
- package/dist/eslint/pasika/rules/md/no-cross-document-link.d.ts +5 -0
- package/dist/eslint/pasika/rules/md/no-cross-document-link.js +36 -0
- package/dist/eslint/pasika/rules/md/no-nested-how-to.d.ts +5 -0
- package/dist/eslint/pasika/rules/md/no-nested-how-to.js +39 -0
- package/dist/eslint/pasika/rules/md/no-template-prompt.d.ts +5 -0
- package/dist/eslint/pasika/rules/md/no-template-prompt.js +22 -0
- package/dist/eslint/pasika/rules/md/overview-length.d.ts +5 -0
- package/dist/eslint/pasika/rules/md/overview-length.js +47 -0
- package/dist/eslint/pasika/rules/md/overview-present.d.ts +5 -0
- package/dist/eslint/pasika/rules/md/overview-present.js +41 -0
- package/dist/eslint/pasika/rules/md/policy-no-examples.d.ts +5 -0
- package/dist/eslint/pasika/rules/md/policy-no-examples.js +28 -0
- package/dist/eslint/pasika/rules/md/policy-single-document.d.ts +5 -0
- package/dist/eslint/pasika/rules/md/policy-single-document.js +34 -0
- package/dist/eslint/pasika/rules/md/policy-subject-headings.d.ts +5 -0
- package/dist/eslint/pasika/rules/md/policy-subject-headings.js +58 -0
- package/dist/eslint/pasika/rules/md/project-index.d.ts +17 -0
- package/dist/eslint/pasika/rules/md/project-index.js +67 -0
- package/dist/eslint/pasika/rules/md/reference-block-headings.d.ts +6 -0
- package/dist/eslint/pasika/rules/md/reference-block-headings.js +31 -0
- package/dist/eslint/pasika/rules/md/reference-no-rfc-vocabulary.d.ts +6 -0
- package/dist/eslint/pasika/rules/md/reference-no-rfc-vocabulary.js +26 -0
- package/dist/eslint/pasika/rules/md/requirement-present.d.ts +5 -0
- package/dist/eslint/pasika/rules/md/requirement-present.js +41 -0
- package/dist/eslint/pasika/rules/md/rfc-only-in-bullets.d.ts +5 -0
- package/dist/eslint/pasika/rules/md/rfc-only-in-bullets.js +46 -0
- package/dist/eslint/pasika/rules/md/rule-paired-examples.d.ts +5 -0
- package/dist/eslint/pasika/rules/md/rule-paired-examples.js +36 -0
- package/dist/eslint/pasika/rules/md/rule-tester.d.ts +11 -0
- package/dist/eslint/pasika/rules/md/rule-tester.js +21 -0
- package/dist/eslint/pasika/rules/md/support-document-placement.d.ts +2 -0
- package/dist/eslint/pasika/rules/md/support-document-placement.js +36 -0
- package/dist/eslint/pasika/rules/md/title-matches-file-name.d.ts +2 -0
- package/dist/eslint/pasika/rules/md/title-matches-file-name.js +46 -0
- package/dist/eslint/pasika/rules/named-exports.d.ts +2 -0
- package/dist/eslint/pasika/rules/named-exports.js +49 -0
- package/dist/eslint/pasika/rules/no-eslint-disable.d.ts +10 -0
- package/dist/eslint/pasika/rules/no-eslint-disable.js +33 -0
- package/dist/eslint/pasika/rules/no-mixed-concerns.js +9 -22
- package/dist/eslint/pasika/rules/no-util-barrel.d.ts +2 -0
- package/dist/eslint/pasika/rules/no-util-barrel.js +48 -0
- package/dist/eslint/pasika/rules/pure-function-extract.d.ts +9 -0
- package/dist/eslint/pasika/rules/pure-function-extract.js +92 -0
- package/dist/eslint/pasika/rules/shared-style-dedup.d.ts +13 -0
- package/dist/eslint/pasika/rules/shared-style-dedup.js +91 -0
- package/dist/eslint/pasika/rules/stay-flat.d.ts +12 -0
- package/dist/eslint/pasika/rules/stay-flat.js +81 -0
- package/dist/eslint/pasika/rules/support-folder-shape.d.ts +2 -0
- package/dist/eslint/pasika/rules/support-folder-shape.js +59 -0
- package/dist/eslint/pasika/rules/type-extraction.d.ts +13 -0
- package/dist/eslint/pasika/rules/type-extraction.js +85 -0
- package/dist/eslint/pasika/rules/ui-state.d.ts +9 -0
- package/dist/eslint/pasika/rules/ui-state.js +99 -0
- package/dist/eslint/pasika/rules/util-file-name.d.ts +2 -0
- package/dist/eslint/pasika/rules/util-file-name.js +48 -0
- package/dist/eslint/pasika/rules/value-extraction.d.ts +13 -0
- package/dist/eslint/pasika/rules/value-extraction.js +48 -0
- package/package.json +14 -7
- package/dist/enforcement/docs-check.d.ts +0 -17
- package/dist/enforcement/docs-check.js +0 -162
- package/enforcement/registry.json +0 -1139
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
* @see docs/code-organization-guide/rules/smart-vs-dumb-component-rule.md
|
|
10
10
|
*/
|
|
11
11
|
import path from "node:path";
|
|
12
|
+
import { parseComponentInfo } from "./component-conventions.js";
|
|
12
13
|
/**
|
|
13
14
|
* Next.js App Router routing files that are exempt from filename-case checks.
|
|
14
15
|
* https://nextjs.org/docs/app/getting-started/project-structure#routing-files
|
|
@@ -70,12 +71,34 @@ export const filenameCaseRule = {
|
|
|
70
71
|
if (NEXT_ROUTING_FILES.has(base)) {
|
|
71
72
|
return {};
|
|
72
73
|
}
|
|
73
|
-
// .tsx components
|
|
74
|
+
// .tsx components use PascalCase when smart and kebab-case when dumb.
|
|
74
75
|
if (ext === ".tsx") {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
76
|
+
return {
|
|
77
|
+
Program(node) {
|
|
78
|
+
const components = parseComponentInfo(context.sourceCode.text, filename);
|
|
79
|
+
const component = components[0];
|
|
80
|
+
if (component?.smart && !isPascalCase(base)) {
|
|
81
|
+
context.report({
|
|
82
|
+
node,
|
|
83
|
+
message: `Smart component files must use PascalCase.tsx. Filename "${path.basename(filename)}" is not PascalCase.`,
|
|
84
|
+
});
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
if (component && !component.smart && !isKebabCase(base)) {
|
|
88
|
+
context.report({
|
|
89
|
+
node,
|
|
90
|
+
message: `Dumb component files must use kebab-case.tsx. Filename "${path.basename(filename)}" is not kebab-case.`,
|
|
91
|
+
});
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
if (!component && !(isPascalCase(base) || isKebabCase(base))) {
|
|
95
|
+
context.report({
|
|
96
|
+
node,
|
|
97
|
+
message: `Filename "${path.basename(filename)}" does not match pasika conventions. Component files must be PascalCase.tsx (smart) or kebab-case.tsx (dumb).`,
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
},
|
|
101
|
+
};
|
|
79
102
|
}
|
|
80
103
|
// Everything else must be kebab-case
|
|
81
104
|
if (!isKebabCase(base)) {
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ESLint rule: pasika/hook-complexity
|
|
3
|
+
*
|
|
4
|
+
* - A custom hook with exactly one consumer MUST be extracted when it contains
|
|
5
|
+
* two or more imperative categories.
|
|
6
|
+
* - A custom hook with one consumer that contains fewer than two imperative
|
|
7
|
+
* categories MUST stay inline in its consumer file.
|
|
8
|
+
*
|
|
9
|
+
* @see docs/code-organization-guide/rules/hook-extraction-rule.md
|
|
10
|
+
*/
|
|
11
|
+
import type { Rule } from "eslint";
|
|
12
|
+
export declare const hookComplexityRule: Rule.RuleModule;
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ESLint rule: pasika/hook-complexity
|
|
3
|
+
*
|
|
4
|
+
* - A custom hook with exactly one consumer MUST be extracted when it contains
|
|
5
|
+
* two or more imperative categories.
|
|
6
|
+
* - A custom hook with one consumer that contains fewer than two imperative
|
|
7
|
+
* categories MUST stay inline in its consumer file.
|
|
8
|
+
*
|
|
9
|
+
* @see docs/code-organization-guide/rules/hook-extraction-rule.md
|
|
10
|
+
*/
|
|
11
|
+
import path from "node:path";
|
|
12
|
+
import ts from "typescript";
|
|
13
|
+
const REACT_HOOKS = new Set([
|
|
14
|
+
"useState",
|
|
15
|
+
"useEffect",
|
|
16
|
+
"useContext",
|
|
17
|
+
"useReducer",
|
|
18
|
+
"useCallback",
|
|
19
|
+
"useMemo",
|
|
20
|
+
"useRef",
|
|
21
|
+
"useLayoutEffect",
|
|
22
|
+
"useImperativeHandle",
|
|
23
|
+
"useDebugValue",
|
|
24
|
+
"useDeferredValue",
|
|
25
|
+
"useTransition",
|
|
26
|
+
"useId",
|
|
27
|
+
"useSyncExternalStore",
|
|
28
|
+
"useInsertionEffect",
|
|
29
|
+
]);
|
|
30
|
+
function isHookName(name) {
|
|
31
|
+
return /^use[A-Z]/.test(name);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Counts the distinct React hook categories called anywhere inside a hook body.
|
|
35
|
+
* Re-parses just the body's source slice with the TypeScript compiler so the
|
|
36
|
+
* walk stays fully typed instead of unrolling ESTree unions by hand.
|
|
37
|
+
*/
|
|
38
|
+
function countImperativeCategories(body, sourceText) {
|
|
39
|
+
const start = body.range?.[0] ?? 0;
|
|
40
|
+
const end = body.range?.[1] ?? sourceText.length;
|
|
41
|
+
const sourceFile = ts.createSourceFile("hook.ts", sourceText.slice(start, end), ts.ScriptTarget.Latest, true, ts.ScriptKind.TS);
|
|
42
|
+
const categories = new Set();
|
|
43
|
+
const visit = (node) => {
|
|
44
|
+
if (ts.isCallExpression(node) && ts.isIdentifier(node.expression) && REACT_HOOKS.has(node.expression.text)) {
|
|
45
|
+
categories.add(node.expression.text);
|
|
46
|
+
}
|
|
47
|
+
ts.forEachChild(node, visit);
|
|
48
|
+
};
|
|
49
|
+
visit(sourceFile);
|
|
50
|
+
return categories.size;
|
|
51
|
+
}
|
|
52
|
+
export const hookComplexityRule = {
|
|
53
|
+
meta: {
|
|
54
|
+
schema: [],
|
|
55
|
+
type: "problem",
|
|
56
|
+
docs: {
|
|
57
|
+
description: "Require extraction of complex single-consumer hooks and inline of simple ones.",
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
create(context) {
|
|
61
|
+
const filename = context.filename;
|
|
62
|
+
const sourceRoot = path.resolve("src");
|
|
63
|
+
const relative = path.relative(sourceRoot, filename);
|
|
64
|
+
if (relative.startsWith(".."))
|
|
65
|
+
return {};
|
|
66
|
+
const segments = relative.split(path.sep);
|
|
67
|
+
const sourceText = context.sourceCode.text;
|
|
68
|
+
function checkHook(node, name, body, exported) {
|
|
69
|
+
if (!exported)
|
|
70
|
+
return;
|
|
71
|
+
if (!name || !isHookName(name))
|
|
72
|
+
return;
|
|
73
|
+
if (!body)
|
|
74
|
+
return;
|
|
75
|
+
const imperativeCount = countImperativeCategories(body, sourceText);
|
|
76
|
+
const parentFolder = segments.length >= 2 ? segments[segments.length - 2] : undefined;
|
|
77
|
+
const inSupportFolder = parentFolder === "hooks";
|
|
78
|
+
if (imperativeCount >= 2 && !inSupportFolder) {
|
|
79
|
+
context.report({
|
|
80
|
+
node,
|
|
81
|
+
message: `Hook "${name}" has ${String(imperativeCount)} imperative categories and must be extracted to a hooks/ folder. ` +
|
|
82
|
+
"See docs/code-organization-guide/rules/hook-extraction-rule.md",
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
else if (imperativeCount < 2 && inSupportFolder) {
|
|
86
|
+
context.report({
|
|
87
|
+
node,
|
|
88
|
+
message: `Hook "${name}" has fewer than two imperative categories and must stay inline in its consumer file. ` +
|
|
89
|
+
"See docs/code-organization-guide/rules/hook-extraction-rule.md",
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return {
|
|
94
|
+
FunctionDeclaration(node) {
|
|
95
|
+
const exported = node.parent?.type === "ExportNamedDeclaration";
|
|
96
|
+
checkHook(node, node.id?.name, node.body, exported);
|
|
97
|
+
},
|
|
98
|
+
VariableDeclarator(node) {
|
|
99
|
+
if (node.id.type !== "Identifier")
|
|
100
|
+
return;
|
|
101
|
+
const exported = node.parent.parent?.type === "ExportNamedDeclaration";
|
|
102
|
+
if (!exported)
|
|
103
|
+
return;
|
|
104
|
+
const init = node.init;
|
|
105
|
+
if (!init || (init.type !== "ArrowFunctionExpression" && init.type !== "FunctionExpression")) {
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
if (init.body.type !== "BlockStatement")
|
|
109
|
+
return;
|
|
110
|
+
checkHook(node, node.id.name, init.body, true);
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
},
|
|
114
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ESLint rule: pasika/hook-extraction
|
|
3
|
+
*
|
|
4
|
+
* A custom hook MUST be extracted to its own file when two or more consumers
|
|
5
|
+
* use it. The file must be read across the tree, because how many files import
|
|
6
|
+
* a hook is not visible from one file.
|
|
7
|
+
*
|
|
8
|
+
* @see docs/code-organization-guide/rules/hook-extraction-rule.md
|
|
9
|
+
*/
|
|
10
|
+
import type { Rule } from "eslint";
|
|
11
|
+
export declare const hookExtractionRule: Rule.RuleModule;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ESLint rule: pasika/hook-extraction
|
|
3
|
+
*
|
|
4
|
+
* A custom hook MUST be extracted to its own file when two or more consumers
|
|
5
|
+
* use it. The file must be read across the tree, because how many files import
|
|
6
|
+
* a hook is not visible from one file.
|
|
7
|
+
*
|
|
8
|
+
* @see docs/code-organization-guide/rules/hook-extraction-rule.md
|
|
9
|
+
*/
|
|
10
|
+
import path from "node:path";
|
|
11
|
+
import { getProjectIndex, symbolKey } from "../project/index.js";
|
|
12
|
+
import { folderSegmentsOf, segmentsOf } from "../project/ccf.js";
|
|
13
|
+
export const hookExtractionRule = {
|
|
14
|
+
meta: {
|
|
15
|
+
schema: [],
|
|
16
|
+
type: "problem",
|
|
17
|
+
docs: {
|
|
18
|
+
description: "Require a custom hook with two or more consumers to live in its own hooks/ file.",
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
create(context) {
|
|
22
|
+
const sourceRoot = path.resolve("src");
|
|
23
|
+
const file = path.resolve(context.filename);
|
|
24
|
+
const segments = segmentsOf(file, sourceRoot);
|
|
25
|
+
if (segments.length === 0)
|
|
26
|
+
return {};
|
|
27
|
+
const index = getProjectIndex(sourceRoot);
|
|
28
|
+
if (!index)
|
|
29
|
+
return {};
|
|
30
|
+
const module = index.modules.get(file);
|
|
31
|
+
if (!module)
|
|
32
|
+
return {};
|
|
33
|
+
const folder = folderSegmentsOf(file, sourceRoot);
|
|
34
|
+
const alreadyInHooksFolder = folder.length > 0 && folder[folder.length - 1] === "hooks";
|
|
35
|
+
const reusedHooks = module.exports
|
|
36
|
+
.filter((exp) => exp.kind === "hook" && !alreadyInHooksFolder)
|
|
37
|
+
.map((exp) => ({ exp, consumers: index.symbolConsumers.get(symbolKey(file, exp.name)) }))
|
|
38
|
+
.filter(({ consumers }) => (consumers?.size ?? 0) >= 2);
|
|
39
|
+
if (reusedHooks.length === 0)
|
|
40
|
+
return {};
|
|
41
|
+
return {
|
|
42
|
+
Program(node) {
|
|
43
|
+
for (const { exp, consumers } of reusedHooks) {
|
|
44
|
+
context.report({
|
|
45
|
+
node,
|
|
46
|
+
loc: { line: exp.line, column: 0 },
|
|
47
|
+
message: `Hook "${exp.name}" has ${String(consumers?.size ?? 0)} consumers; ` +
|
|
48
|
+
"extract it to its own file in a hooks/ folder. " +
|
|
49
|
+
"See docs/code-organization-guide/rules/hook-extraction-rule.md",
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
},
|
|
55
|
+
};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { resolveSpecifier } from "../project/index.js";
|
|
3
|
+
import { segmentsOf } from "../project/ccf.js";
|
|
4
|
+
export const importThroughIndexRule = {
|
|
5
|
+
meta: {
|
|
6
|
+
schema: [],
|
|
7
|
+
type: "problem",
|
|
8
|
+
docs: {
|
|
9
|
+
description: "Require imports of support files to use their support-folder index.",
|
|
10
|
+
},
|
|
11
|
+
},
|
|
12
|
+
create(context) {
|
|
13
|
+
const filename = path.resolve(context.filename);
|
|
14
|
+
const sourceRoot = sourceRootOf(filename);
|
|
15
|
+
return {
|
|
16
|
+
Program(node) {
|
|
17
|
+
for (const specifier of importSpecifiers(context.sourceCode.text)) {
|
|
18
|
+
const target = resolveSpecifier(filename, specifier, sourceRoot);
|
|
19
|
+
if (!target)
|
|
20
|
+
continue;
|
|
21
|
+
const targetSegments = segmentsOf(target, sourceRoot);
|
|
22
|
+
const supportFolderIndex = targetSegments.findIndex((segment) => ["constants", "types", "schemas"].includes(segment));
|
|
23
|
+
const supportFolder = supportFolderIndex >= 0 ? targetSegments[supportFolderIndex] : undefined;
|
|
24
|
+
if (!supportFolder || path.basename(target).startsWith("index."))
|
|
25
|
+
continue;
|
|
26
|
+
const folderIndex = targetSegments.slice(0, supportFolderIndex + 1);
|
|
27
|
+
const expected = `@/${folderIndex.join("/")}`;
|
|
28
|
+
context.report({
|
|
29
|
+
node,
|
|
30
|
+
message: `Import support files through ${expected} instead of "${specifier}". ` +
|
|
31
|
+
"See the code-organization-guide exports and imports rules.",
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
function importSpecifiers(source) {
|
|
39
|
+
const specifiers = [];
|
|
40
|
+
const pattern = /\bimport\s+(?:(?:type\s+)?[\s\S]*?\sfrom\s+)?["'](?<specifier>[^"']+)["']/g;
|
|
41
|
+
for (const match of source.matchAll(pattern)) {
|
|
42
|
+
const specifier = match.groups?.specifier;
|
|
43
|
+
if (specifier)
|
|
44
|
+
specifiers.push(specifier);
|
|
45
|
+
}
|
|
46
|
+
return specifiers;
|
|
47
|
+
}
|
|
48
|
+
function sourceRootOf(filename) {
|
|
49
|
+
const marker = `${path.sep}src${path.sep}`;
|
|
50
|
+
const srcIndex = filename.lastIndexOf(marker);
|
|
51
|
+
return srcIndex >= 0 ? filename.slice(0, srcIndex + marker.length - 1) : path.resolve("src");
|
|
52
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ESLint rule: pasika/interactive-component
|
|
3
|
+
*
|
|
4
|
+
* Interactive elements are component boundaries when they are mixed with other
|
|
5
|
+
* content in the same component. Standalone interactive components are valid.
|
|
6
|
+
*
|
|
7
|
+
* @see docs/code-organization-guide/rules/interactive-component-rule.md
|
|
8
|
+
*/
|
|
9
|
+
import type { Rule } from "eslint";
|
|
10
|
+
export declare const interactiveComponentRule: Rule.RuleModule;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ESLint rule: pasika/interactive-component
|
|
3
|
+
*
|
|
4
|
+
* Interactive elements are component boundaries when they are mixed with other
|
|
5
|
+
* content in the same component. Standalone interactive components are valid.
|
|
6
|
+
*
|
|
7
|
+
* @see docs/code-organization-guide/rules/interactive-component-rule.md
|
|
8
|
+
*/
|
|
9
|
+
const INTERACTIVE_TAGS = new Set([
|
|
10
|
+
"a",
|
|
11
|
+
"button",
|
|
12
|
+
"details",
|
|
13
|
+
"embed",
|
|
14
|
+
"iframe",
|
|
15
|
+
"input",
|
|
16
|
+
"label",
|
|
17
|
+
"select",
|
|
18
|
+
"textarea",
|
|
19
|
+
"video",
|
|
20
|
+
]);
|
|
21
|
+
function tagName(node) {
|
|
22
|
+
const name = node.openingElement?.name;
|
|
23
|
+
if (name?.type !== "JSXIdentifier")
|
|
24
|
+
return undefined;
|
|
25
|
+
return name.name;
|
|
26
|
+
}
|
|
27
|
+
function isInteractive(node) {
|
|
28
|
+
const name = tagName(node);
|
|
29
|
+
return name !== undefined && INTERACTIVE_TAGS.has(name);
|
|
30
|
+
}
|
|
31
|
+
function isComponentElement(node) {
|
|
32
|
+
const name = tagName(node);
|
|
33
|
+
return name !== undefined && /^[A-Z]/.test(name);
|
|
34
|
+
}
|
|
35
|
+
function isComponentReturn(node) {
|
|
36
|
+
const parent = node.parent;
|
|
37
|
+
if (parent.type === "ReturnStatement")
|
|
38
|
+
return true;
|
|
39
|
+
return parent.type === "ArrowFunctionExpression" && parent.body === node;
|
|
40
|
+
}
|
|
41
|
+
export const interactiveComponentRule = {
|
|
42
|
+
meta: {
|
|
43
|
+
schema: [],
|
|
44
|
+
type: "problem",
|
|
45
|
+
docs: {
|
|
46
|
+
description: "Require interactive HTML elements mixed with other content to be extracted into components.",
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
create(context) {
|
|
50
|
+
if (!context.filename.endsWith(".tsx") && !context.filename.endsWith(".jsx"))
|
|
51
|
+
return {};
|
|
52
|
+
return {
|
|
53
|
+
JSXElement(node) {
|
|
54
|
+
if (!isInteractive(node))
|
|
55
|
+
return;
|
|
56
|
+
if (isComponentElement(node.parent)) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
if (isInteractive(node.parent)) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
if (isComponentReturn(node)) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const name = tagName(node);
|
|
66
|
+
if (name === undefined)
|
|
67
|
+
return;
|
|
68
|
+
context.report({
|
|
69
|
+
node,
|
|
70
|
+
message: `Extract the interactive <${name}> into a descriptive component. ` +
|
|
71
|
+
"See docs/code-organization-guide/rules/interactive-component-rule.md",
|
|
72
|
+
});
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
},
|
|
76
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* All JSON rules for the package.json checks.
|
|
3
|
+
*/
|
|
4
|
+
import { noCacheFlagRule } from "./no-cache-flag.js";
|
|
5
|
+
import { noVulykDependencyRule } from "./no-vulyk-dependency.js";
|
|
6
|
+
export const jsonRules = {
|
|
7
|
+
"no-cache-flag": noCacheFlagRule,
|
|
8
|
+
"no-vulyk-dependency": noVulykDependencyRule,
|
|
9
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ESLint rule: pasika/no-cache-flag
|
|
3
|
+
*
|
|
4
|
+
* A lint command MUST NOT pass ESLint's --cache flag, because rules that
|
|
5
|
+
* compare a file against the rest of the tree need every file in the run.
|
|
6
|
+
*
|
|
7
|
+
* @see docs/agent-policy.md
|
|
8
|
+
*/
|
|
9
|
+
import type { JSONRuleDefinition } from "@eslint/json";
|
|
10
|
+
export declare const noCacheFlagRule: JSONRuleDefinition;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ESLint rule: pasika/no-cache-flag
|
|
3
|
+
*
|
|
4
|
+
* A lint command MUST NOT pass ESLint's --cache flag, because rules that
|
|
5
|
+
* compare a file against the rest of the tree need every file in the run.
|
|
6
|
+
*
|
|
7
|
+
* @see docs/agent-policy.md
|
|
8
|
+
*/
|
|
9
|
+
function memberName(member) {
|
|
10
|
+
return member.name.type === "String" ? member.name.value : member.name.name;
|
|
11
|
+
}
|
|
12
|
+
export const noCacheFlagRule = {
|
|
13
|
+
meta: {
|
|
14
|
+
schema: [],
|
|
15
|
+
type: "problem",
|
|
16
|
+
docs: {
|
|
17
|
+
description: "Require lint scripts to not pass ESLint's --cache flag.",
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
create(context) {
|
|
21
|
+
return {
|
|
22
|
+
Document(node) {
|
|
23
|
+
const root = node.body;
|
|
24
|
+
if (root.type !== "Object")
|
|
25
|
+
return;
|
|
26
|
+
const scripts = root.members.find((member) => memberName(member) === "scripts");
|
|
27
|
+
if (scripts?.value.type !== "Object")
|
|
28
|
+
return;
|
|
29
|
+
for (const member of scripts.value.members) {
|
|
30
|
+
const name = memberName(member);
|
|
31
|
+
if (name !== "lint" && name !== "fix")
|
|
32
|
+
continue;
|
|
33
|
+
if (member.value.type !== "String")
|
|
34
|
+
continue;
|
|
35
|
+
if (member.value.value.includes("--cache")) {
|
|
36
|
+
context.report({
|
|
37
|
+
node: member,
|
|
38
|
+
message: "Lint scripts must not pass ESLint's --cache flag because cross-file rules require every file in the run.",
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
},
|
|
45
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ESLint rule: pasika/no-vulyk-dependency
|
|
3
|
+
*
|
|
4
|
+
* Vulyk MUST NOT be added to package.json in order to run its CLI; it MUST run
|
|
5
|
+
* as an ephemeral command such as npx vulyk@latest.
|
|
6
|
+
*
|
|
7
|
+
* @see docs/agent-policy.md
|
|
8
|
+
*/
|
|
9
|
+
import type { JSONRuleDefinition } from "@eslint/json";
|
|
10
|
+
export declare const noVulykDependencyRule: JSONRuleDefinition;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ESLint rule: pasika/no-vulyk-dependency
|
|
3
|
+
*
|
|
4
|
+
* Vulyk MUST NOT be added to package.json in order to run its CLI; it MUST run
|
|
5
|
+
* as an ephemeral command such as npx vulyk@latest.
|
|
6
|
+
*
|
|
7
|
+
* @see docs/agent-policy.md
|
|
8
|
+
*/
|
|
9
|
+
function memberName(member) {
|
|
10
|
+
return member.name.type === "String" ? member.name.value : member.name.name;
|
|
11
|
+
}
|
|
12
|
+
export const noVulykDependencyRule = {
|
|
13
|
+
meta: {
|
|
14
|
+
schema: [],
|
|
15
|
+
type: "problem",
|
|
16
|
+
docs: {
|
|
17
|
+
description: "Require vulyk to run as an ephemeral command, not a package.json dependency.",
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
create(context) {
|
|
21
|
+
return {
|
|
22
|
+
Document(node) {
|
|
23
|
+
const root = node.body;
|
|
24
|
+
if (root.type !== "Object")
|
|
25
|
+
return;
|
|
26
|
+
for (const key of ["dependencies", "devDependencies"]) {
|
|
27
|
+
const section = root.members.find((member) => memberName(member) === key);
|
|
28
|
+
if (section?.value.type !== "Object")
|
|
29
|
+
continue;
|
|
30
|
+
const vulyk = section.value.members.find((member) => memberName(member) === "vulyk");
|
|
31
|
+
if (vulyk) {
|
|
32
|
+
context.report({
|
|
33
|
+
node: vulyk,
|
|
34
|
+
message: 'Vulyk must not be a package.json dependency. Run it as "npx vulyk@latest" instead.',
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
},
|
|
41
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared RuleTester wiring for the package.json rule tests.
|
|
3
|
+
*
|
|
4
|
+
* Each `describe` title in a rule test is the exact text of the requirement the
|
|
5
|
+
* case pins, which is how `pasika coverage` verifies that a requirement recorded
|
|
6
|
+
* as lint-enforced has a test behind it.
|
|
7
|
+
*/
|
|
8
|
+
import { describe } from "node:test";
|
|
9
|
+
import { RuleTester } from "eslint";
|
|
10
|
+
export declare const jsonRuleTester: RuleTester;
|
|
11
|
+
export { describe };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared RuleTester wiring for the package.json rule tests.
|
|
3
|
+
*
|
|
4
|
+
* Each `describe` title in a rule test is the exact text of the requirement the
|
|
5
|
+
* case pins, which is how `pasika coverage` verifies that a requirement recorded
|
|
6
|
+
* as lint-enforced has a test behind it.
|
|
7
|
+
*/
|
|
8
|
+
import { describe, it } from "node:test";
|
|
9
|
+
import { RuleTester } from "eslint";
|
|
10
|
+
import jsonPlugin from "@eslint/json";
|
|
11
|
+
import { jsonRules } from "../../index.js";
|
|
12
|
+
RuleTester.describe = describe;
|
|
13
|
+
RuleTester.it = it;
|
|
14
|
+
export const jsonRuleTester = new RuleTester({
|
|
15
|
+
language: "json/json",
|
|
16
|
+
plugins: {
|
|
17
|
+
json: { languages: { json: jsonPlugin.languages.json } },
|
|
18
|
+
pasika: { rules: jsonRules },
|
|
19
|
+
},
|
|
20
|
+
});
|
|
21
|
+
export { describe };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ESLint rule: pasika/jsx-hygiene
|
|
3
|
+
*
|
|
4
|
+
* Keeps calculations and complex conditions out of JSX children and attributes.
|
|
5
|
+
*
|
|
6
|
+
* @see docs/code-organization-guide/rules/jsx-hygiene-rule.md
|
|
7
|
+
*/
|
|
8
|
+
import type { Rule } from "eslint";
|
|
9
|
+
export declare const jsxHygieneRule: Rule.RuleModule;
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ESLint rule: pasika/jsx-hygiene
|
|
3
|
+
*
|
|
4
|
+
* Keeps calculations and complex conditions out of JSX children and attributes.
|
|
5
|
+
*
|
|
6
|
+
* @see docs/code-organization-guide/rules/jsx-hygiene-rule.md
|
|
7
|
+
*/
|
|
8
|
+
import ts from "typescript";
|
|
9
|
+
const ARITHMETIC_OPERATORS = new Set(["+", "-", "*", "/", "%"]);
|
|
10
|
+
function isCallExpression(node) {
|
|
11
|
+
return node.type === "CallExpression";
|
|
12
|
+
}
|
|
13
|
+
function isMemberExpression(node) {
|
|
14
|
+
return ["MemberExpression", "OptionalMemberExpression"].includes(node.type);
|
|
15
|
+
}
|
|
16
|
+
function logicalOperatorCount(node) {
|
|
17
|
+
if (node.type !== "LogicalExpression")
|
|
18
|
+
return 0;
|
|
19
|
+
return 1 + logicalOperatorCount(node.left) + logicalOperatorCount(node.right);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Whether the parsed text of a node's subtree contains a conditional expression
|
|
23
|
+
* anywhere. Re-parses just the node's source slice with the TypeScript compiler
|
|
24
|
+
* so the walk stays fully typed, including inside JSX children.
|
|
25
|
+
*/
|
|
26
|
+
function containsConditionalIn(node, sourceText) {
|
|
27
|
+
const start = node.range?.[0] ?? 0;
|
|
28
|
+
const end = node.range?.[1] ?? sourceText.length;
|
|
29
|
+
const sourceFile = ts.createSourceFile("fragment.tsx", sourceText.slice(start, end), ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX);
|
|
30
|
+
let found = false;
|
|
31
|
+
const visit = (child) => {
|
|
32
|
+
if (found)
|
|
33
|
+
return;
|
|
34
|
+
if (ts.isConditionalExpression(child)) {
|
|
35
|
+
found = true;
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
ts.forEachChild(child, visit);
|
|
39
|
+
};
|
|
40
|
+
visit(sourceFile);
|
|
41
|
+
return found;
|
|
42
|
+
}
|
|
43
|
+
function hasNestedTernary(node, sourceText) {
|
|
44
|
+
if (node.type !== "ConditionalExpression")
|
|
45
|
+
return false;
|
|
46
|
+
return containsConditionalIn(node.consequent, sourceText) || containsConditionalIn(node.alternate, sourceText);
|
|
47
|
+
}
|
|
48
|
+
function hasChainedCall(node) {
|
|
49
|
+
if (!isCallExpression(node) || !isMemberExpression(node.callee))
|
|
50
|
+
return false;
|
|
51
|
+
return isCallExpression(node.callee.object) || isMemberExpression(node.callee.object);
|
|
52
|
+
}
|
|
53
|
+
function hasExternalCall(node) {
|
|
54
|
+
if (!isCallExpression(node))
|
|
55
|
+
return false;
|
|
56
|
+
const callee = node.callee;
|
|
57
|
+
if (callee.type === "Identifier" && callee.name === "cn")
|
|
58
|
+
return false;
|
|
59
|
+
if (isMemberExpression(callee))
|
|
60
|
+
return false;
|
|
61
|
+
return callee.type === "Identifier";
|
|
62
|
+
}
|
|
63
|
+
function findViolation(node, sourceText) {
|
|
64
|
+
if (node.type === "BinaryExpression" && ARITHMETIC_OPERATORS.has(node.operator)) {
|
|
65
|
+
return "arithmetic";
|
|
66
|
+
}
|
|
67
|
+
if (hasChainedCall(node))
|
|
68
|
+
return "chained built-in method calls";
|
|
69
|
+
if (hasExternalCall(node))
|
|
70
|
+
return "calls to functions declared outside the component";
|
|
71
|
+
if (hasNestedTernary(node, sourceText))
|
|
72
|
+
return "nested ternaries";
|
|
73
|
+
if (logicalOperatorCount(node) >= 2)
|
|
74
|
+
return "conditions containing two or more logical operators";
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
export const jsxHygieneRule = {
|
|
78
|
+
meta: {
|
|
79
|
+
schema: [],
|
|
80
|
+
type: "problem",
|
|
81
|
+
docs: {
|
|
82
|
+
description: "Require complex JSX expressions to be extracted before return.",
|
|
83
|
+
},
|
|
84
|
+
},
|
|
85
|
+
create(context) {
|
|
86
|
+
if (!context.filename.endsWith(".tsx") && !context.filename.endsWith(".jsx"))
|
|
87
|
+
return {};
|
|
88
|
+
const sourceText = context.sourceCode.text;
|
|
89
|
+
function checkExpression(node) {
|
|
90
|
+
const violation = findViolation(node, sourceText);
|
|
91
|
+
if (!violation)
|
|
92
|
+
return;
|
|
93
|
+
context.report({
|
|
94
|
+
node,
|
|
95
|
+
message: `Extract ${violation} from JSX before return. ` +
|
|
96
|
+
"See docs/code-organization-guide/rules/jsx-hygiene-rule.md",
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
return {
|
|
100
|
+
JSXExpressionContainer(node) {
|
|
101
|
+
const expression = node.expression;
|
|
102
|
+
if (!expression || expression.type === "JSXEmptyExpression")
|
|
103
|
+
return;
|
|
104
|
+
checkExpression(expression);
|
|
105
|
+
},
|
|
106
|
+
};
|
|
107
|
+
},
|
|
108
|
+
};
|