@trackunit/eslint-plugin-trackunit 0.7.15-alpha-86dd3430ebe.0 → 0.7.15

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 (37) hide show
  1. package/CHANGELOG.md +35 -0
  2. package/README.md +12 -0
  3. package/package.json +2 -2
  4. package/src/index.d.ts +1 -1
  5. package/src/index.js +2 -1
  6. package/src/lib/config/index.d.ts +20 -0
  7. package/src/lib/config/plugins.d.ts +23 -0
  8. package/src/lib/config/plugins.js +19 -1
  9. package/src/lib/config/presets/react.d.ts +20 -0
  10. package/src/lib/config/presets/react.js +4 -0
  11. package/src/lib/rules/no-unused-translation-key/used-strings.js +1 -1
  12. package/src/lib/{utils → rules/no-unused-translation-key/utils/program-utils}/program-utils.js +1 -1
  13. package/src/lib/rules/require-component-directory/require-component-directory.d.ts +16 -0
  14. package/src/lib/rules/require-component-directory/require-component-directory.js +240 -0
  15. package/src/lib/rules/require-component-directory/utils/component-directory-structure-utils/component-directory-structure-utils.d.ts +34 -0
  16. package/src/lib/rules/require-component-directory/utils/component-directory-structure-utils/component-directory-structure-utils.js +114 -0
  17. package/src/lib/rules/require-hook-util-directory/require-hook-util-directory.d.ts +9 -0
  18. package/src/lib/rules/require-hook-util-directory/require-hook-util-directory.js +50 -0
  19. package/src/lib/rules/require-hook-util-directory/utils/hook-util-directory-structure-utils/hook-util-directory-structure-utils.d.ts +15 -0
  20. package/src/lib/rules/require-hook-util-directory/utils/hook-util-directory-structure-utils/hook-util-directory-structure-utils.js +44 -0
  21. package/src/lib/rules/require-nested-components-directory/require-nested-components-directory.d.ts +9 -0
  22. package/src/lib/rules/require-nested-components-directory/require-nested-components-directory.js +44 -0
  23. package/src/lib/rules/require-nested-components-directory/utils/nested-component-directory-utils/nested-component-directory-utils.d.ts +17 -0
  24. package/src/lib/rules/require-nested-components-directory/utils/nested-component-directory-utils/nested-component-directory-utils.js +206 -0
  25. package/src/lib/rules/require-nested-components-privacy/require-nested-components-privacy.d.ts +9 -0
  26. package/src/lib/rules/require-nested-components-privacy/require-nested-components-privacy.js +63 -0
  27. package/src/lib/rules/require-nested-components-privacy/utils/nested-component-privacy-utils/nested-component-privacy-utils.d.ts +14 -0
  28. package/src/lib/rules/require-nested-components-privacy/utils/nested-component-privacy-utils/nested-component-privacy-utils.js +62 -0
  29. package/src/lib/rules/require-page-directory/require-page-directory.d.ts +8 -0
  30. package/src/lib/rules/require-page-directory/require-page-directory.js +44 -0
  31. package/src/lib/rules/require-page-directory/utils/page-directory-structure-utils/page-directory-structure-utils.d.ts +18 -0
  32. package/src/lib/rules/require-page-directory/utils/page-directory-structure-utils/page-directory-structure-utils.js +159 -0
  33. package/src/lib/utils/create-rule/create-rule.d.ts +7 -0
  34. package/src/lib/utils/create-rule/create-rule.js +9 -0
  35. package/src/lib/utils/source-tree-utils/source-tree-utils.d.ts +43 -0
  36. package/src/lib/utils/source-tree-utils/source-tree-utils.js +136 -0
  37. /package/src/lib/{utils → rules/no-unused-translation-key/utils/program-utils}/program-utils.d.ts +0 -0
