@trackunit/eslint-plugin-trackunit 0.8.9 β†’ 0.9.0

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.
Files changed (33) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/README.md +7 -9
  3. package/package.json +3 -2
  4. package/src/lib/config/index.d.ts +2 -8
  5. package/src/lib/config/plugins.d.ts +2 -8
  6. package/src/lib/config/plugins.js +0 -4
  7. package/src/lib/config/presets/react.d.ts +2 -8
  8. package/src/lib/rules/one-component-per-file/one-component-per-file.js +1 -0
  9. package/src/lib/rules/require-component-directory/require-component-directory.js +26 -197
  10. package/src/lib/rules/require-component-directory/utils/component-directory-structure-utils/component-directory-structure-utils.d.ts +25 -25
  11. package/src/lib/rules/require-component-directory/utils/component-directory-structure-utils/component-directory-structure-utils.js +178 -78
  12. package/src/lib/rules/require-hook-util-directory/require-hook-util-directory.d.ts +5 -3
  13. package/src/lib/rules/require-hook-util-directory/require-hook-util-directory.js +17 -13
  14. package/src/lib/rules/require-hook-util-directory/utils/hook-util-directory-structure-utils/hook-util-directory-structure-utils.d.ts +13 -4
  15. package/src/lib/rules/require-hook-util-directory/utils/hook-util-directory-structure-utils/hook-util-directory-structure-utils.js +31 -15
  16. package/src/lib/rules/require-nested-components-directory/require-nested-components-directory.d.ts +2 -1
  17. package/src/lib/rules/require-nested-components-directory/require-nested-components-directory.js +7 -6
  18. package/src/lib/rules/require-nested-components-directory/utils/nested-component-directory-utils/nested-component-directory-utils.d.ts +13 -9
  19. package/src/lib/rules/require-nested-components-directory/utils/nested-component-directory-utils/nested-component-directory-utils.js +46 -162
  20. package/src/lib/utils/component-utils.d.ts +13 -4
  21. package/src/lib/utils/component-utils.js +170 -64
  22. package/src/lib/utils/react-module-summary/react-module-summary.d.ts +26 -0
  23. package/src/lib/utils/react-module-summary/react-module-summary.js +84 -0
  24. package/src/lib/utils/source-tree-utils/source-tree-utils.d.ts +0 -37
  25. package/src/lib/utils/source-tree-utils/source-tree-utils.js +6 -109
  26. package/src/lib/rules/require-nested-components-privacy/require-nested-components-privacy.d.ts +0 -9
  27. package/src/lib/rules/require-nested-components-privacy/require-nested-components-privacy.js +0 -63
  28. package/src/lib/rules/require-nested-components-privacy/utils/nested-component-privacy-utils/nested-component-privacy-utils.d.ts +0 -14
  29. package/src/lib/rules/require-nested-components-privacy/utils/nested-component-privacy-utils/nested-component-privacy-utils.js +0 -62
  30. package/src/lib/rules/require-page-directory/require-page-directory.d.ts +0 -8
  31. package/src/lib/rules/require-page-directory/require-page-directory.js +0 -44
  32. package/src/lib/rules/require-page-directory/utils/page-directory-structure-utils/page-directory-structure-utils.d.ts +0 -18
  33. package/src/lib/rules/require-page-directory/utils/page-directory-structure-utils/page-directory-structure-utils.js +0 -159
package/CHANGELOG.md CHANGED
@@ -1,3 +1,22 @@
1
+ ## 0.9.0 (2026-11-16)
2
+
3
+ ### πŸš€ Features
4
+
5
+ - ⚠️ **eslint-plugin-trackunit:** fix folder-structure rule review findings [SPIX-1395] ([#27317](https://github.com/Trackunit/manager/pull/27317))
6
+
7
+ ### ⚠️ Breaking Changes
8
+
9
+ - **eslint-plugin-trackunit:** fix folder-structure rule review findings [SPIX-1395] ([#27317](https://github.com/Trackunit/manager/pull/27317))
10
+
11
+ ### 🧱 Updated Dependencies
12
+
13
+ - Updated css-classname-utils to 0.0.128
14
+ - Updated shared-utils to 1.22.47
15
+
16
+ ### ❀️ Thank You
17
+
18
+ - Michael Buss RΓΈnne @man-trackunit
19
+
1
20
  ## 0.8.9 (2026-11-16)
2
21
 
3
22
  ### 🧱 Updated Dependencies
package/README.md CHANGED
@@ -77,15 +77,13 @@ All custom rules are enabled automatically through the presets under the `@track
77
77
 
78
78
  ### Folder structure
79
79
 
80
- The published `react` preset registers `@trackunit/structure` and does not enable these rules. Manager turns `require-hook-util-directory` on as `warn` in `eslint-client.config.cjs`.
81
-
82
- | Rule | Severity | Auto-fix | Description |
83
- | -------------------------------------------------- | -------- | -------- | ---------------------------------------------------------------------------------------------------- |
84
- | `@trackunit/structure/require-component-directory` | β€” | β€” | Not enabled by this preset. Manager enables it as `error` through the internal `folderStructure` preset and phases it in (`warn` until no project violates it). One directory per component under `components/`. |
85
- | `@trackunit/structure/require-nested-components-directory` | β€” | β€” | Not enabled by this preset. Manager enables it as `error` through the internal `folderStructure` preset and phases it in (`warn` until no project violates it). Exported child components live under `<Parent>/components/`. |
86
- | `@trackunit/structure/require-nested-components-privacy` | β€” | β€” | Not enabled. Review guidance for private `components/<Child>/` imports. |
87
- | `@trackunit/structure/require-hook-util-directory` | β€” | β€” | Not enabled by this preset. Manager enables it as `error` through the internal `folderStructure` preset and phases it in (`warn` until no project violates it). One directory per hook or util under `hooks/` or `utils/`. |
88
- | `@trackunit/structure/require-page-directory` | β€” | β€” | Not enabled. Review guidance for route `component`s in `pages/<Name>/<Name>.tsx`. |
80
+ The published `react` preset registers `@trackunit/structure` and does not enable these rules. Manager enables all three as `error` through the internal `folderStructure` preset and phases them in (`warn` until no project violates them).
81
+
82
+ | Rule | Severity | Auto-fix | Description |
83
+ | ---------------------------------------------------------- | -------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- |
84
+ | `@trackunit/structure/require-component-directory` | β€” | β€” | One folder per component under `components/`. Suffix files (`*.utils`, `*.types`, …) directly in `components/` or `pages/`, or named after another folder, are reported. |
85
+ | `@trackunit/structure/require-nested-components-directory` | β€” | β€” | Child components of `<Parent>/<Parent>.tsx` live under `<Parent>/components/`. Context modules and compound parts (`<Parent><Word>`) stay beside the parent. |
86
+ | `@trackunit/structure/require-hook-util-directory` | β€” | β€” | No source files directly inside a `hooks/` or `utils/` directory; each goes in a subfolder. Subfolder names are not checked. |
89
87
 
90
88
  ### Testing
91
89
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trackunit/eslint-plugin-trackunit",
3
- "version": "0.8.9",
3
+ "version": "0.9.0",
4
4
  "license": "SEE LICENSE IN LICENSE.txt",
5
5
  "repository": "https://github.com/Trackunit/manager",
6
6
  "engines": {
@@ -8,8 +8,9 @@
8
8
  },
9
9
  "dependencies": {
10
10
  "@nx/eslint-plugin": "23.3.0",
11
- "@trackunit/css-classname-utils": "0.0.127",
11
+ "@trackunit/css-classname-utils": "0.0.128",
12
12
  "@typescript-eslint/eslint-plugin": "8.71.2",
13
+ "@typescript-eslint/typescript-estree": "8.71.2",
13
14
  "@typescript-eslint/utils": "8.71.2",
14
15
  "eslint-config-prettier": "^10.1.8",
15
16
  "eslint-plugin-import": "2.32.0",
@@ -630,21 +630,15 @@ export declare const configs: {
630
630
  };
631
631
  "@trackunit/structure": {
632
632
  rules: {
633
- "require-component-directory": import("@typescript-eslint/utils/ts-eslint").RuleModule<"flatComponentSuffixFile" | "suffixComponentNameMismatch" | "flatComponentFile", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
633
+ "require-component-directory": import("@typescript-eslint/utils/ts-eslint").RuleModule<"flatComponentFile" | "flatComponentSuffixFile" | "suffixComponentNameMismatch", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
634
634
  name: string;
635
635
  };
636
- "require-hook-util-directory": import("@typescript-eslint/utils/ts-eslint").RuleModule<"flatHookOrUtilFile", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
636
+ "require-hook-util-directory": import("@typescript-eslint/utils/ts-eslint").RuleModule<"flatHookOrUtilFile" | "flatHookOrUtilFileWithSpec", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
637
637
  name: string;
638
638
  };
639
639
  "require-nested-components-directory": import("@typescript-eslint/utils/ts-eslint").RuleModule<"nestedComponentsNotInSubfolder", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
640
640
  name: string;
641
641
  };
642
- "require-nested-components-privacy": import("@typescript-eslint/utils/ts-eslint").RuleModule<"nestedComponentUsedOutsideParent", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
643
- name: string;
644
- };
645
- "require-page-directory": import("@typescript-eslint/utils/ts-eslint").RuleModule<"routeComponentOutsidePagesFolder", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
646
- name: string;
647
- };
648
642
  };
649
643
  };
650
644
  "@typescript-eslint": {
@@ -151,21 +151,15 @@ export declare const storybookPlugin: {
151
151
  */
152
152
  export declare const structurePlugin: {
153
153
  rules: {
154
- "require-component-directory": import("@typescript-eslint/utils/ts-eslint").RuleModule<"flatComponentSuffixFile" | "suffixComponentNameMismatch" | "flatComponentFile", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
154
+ "require-component-directory": import("@typescript-eslint/utils/ts-eslint").RuleModule<"flatComponentFile" | "flatComponentSuffixFile" | "suffixComponentNameMismatch", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
155
155
  name: string;
156
156
  };
157
- "require-hook-util-directory": import("@typescript-eslint/utils/ts-eslint").RuleModule<"flatHookOrUtilFile", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
157
+ "require-hook-util-directory": import("@typescript-eslint/utils/ts-eslint").RuleModule<"flatHookOrUtilFile" | "flatHookOrUtilFileWithSpec", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
158
158
  name: string;
159
159
  };
160
160
  "require-nested-components-directory": import("@typescript-eslint/utils/ts-eslint").RuleModule<"nestedComponentsNotInSubfolder", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
161
161
  name: string;
162
162
  };
163
- "require-nested-components-privacy": import("@typescript-eslint/utils/ts-eslint").RuleModule<"nestedComponentUsedOutsideParent", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
164
- name: string;
165
- };
166
- "require-page-directory": import("@typescript-eslint/utils/ts-eslint").RuleModule<"routeComponentOutsidePagesFolder", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
167
- name: string;
168
- };
169
163
  };
170
164
  };
171
165
  export { eslintConfigPrettier, globals, importPlugin, jsdoc, jsoncParser, noNull, nx, react, reactHooks, testingLibrary, tsParser, typescriptEslint, };
@@ -41,8 +41,6 @@ const require_csf_factory_1 = require("../rules/require-csf-factory/require-csf-
41
41
  const require_component_directory_1 = require("../rules/require-component-directory/require-component-directory");
42
42
  const require_hook_util_directory_1 = require("../rules/require-hook-util-directory/require-hook-util-directory");
43
43
  const require_nested_components_directory_1 = require("../rules/require-nested-components-directory/require-nested-components-directory");
44
- const require_nested_components_privacy_1 = require("../rules/require-nested-components-privacy/require-nested-components-privacy");
45
- const require_page_directory_1 = require("../rules/require-page-directory/require-page-directory");
46
44
  const require_storybook_folder_landing_page_1 = require("../rules/require-storybook-folder-landing-page/require-storybook-folder-landing-page");
47
45
  exports.localRulesPlugin = { rules: rules_map_1.rulesMap };
48
46
  /**
@@ -70,8 +68,6 @@ exports.structurePlugin = {
70
68
  "require-component-directory": require_component_directory_1.requireComponentDirectory,
71
69
  "require-hook-util-directory": require_hook_util_directory_1.requireHookUtilDirectory,
72
70
  "require-nested-components-directory": require_nested_components_directory_1.requireNestedComponentsDirectory,
73
- "require-nested-components-privacy": require_nested_components_privacy_1.requireNestedComponentsPrivacy,
74
- "require-page-directory": require_page_directory_1.requirePageDirectory,
75
71
  },
76
72
  };
77
73
  //# sourceMappingURL=plugins.js.map
@@ -120,21 +120,15 @@ export declare const reactPreset: (import("eslint").Linter.Config<import("eslint
120
120
  };
121
121
  "@trackunit/structure": {
122
122
  rules: {
123
- "require-component-directory": import("@typescript-eslint/utils/ts-eslint").RuleModule<"flatComponentSuffixFile" | "suffixComponentNameMismatch" | "flatComponentFile", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
123
+ "require-component-directory": import("@typescript-eslint/utils/ts-eslint").RuleModule<"flatComponentFile" | "flatComponentSuffixFile" | "suffixComponentNameMismatch", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
124
124
  name: string;
125
125
  };
126
- "require-hook-util-directory": import("@typescript-eslint/utils/ts-eslint").RuleModule<"flatHookOrUtilFile", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
126
+ "require-hook-util-directory": import("@typescript-eslint/utils/ts-eslint").RuleModule<"flatHookOrUtilFile" | "flatHookOrUtilFileWithSpec", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
127
127
  name: string;
128
128
  };
129
129
  "require-nested-components-directory": import("@typescript-eslint/utils/ts-eslint").RuleModule<"nestedComponentsNotInSubfolder", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
130
130
  name: string;
131
131
  };
132
- "require-nested-components-privacy": import("@typescript-eslint/utils/ts-eslint").RuleModule<"nestedComponentUsedOutsideParent", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
133
- name: string;
134
- };
135
- "require-page-directory": import("@typescript-eslint/utils/ts-eslint").RuleModule<"routeComponentOutsidePagesFolder", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
136
- name: string;
137
- };
138
132
  };
139
133
  };
140
134
  "@typescript-eslint": {
@@ -55,6 +55,7 @@ exports.oneComponentPerFile = createRule({
55
55
  const tracker = (0, component_utils_1.createComponentFileTracker)();
56
56
  return {
57
57
  FunctionDeclaration: tracker.onFunctionDeclaration,
58
+ ClassDeclaration: tracker.onClassDeclaration,
58
59
  VariableDeclarator: tracker.onVariableDeclarator,
59
60
  ExportNamedDeclaration: tracker.onExportNamedDeclaration,
60
61
  ExportDefaultDeclaration: tracker.onExportDefaultDeclaration,
@@ -13,164 +13,10 @@
13
13
  Object.defineProperty(exports, "__esModule", { value: true });
14
14
  exports.requireComponentDirectory = void 0;
15
15
  const tslib_1 = require("tslib");
16
- const utils_1 = require("@typescript-eslint/utils");
17
- const fs = tslib_1.__importStar(require("fs"));
18
16
  const path = tslib_1.__importStar(require("path"));
19
- const ts = tslib_1.__importStar(require("typescript"));
20
- const component_utils_1 = require("../../utils/component-utils");
21
17
  const create_rule_1 = require("../../utils/create-rule/create-rule");
18
+ const react_module_summary_1 = require("../../utils/react-module-summary/react-module-summary");
22
19
  const component_directory_structure_utils_1 = require("./utils/component-directory-structure-utils/component-directory-structure-utils");
23
- const COMPANION_FILE_PATTERN = /\.(spec|stories|docs|variants|module)\.(tsx|ts)$/;
24
- /**
25
- * Same exported-component detection as `one-component-per-file`. A PascalCase
26
- * file that does not export a component (a context object, a reducer) is not a
27
- * component file. Specs, stories, docs, and variants still move with the component.
28
- */
29
- const fileExportsComponent = (program) => {
30
- const tracker = (0, component_utils_1.createComponentFileTracker)();
31
- for (const statement of program.body) {
32
- if (statement.type === utils_1.AST_NODE_TYPES.ExportNamedDeclaration) {
33
- tracker.onExportNamedDeclaration(statement);
34
- const declaration = statement.declaration;
35
- if (declaration?.type === utils_1.AST_NODE_TYPES.FunctionDeclaration) {
36
- tracker.onFunctionDeclaration(declaration);
37
- }
38
- if (declaration?.type === utils_1.AST_NODE_TYPES.VariableDeclaration) {
39
- for (const declarator of declaration.declarations) {
40
- tracker.onVariableDeclarator(declarator);
41
- }
42
- }
43
- if (exportsClassComponent(declaration)) {
44
- return true;
45
- }
46
- continue;
47
- }
48
- if (statement.type === utils_1.AST_NODE_TYPES.FunctionDeclaration) {
49
- tracker.onFunctionDeclaration(statement);
50
- continue;
51
- }
52
- if (statement.type === utils_1.AST_NODE_TYPES.VariableDeclaration) {
53
- for (const declarator of statement.declarations) {
54
- tracker.onVariableDeclarator(declarator);
55
- }
56
- continue;
57
- }
58
- if (statement.type === utils_1.AST_NODE_TYPES.ExportDefaultDeclaration) {
59
- tracker.onExportDefaultDeclaration(statement);
60
- if (statement.declaration.type === utils_1.AST_NODE_TYPES.FunctionDeclaration) {
61
- tracker.onFunctionDeclaration(statement.declaration);
62
- }
63
- if (exportsClassComponent(statement.declaration)) {
64
- return true;
65
- }
66
- }
67
- }
68
- return tracker.getExportedComponents().length > 0;
69
- };
70
- const exportsClassComponent = (declaration) => {
71
- if (declaration?.type !== utils_1.AST_NODE_TYPES.ClassDeclaration) {
72
- return false;
73
- }
74
- const name = declaration.id?.name;
75
- return name === undefined || /^[A-Z]/.test(name);
76
- };
77
- /**
78
- * A spec, story, or docs file moves with its component. When the sibling owner
79
- * exists and does not export a component (a context, a reducer), the companion
80
- * stays where it is.
81
- */
82
- const REACT_WRAPPER_NAMES = new Set(["memo", "forwardRef"]);
83
- const isComponentInitializer = (expression) => {
84
- if (ts.isArrowFunction(expression) || ts.isFunctionExpression(expression)) {
85
- return true;
86
- }
87
- return (ts.isCallExpression(expression) &&
88
- ts.isIdentifier(expression.expression) &&
89
- REACT_WRAPPER_NAMES.has(expression.expression.text));
90
- };
91
- const statementIsExported = (node) => ts.canHaveModifiers(node) &&
92
- ts.getModifiers(node)?.some(modifier => modifier.kind === ts.SyntaxKind.ExportKeyword) === true;
93
- /**
94
- * Reads a companion's sibling owner. A context or reducer does not count as a component.
95
- * `export default Foo` and `export { Foo }` count when `Foo` is a top-level PascalCase
96
- * function, class, or component arrow.
97
- */
98
- const pathExportsComponent = (ownerPath) => {
99
- let content;
100
- try {
101
- content = fs.readFileSync(ownerPath, "utf8");
102
- }
103
- catch {
104
- return false;
105
- }
106
- const sourceFile = ts.createSourceFile(ownerPath, content, ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX);
107
- const localComponentNames = new Set();
108
- const exportedNames = [];
109
- let exportsComponent = false;
110
- let defaultExportName;
111
- let defaultExportIsComponent = false;
112
- const rememberComponent = (name) => {
113
- if (!/^[A-Z]/.test(name)) {
114
- return false;
115
- }
116
- localComponentNames.add(name);
117
- return true;
118
- };
119
- for (const statement of sourceFile.statements) {
120
- if ((ts.isFunctionDeclaration(statement) || ts.isClassDeclaration(statement)) && statement.name !== undefined) {
121
- if (rememberComponent(statement.name.text) && statementIsExported(statement)) {
122
- exportsComponent = true;
123
- }
124
- }
125
- if (ts.isVariableStatement(statement)) {
126
- for (const declaration of statement.declarationList.declarations) {
127
- if (ts.isIdentifier(declaration.name) &&
128
- declaration.initializer !== undefined &&
129
- isComponentInitializer(declaration.initializer) &&
130
- rememberComponent(declaration.name.text) &&
131
- statementIsExported(statement)) {
132
- exportsComponent = true;
133
- }
134
- }
135
- }
136
- if (ts.isExportAssignment(statement) && !statement.isExportEquals) {
137
- if (ts.isIdentifier(statement.expression)) {
138
- defaultExportName = statement.expression.text;
139
- }
140
- if (isComponentInitializer(statement.expression)) {
141
- defaultExportIsComponent = true;
142
- }
143
- }
144
- if (ts.isExportDeclaration(statement) &&
145
- statement.exportClause !== undefined &&
146
- ts.isNamedExports(statement.exportClause)) {
147
- for (const element of statement.exportClause.elements) {
148
- const localName = element.propertyName ?? element.name;
149
- if (ts.isIdentifier(localName)) {
150
- exportedNames.push(localName.text);
151
- }
152
- }
153
- }
154
- }
155
- if (defaultExportIsComponent || (defaultExportName !== undefined && localComponentNames.has(defaultExportName))) {
156
- return true;
157
- }
158
- return exportsComponent || exportedNames.some(name => localComponentNames.has(name));
159
- };
160
- const companionOwnerExportsComponent = (filePath) => {
161
- const fileName = path.basename(filePath);
162
- if (!COMPANION_FILE_PATTERN.test(fileName)) {
163
- return undefined;
164
- }
165
- const ownerBaseName = fileName.replace(COMPANION_FILE_PATTERN, "");
166
- const ownerPath = [`${ownerBaseName}.tsx`, `${ownerBaseName}.ts`]
167
- .map(candidate => path.join(path.dirname(filePath), candidate))
168
- .find(candidate => fs.existsSync(candidate));
169
- if (ownerPath === undefined) {
170
- return undefined;
171
- }
172
- return pathExportsComponent(ownerPath);
173
- };
174
20
  exports.requireComponentDirectory = (0, create_rule_1.createRule)({
175
21
  name: "require-component-directory",
176
22
  meta: {
@@ -181,58 +27,41 @@ exports.requireComponentDirectory = (0, create_rule_1.createRule)({
181
27
  schema: [],
182
28
  messages: {
183
29
  flatComponentFile: "Component files must live in their own folder. Move `{{file}}` to `{{suggestedPath}}`.",
184
- flatComponentSuffixFile: "Suffix files must live inside their component folder. Move `{{file}}` to `{{suggestedPath}}`.",
185
- suffixComponentNameMismatch: "Suffix file `{{file}}` stays with `{{folderName}}`. Move it to `{{suggestedPath}}`.",
30
+ flatComponentSuffixFile: "Suffix files must not sit directly in `{{moduleDirectoryName}}/`. Move `{{file}}` to `{{suggestedPath}}`.",
31
+ suffixComponentNameMismatch: "Only `{{folderName}}.*` suffix files belong in `{{folderName}}/`. Move `{{file}}` to `{{suggestedPath}}`.",
186
32
  },
187
33
  },
188
34
  defaultOptions: [],
189
35
  create(context) {
190
36
  return {
191
37
  Program(node) {
192
- const violation = (0, component_directory_structure_utils_1.analyzeComponentFolderStructure)(context.filename);
38
+ const violation = (0, component_directory_structure_utils_1.analyzeComponentFolderStructure)(context.filename, () => (0, react_module_summary_1.summarizeReactModule)(node));
193
39
  if (violation === null) {
194
40
  return;
195
41
  }
196
- const fileName = path.basename(context.filename);
197
- if (violation.kind === "flatComponentFile" && companionOwnerExportsComponent(context.filename) === false) {
198
- return;
199
- }
200
- if (violation.kind === "flatComponentFile" &&
201
- !COMPANION_FILE_PATTERN.test(fileName) &&
202
- !fileExportsComponent(node)) {
203
- return;
204
- }
205
- if (violation.kind === "flatComponentSuffixFile") {
206
- context.report({
207
- node,
208
- messageId: "flatComponentSuffixFile",
209
- data: {
210
- file: fileName,
211
- suggestedPath: (0, component_directory_structure_utils_1.getSuggestedComponentFolderPath)(context.filename, violation.componentName),
212
- },
213
- });
214
- return;
215
- }
216
- if (violation.kind === "suffixComponentNameMismatch") {
217
- context.report({
218
- node,
219
- messageId: "suffixComponentNameMismatch",
220
- data: {
221
- file: fileName,
222
- folderName: violation.folderName,
223
- suggestedPath: (0, component_directory_structure_utils_1.getSuggestedSuffixRolePath)(context.filename, violation.componentName, violation.suffix),
224
- },
225
- });
226
- return;
42
+ const file = path.basename(context.filename);
43
+ const suggestedPath = (0, component_directory_structure_utils_1.formatSuggestedPath)(context.filename, violation.targetPath);
44
+ switch (violation.kind) {
45
+ case "flatComponentFile":
46
+ context.report({ node, messageId: "flatComponentFile", data: { file, suggestedPath } });
47
+ return;
48
+ case "flatComponentSuffixFile":
49
+ context.report({
50
+ node,
51
+ messageId: "flatComponentSuffixFile",
52
+ data: { file, suggestedPath, moduleDirectoryName: violation.moduleDirectoryName },
53
+ });
54
+ return;
55
+ case "suffixComponentNameMismatch":
56
+ context.report({
57
+ node,
58
+ messageId: "suffixComponentNameMismatch",
59
+ data: { file, suggestedPath, folderName: violation.folderName },
60
+ });
61
+ return;
62
+ default:
63
+ throw new Error(`${violation} is not known`);
227
64
  }
228
- context.report({
229
- node,
230
- messageId: "flatComponentFile",
231
- data: {
232
- file: fileName,
233
- suggestedPath: (0, component_directory_structure_utils_1.getSuggestedComponentFolderPath)(context.filename, violation.componentName),
234
- },
235
- });
236
65
  },
237
66
  };
238
67
  },
@@ -1,34 +1,34 @@
1
- type ComponentFolderViolation = {
1
+ import { ReactModuleSummary } from "../../../../utils/react-module-summary/react-module-summary";
2
+ export type ComponentFolderViolation = {
3
+ /** A component file, or its spec/story/docs, directly in `components/`. */
4
+ readonly kind: "flatComponentFile";
5
+ readonly targetPath: string;
6
+ } | {
7
+ /** A `*.utils|types|constants|helpers` file directly in `components/` or `pages/`. */
2
8
  readonly kind: "flatComponentSuffixFile";
3
- readonly componentName: string;
4
- readonly suffix: string;
9
+ readonly moduleDirectoryName: string;
10
+ readonly targetPath: string;
5
11
  } | {
12
+ /** A suffix file inside `components/<Folder>/` or `pages/<Folder>/` named after something else. */
6
13
  readonly kind: "suffixComponentNameMismatch";
7
- readonly componentName: string;
8
14
  readonly folderName: string;
9
- readonly suffix: string;
10
- } | {
11
- readonly kind: "flatComponentFile";
12
- readonly componentName: string;
15
+ readonly targetPath: string;
13
16
  };
14
17
  /**
15
- * Returns a folder-structure violation when `filePath` is a direct child of a
16
- * `components/` directory (including scoped `components/` folders), or
17
- * when a suffix file inside a component folder does not match that folder name,
18
- * or `null` when the path is allowed.
19
- */
20
- export declare const analyzeComponentFolderStructure: (filePath: string) => ComponentFolderViolation | null;
21
- /**
22
- * `components/Button.tsx` β†’ `components/Button/Button.tsx`, relative to the
23
- * `components/` directory's parent. Every violation this rule reports lives
24
- * either directly under `components/` or one level below it, so the target is
25
- * the same in both cases.
18
+ * Returns a folder-structure violation, or `null` when the path is allowed:
19
+ *
20
+ * - a file directly in `components/` that exports a component (by `one-component-per-file`'s
21
+ * detection), or the spec/story/docs of one, whatever the file name's case
22
+ * - a `*.utils|types|constants|helpers` file directly in `components/` or `pages/`, whatever its case,
23
+ * except one in `pages/` beside the flat page it belongs to
24
+ * - a suffix file inside `components/<Folder>/` or `pages/<Folder>/` whose name is not `<Folder>`,
25
+ * ignoring case
26
+ *
27
+ * `readOwnSummary` summarizes the file itself. A rule passes its parsed program; a codemod reads the file.
26
28
  */
27
- export declare const getSuggestedComponentFolderPath: (filePath: string, componentName: string) => string;
29
+ export declare const analyzeComponentFolderStructure: (filePath: string, readOwnSummary?: () => ReactModuleSummary) => ComponentFolderViolation | null;
28
30
  /**
29
- * A suffix file inside `components/<Parent>/` that does not match `<Parent>`
30
- * stays with that parent. Suggest `components/<Parent>/<role>/<name>/<name>.ts`
31
- * rather than a new component folder named after the suffix prefix.
31
+ * `targetPath` relative to the library's `src/`, so `features/Auth/components/LoginForm/LoginForm.tsx`
32
+ * is not confused with another `components/` folder. Falls back to the file's grandparent outside `src/`.
32
33
  */
33
- export declare const getSuggestedSuffixRolePath: (filePath: string, componentName: string, suffix: string) => string;
34
- export {};
34
+ export declare const formatSuggestedPath: (filePath: string, targetPath: string) => string;