package/CHANGELOG.md CHANGED
@@ -1,3 +1,38 @@
1
+ ## 0.7.15 (2026-11-04)
2
+
3
+ ### 🧱 Updated Dependencies
4
+
5
+ - Updated css-classname-utils to 0.0.108
6
+ - Updated shared-utils to 1.22.27
7
+
8
+ ## 0.7.14 (2026-11-03)
9
+
10
+ ### 🚀 Features
11
+
12
+ - **folder-structure:** add the nested component folder rule ([093159241c7](https://github.com/Trackunit/manager/commit/093159241c7))
13
+ - **folder-structure:** add the component folder rule ([e7f8aed8552](https://github.com/Trackunit/manager/commit/e7f8aed8552))
14
+ - **folder-structure:** warn on flat hooks and utils ([c5fb3bd571a](https://github.com/Trackunit/manager/commit/c5fb3bd571a))
15
+ - **folder-structure:** add the hook and util folder rule ([ec1f507fdf7](https://github.com/Trackunit/manager/commit/ec1f507fdf7))
16
+
17
+ ### 🩹 Fixes
18
+
19
+ - **folder-structure:** limit the singular-stem exemption to flat files ([b26fdd6415d](https://github.com/Trackunit/manager/commit/b26fdd6415d))
20
+ - **folder-structure:** drop the Tabs false positive from the react-components budget ([76f4019e8c1](https://github.com/Trackunit/manager/commit/76f4019e8c1))
21
+ - **folder-structure:** recognize real child components and plural compound parts ([3f87048dec9](https://github.com/Trackunit/manager/commit/3f87048dec9))
22
+ - **folder-structure:** move specs whose owner is exported by name ([a48fb7c8504](https://github.com/Trackunit/manager/commit/a48fb7c8504))
23
+ - **folder-structure:** treat class components and docs as part of the component ([9fcf54058df](https://github.com/Trackunit/manager/commit/9fcf54058df))
24
+ - **folder-structure:** correct max-warnings budgets for new rule violations ([c168e664b92](https://github.com/Trackunit/manager/commit/c168e664b92))
25
+ - **folder-structure:** cover the imports the hook codemod used to miss ([a7ef50d5a69](https://github.com/Trackunit/manager/commit/a7ef50d5a69))
26
+
27
+ ### 🧱 Updated Dependencies
28
+
29
+ - Updated css-classname-utils to 0.0.107
30
+ - Updated shared-utils to 1.22.26
31
+
32
+ ### ❤️ Thank You
33
+
34
+ - fhg @FemkeTrackunit
35
+
1
36
  ## 0.7.13 (2026-11-03)
2
37
 
3
38
  ### 🧱 Updated Dependencies
package/README.md CHANGED
@@ -71,6 +71,18 @@ All custom rules are enabled automatically through the presets under the `@track
71
71
  | `@trackunit/one-component-per-file` | error | — | Enforces at most one exported React component per file. `exportedOnly: false` restricts to one total. |
72
72
  | `@trackunit/component-name-matches-filename` | error | — | Enforces that exported React component names match the filename (e.g. `Foo` in `Foo.tsx`). |
73
73
 
74
+ ### Folder structure
75
+
76
+ 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`.
77
+
78
+ | Rule | Severity | Auto-fix | Description |
79
+ | -------------------------------------------------- | -------- | -------- | ---------------------------------------------------------------------------------------------------- |
80
+ | `@trackunit/structure/require-component-directory` | — | — | Not enabled by this preset. Manager turns it on as `warn` in `eslint-client.config.cjs`. One directory per component under `components/`. |
81
+ | `@trackunit/structure/require-nested-components-directory` | — | — | Not enabled by this preset. Manager turns it on as `warn` in `eslint-client.config.cjs`. Exported child components live under `<Parent>/components/`. |
82
+ | `@trackunit/structure/require-nested-components-privacy` | — | — | Not enabled. Review guidance for private `components/<Child>/` imports. |
83
+ | `@trackunit/structure/require-hook-util-directory` | — | — | Not enabled by this preset. Manager turns it on as `warn` in `eslint-client.config.cjs`. One directory per hook or util under `hooks/` or `utils/`. |
84
+ | `@trackunit/structure/require-page-directory` | — | — | Not enabled. Review guidance for route `component`s in `pages/<Name>/<Name>.tsx`. |
85
+
74
86
  ### Testing
75
87
 
76
88
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trackunit/eslint-plugin-trackunit",
3
- "version": "0.7.15-alpha-86dd3430ebe.0",
3
+ "version": "0.7.15",
4
4
  "license": "SEE LICENSE IN LICENSE.txt",
5
5
  "repository": "https://github.com/Trackunit/manager",
6
6
  "engines": {
@@ -8,7 +8,7 @@
8
8
  },
9
9
  "dependencies": {
10
10
  "@nx/eslint-plugin": "23.3.0",
11
- "@trackunit/css-classname-utils": "0.0.108-alpha-86dd3430ebe.0",
11
+ "@trackunit/css-classname-utils": "0.0.108",
12
12
  "@typescript-eslint/eslint-plugin": "8.71.2",
13
13
  "@typescript-eslint/utils": "8.71.2",
14
14
  "eslint-config-prettier": "^10.1.8",
package/src/index.d.ts CHANGED
@@ -7,4 +7,4 @@ export type { MatchCriteria, ProjectMetadata } from "./lib/utils/nx-utils";
7
7
  export { strictJsdocRules } from "./lib/config/fragments/jsdoc-rules";
8
8
  export { publishedLibRestrictedImports, restrictedImportRules } from "./lib/config/fragments/restricted-imports";
9
9
  export { storyFileGlobs, storyRelaxations } from "./lib/config/fragments/story-relaxations";
10
- export { globals, jsdoc, localRulesPlugin, nx, reactHooks, storybookPlugin, tsParser } from "./lib/config/plugins";
10
+ export { globals, jsdoc, localRulesPlugin, nx, reactHooks, storybookPlugin, structurePlugin, tsParser, } from "./lib/config/plugins";
package/src/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.tsParser = exports.storybookPlugin = exports.reactHooks = exports.nx = exports.localRulesPlugin = exports.jsdoc = exports.globals = exports.storyRelaxations = exports.storyFileGlobs = exports.restrictedImportRules = exports.publishedLibRestrictedImports = exports.strictJsdocRules = exports.findMonorepoRoot = exports.createSkipWhen = exports.configs = void 0;
3
+ exports.tsParser = exports.structurePlugin = exports.storybookPlugin = exports.reactHooks = exports.nx = exports.localRulesPlugin = exports.jsdoc = exports.globals = exports.storyRelaxations = exports.storyFileGlobs = exports.restrictedImportRules = exports.publishedLibRestrictedImports = exports.strictJsdocRules = exports.findMonorepoRoot = exports.createSkipWhen = exports.configs = void 0;
4
4
  const rules_map_1 = require("./lib/rules-map");
5
5
  exports.default = rules_map_1.rulesMap;
6
6
  var config_1 = require("./lib/config");
@@ -24,5 +24,6 @@ Object.defineProperty(exports, "localRulesPlugin", { enumerable: true, get: func
24
24
  Object.defineProperty(exports, "nx", { enumerable: true, get: function () { return plugins_1.nx; } });
25
25
  Object.defineProperty(exports, "reactHooks", { enumerable: true, get: function () { return plugins_1.reactHooks; } });
26
26
  Object.defineProperty(exports, "storybookPlugin", { enumerable: true, get: function () { return plugins_1.storybookPlugin; } });
27
+ Object.defineProperty(exports, "structurePlugin", { enumerable: true, get: function () { return plugins_1.structurePlugin; } });
27
28
  Object.defineProperty(exports, "tsParser", { enumerable: true, get: function () { return plugins_1.tsParser; } });
28
29
  //# sourceMappingURL=index.js.map
@@ -616,6 +616,25 @@ export declare const configs: {
616
616
  };
617
617
  };
618
618
  };
619
+ "@trackunit/structure": {
620
+ rules: {
621
+ "require-component-directory": import("@typescript-eslint/utils/ts-eslint").RuleModule<"flatComponentSuffixFile" | "suffixComponentNameMismatch" | "flatComponentFile", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
622
+ name: string;
623
+ };
624
+ "require-hook-util-directory": import("@typescript-eslint/utils/ts-eslint").RuleModule<"flatHookOrUtilFile", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
625
+ name: string;
626
+ };
627
+ "require-nested-components-directory": import("@typescript-eslint/utils/ts-eslint").RuleModule<"nestedComponentsNotInSubfolder", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
628
+ name: string;
629
+ };
630
+ "require-nested-components-privacy": import("@typescript-eslint/utils/ts-eslint").RuleModule<"nestedComponentUsedOutsideParent", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
631
+ name: string;
632
+ };
633
+ "require-page-directory": import("@typescript-eslint/utils/ts-eslint").RuleModule<"routeComponentOutsidePagesFolder", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
634
+ name: string;
635
+ };
636
+ };
637
+ };
619
638
  "@typescript-eslint": {
620
639
  configs: Record<string, import("@typescript-eslint/utils/ts-eslint").ClassicConfig.Config>;
621
640
  meta: import("typescript-eslint").FlatConfig.PluginMeta;
@@ -1113,6 +1132,7 @@ export declare const configs: {
1113
1132
  };
1114
1133
  "@trackunit"?: undefined;
1115
1134
  "@trackunit/storybook"?: undefined;
1135
+ "@trackunit/structure"?: undefined;
1116
1136
  "@typescript-eslint"?: undefined;
1117
1137
  jsdoc?: undefined;
1118
1138
  react?: undefined;
@@ -142,4 +142,27 @@ export declare const storybookPlugin: {
142
142
  };
143
143
  };
144
144
  };
145
+ /**
146
+ * Shared plugin object for the `@trackunit/structure` namespace. The published react preset
147
+ * registers this object and does not enable the rules. Must stay a single object reference.
148
+ */
149
+ export declare const structurePlugin: {
150
+ rules: {
151
+ "require-component-directory": import("@typescript-eslint/utils/ts-eslint").RuleModule<"flatComponentSuffixFile" | "suffixComponentNameMismatch" | "flatComponentFile", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
152
+ name: string;
153
+ };
154
+ "require-hook-util-directory": import("@typescript-eslint/utils/ts-eslint").RuleModule<"flatHookOrUtilFile", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
155
+ name: string;
156
+ };
157
+ "require-nested-components-directory": import("@typescript-eslint/utils/ts-eslint").RuleModule<"nestedComponentsNotInSubfolder", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
158
+ name: string;
159
+ };
160
+ "require-nested-components-privacy": import("@typescript-eslint/utils/ts-eslint").RuleModule<"nestedComponentUsedOutsideParent", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
161
+ name: string;
162
+ };
163
+ "require-page-directory": import("@typescript-eslint/utils/ts-eslint").RuleModule<"routeComponentOutsidePagesFolder", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
164
+ name: string;
165
+ };
166
+ };
167
+ };
145
168
  export { eslintConfigPrettier, globals, importPlugin, jsdoc, jsoncParser, noNull, nx, react, reactHooks, testingLibrary, tsParser, typescriptEslint, };
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.typescriptEslint = exports.tsParser = exports.testingLibrary = exports.reactHooks = exports.react = exports.nx = exports.noNull = exports.jsoncParser = exports.jsdoc = exports.importPlugin = exports.globals = exports.eslintConfigPrettier = exports.storybookPlugin = exports.localRulesPlugin = void 0;
3
+ exports.typescriptEslint = exports.tsParser = exports.testingLibrary = exports.reactHooks = exports.react = exports.nx = exports.noNull = exports.jsoncParser = exports.jsdoc = exports.importPlugin = exports.globals = exports.eslintConfigPrettier = exports.structurePlugin = exports.storybookPlugin = exports.localRulesPlugin = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  /**
6
6
  * Shared plugin instances for ESLint flat config.
@@ -38,6 +38,11 @@ exports.jsoncParser = jsoncParser;
38
38
  const rules_map_1 = require("../rules-map");
39
39
  const no_local_meta_component_1 = require("../rules/no-local-meta-component/no-local-meta-component");
40
40
  const require_csf_factory_1 = require("../rules/require-csf-factory/require-csf-factory");
41
+ const require_component_directory_1 = require("../rules/require-component-directory/require-component-directory");
42
+ const require_hook_util_directory_1 = require("../rules/require-hook-util-directory/require-hook-util-directory");
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");
41
46
  const require_storybook_folder_landing_page_1 = require("../rules/require-storybook-folder-landing-page/require-storybook-folder-landing-page");
42
47
  exports.localRulesPlugin = { rules: rules_map_1.rulesMap };
43
48
  /**
@@ -56,4 +61,17 @@ exports.storybookPlugin = {
56
61
  "no-local-meta-component": no_local_meta_component_1.noLocalMetaComponent,
57
62
  },
58
63
  };
64
+ /**
65
+ * Shared plugin object for the `@trackunit/structure` namespace. The published react preset
66
+ * registers this object and does not enable the rules. Must stay a single object reference.
67
+ */
68
+ exports.structurePlugin = {
69
+ rules: {
70
+ "require-component-directory": require_component_directory_1.requireComponentDirectory,
71
+ "require-hook-util-directory": require_hook_util_directory_1.requireHookUtilDirectory,
72
+ "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
+ },
76
+ };
59
77
  //# sourceMappingURL=plugins.js.map
@@ -115,6 +115,25 @@ export declare const reactPreset: (import("eslint").Linter.Config<import("eslint
115
115
  };
116
116
  };
117
117
  };
118
+ "@trackunit/structure": {
119
+ rules: {
120
+ "require-component-directory": import("@typescript-eslint/utils/ts-eslint").RuleModule<"flatComponentSuffixFile" | "suffixComponentNameMismatch" | "flatComponentFile", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
121
+ name: string;
122
+ };
123
+ "require-hook-util-directory": import("@typescript-eslint/utils/ts-eslint").RuleModule<"flatHookOrUtilFile", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
124
+ name: string;
125
+ };
126
+ "require-nested-components-directory": import("@typescript-eslint/utils/ts-eslint").RuleModule<"nestedComponentsNotInSubfolder", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
127
+ name: string;
128
+ };
129
+ "require-nested-components-privacy": import("@typescript-eslint/utils/ts-eslint").RuleModule<"nestedComponentUsedOutsideParent", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
130
+ name: string;
131
+ };
132
+ "require-page-directory": import("@typescript-eslint/utils/ts-eslint").RuleModule<"routeComponentOutsidePagesFolder", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
133
+ name: string;
134
+ };
135
+ };
136
+ };
118
137
  "@typescript-eslint": {
119
138
  configs: Record<string, import("@typescript-eslint/utils/ts-eslint").ClassicConfig.Config>;
120
139
  meta: import("typescript-eslint").FlatConfig.PluginMeta;
@@ -612,6 +631,7 @@ export declare const reactPreset: (import("eslint").Linter.Config<import("eslint
612
631
  };
613
632
  "@trackunit"?: undefined;
614
633
  "@trackunit/storybook"?: undefined;
634
+ "@trackunit/structure"?: undefined;
615
635
  "@typescript-eslint"?: undefined;
616
636
  jsdoc?: undefined;
617
637
  react?: undefined;
@@ -39,6 +39,10 @@ exports.reactPreset = [
39
39
  // `@trackunit/storybook/require-folder-landing-page` without extra config. This preset
40
40
  // intentionally does not enable any rule under that key itself.
41
41
  "@trackunit/storybook": plugins_1.storybookPlugin,
42
+ // Registers `@trackunit/structure` so opt-in consumers can reference
43
+ // `@trackunit/structure/require-component-directory` and the other layout rules
44
+ // without extra config. This preset does not enable any rule under that key.
45
+ "@trackunit/structure": plugins_1.structurePlugin,
42
46
  "@typescript-eslint": plugins_1.typescriptEslint,
43
47
  jsdoc: plugins_1.jsdoc,
44
48
  react: plugins_1.react,
@@ -4,8 +4,8 @@ exports.isTranslationKeyUsed = exports.getUsedTranslationStrings = exports.build
4
4
  const tslib_1 = require("tslib");
5
5
  const fs = tslib_1.__importStar(require("fs"));
6
6
  const path = tslib_1.__importStar(require("path"));
7
- const program_utils_1 = require("../../utils/program-utils");
8
7
  const expand_dynamic_key_1 = require("./expand-dynamic-key");
8
+ const program_utils_1 = require("./utils/program-utils/program-utils");
9
9
  const EXCLUDED_DIRS = new Set(["node_modules", "dist", "generated", "coverage", ".git"]);
10
10
  const SCANNED_EXTENSIONS = [".ts", ".tsx"];
11
11
  /**
@@ -4,7 +4,7 @@ exports.clearProgramCache = exports.getLibraryProgram = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const path = tslib_1.__importStar(require("path"));
6
6
  const ts = tslib_1.__importStar(require("typescript"));
7
- const file_utils_1 = require("./file-utils");
7
+ const file_utils_1 = require("../../../../utils/file-utils");
8
8
  /**
9
9
  * Cache for library Programs to avoid re-parsing tsconfig and rebuilding the (expensive) `ts.Program`
10
10
  * on every call. Key is the resolved `projectDir` passed in, value is the built program/checker pair.
@@ -0,0 +1,16 @@
1
+ /**
2
+ * ESLint rule: require-component-directory
3
+ *
4
+ * Disallows a flat `components/` directory where component files, specs, stories,
5
+ * and related UI files live directly alongside each other.
6
+ *
7
+ * Each component must have its own PascalCase folder:
8
+ * `components/Button/Button.tsx`, not `components/Button.tsx`.
9
+ *
10
+ * See README.md for incorrect/correct examples.
11
+ */
12
+ type MessageIds = "flatComponentFile" | "flatComponentSuffixFile" | "suffixComponentNameMismatch";
13
+ export declare const requireComponentDirectory: import("@typescript-eslint/utils/ts-eslint").RuleModule<MessageIds, [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
14
+ name: string;
15
+ };
16
+ export {};
@@ -0,0 +1,240 @@
1
+ "use strict";
2
+ /**
3
+ * ESLint rule: require-component-directory
4
+ *
5
+ * Disallows a flat `components/` directory where component files, specs, stories,
6
+ * and related UI files live directly alongside each other.
7
+ *
8
+ * Each component must have its own PascalCase folder:
9
+ * `components/Button/Button.tsx`, not `components/Button.tsx`.
10
+ *
11
+ * See README.md for incorrect/correct examples.
12
+ */
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ exports.requireComponentDirectory = void 0;
15
+ const tslib_1 = require("tslib");
16
+ const utils_1 = require("@typescript-eslint/utils");
17
+ const fs = tslib_1.__importStar(require("fs"));
18
+ 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
+ const create_rule_1 = require("../../utils/create-rule/create-rule");
22
+ 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
+ exports.requireComponentDirectory = (0, create_rule_1.createRule)({
175
+ name: "require-component-directory",
176
+ meta: {
177
+ type: "suggestion",
178
+ docs: {
179
+ description: "Require one folder per component under `components/`.",
180
+ },
181
+ schema: [],
182
+ messages: {
183
+ 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}}`.",
186
+ },
187
+ },
188
+ defaultOptions: [],
189
+ create(context) {
190
+ return {
191
+ Program(node) {
192
+ const violation = (0, component_directory_structure_utils_1.analyzeComponentFolderStructure)(context.filename);
193
+ if (violation === null) {
194
+ return;
195
+ }
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;
227
+ }
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
+ },
237
+ };
238
+ },
239
+ });
240
+ //# sourceMappingURL=require-component-directory.js.map
@@ -0,0 +1,34 @@
1
+ type ComponentFolderViolation = {
2
+ readonly kind: "flatComponentSuffixFile";
3
+ readonly componentName: string;
4
+ readonly suffix: string;
5
+ } | {
6
+ readonly kind: "suffixComponentNameMismatch";
7
+ readonly componentName: string;
8
+ readonly folderName: string;
9
+ readonly suffix: string;
10
+ } | {
11
+ readonly kind: "flatComponentFile";
12
+ readonly componentName: string;
13
+ };
14
+ /**
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.
26
+ */
27
+ export declare const getSuggestedComponentFolderPath: (filePath: string, componentName: string) => string;
28
+ /**
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.
32
+ */
33
+ export declare const getSuggestedSuffixRolePath: (filePath: string, componentName: string, suffix: string) => string;
34
+ export {};