@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.
- package/CHANGELOG.md +19 -0
- package/README.md +7 -9
- package/package.json +3 -2
- package/src/lib/config/index.d.ts +2 -8
- package/src/lib/config/plugins.d.ts +2 -8
- package/src/lib/config/plugins.js +0 -4
- package/src/lib/config/presets/react.d.ts +2 -8
- package/src/lib/rules/one-component-per-file/one-component-per-file.js +1 -0
- package/src/lib/rules/require-component-directory/require-component-directory.js +26 -197
- package/src/lib/rules/require-component-directory/utils/component-directory-structure-utils/component-directory-structure-utils.d.ts +25 -25
- package/src/lib/rules/require-component-directory/utils/component-directory-structure-utils/component-directory-structure-utils.js +178 -78
- package/src/lib/rules/require-hook-util-directory/require-hook-util-directory.d.ts +5 -3
- package/src/lib/rules/require-hook-util-directory/require-hook-util-directory.js +17 -13
- package/src/lib/rules/require-hook-util-directory/utils/hook-util-directory-structure-utils/hook-util-directory-structure-utils.d.ts +13 -4
- package/src/lib/rules/require-hook-util-directory/utils/hook-util-directory-structure-utils/hook-util-directory-structure-utils.js +31 -15
- package/src/lib/rules/require-nested-components-directory/require-nested-components-directory.d.ts +2 -1
- package/src/lib/rules/require-nested-components-directory/require-nested-components-directory.js +7 -6
- package/src/lib/rules/require-nested-components-directory/utils/nested-component-directory-utils/nested-component-directory-utils.d.ts +13 -9
- package/src/lib/rules/require-nested-components-directory/utils/nested-component-directory-utils/nested-component-directory-utils.js +46 -162
- package/src/lib/utils/component-utils.d.ts +13 -4
- package/src/lib/utils/component-utils.js +170 -64
- package/src/lib/utils/react-module-summary/react-module-summary.d.ts +26 -0
- package/src/lib/utils/react-module-summary/react-module-summary.js +84 -0
- package/src/lib/utils/source-tree-utils/source-tree-utils.d.ts +0 -37
- package/src/lib/utils/source-tree-utils/source-tree-utils.js +6 -109
- package/src/lib/rules/require-nested-components-privacy/require-nested-components-privacy.d.ts +0 -9
- package/src/lib/rules/require-nested-components-privacy/require-nested-components-privacy.js +0 -63
- package/src/lib/rules/require-nested-components-privacy/utils/nested-component-privacy-utils/nested-component-privacy-utils.d.ts +0 -14
- package/src/lib/rules/require-nested-components-privacy/utils/nested-component-privacy-utils/nested-component-privacy-utils.js +0 -62
- package/src/lib/rules/require-page-directory/require-page-directory.d.ts +0 -8
- package/src/lib/rules/require-page-directory/require-page-directory.js +0 -44
- package/src/lib/rules/require-page-directory/utils/page-directory-structure-utils/page-directory-structure-utils.d.ts +0 -18
- package/src/lib/rules/require-page-directory/utils/page-directory-structure-utils/page-directory-structure-utils.js +0 -159
|
@@ -1,114 +1,214 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.formatSuggestedPath = exports.analyzeComponentFolderStructure = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
|
+
const fs = tslib_1.__importStar(require("fs"));
|
|
5
6
|
const path = tslib_1.__importStar(require("path"));
|
|
7
|
+
const react_module_summary_1 = require("../../../../utils/react-module-summary/react-module-summary");
|
|
6
8
|
const source_tree_utils_1 = require("../../../../utils/source-tree-utils/source-tree-utils");
|
|
7
9
|
const COMPONENTS_DIRECTORY_NAME = "components";
|
|
8
10
|
const MODULE_DIRECTORY_NAMES = new Set([COMPONENTS_DIRECTORY_NAME, "pages"]);
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
const SUFFIX_FILE_PATTERN = /^(.+)\.(utils|types|constants|helpers)\.(tsx?|jsx?)$/i;
|
|
12
|
+
const COMPANION_FILE_PATTERN = /^(.+)\.(spec|stories|docs|variants|module)\.(tsx|ts)$/;
|
|
13
|
+
const COMPONENT_FILE_PATTERN = /^(.+)\.(tsx|ts)$/;
|
|
14
|
+
const OWNER_EXTENSIONS = [".tsx", ".ts"];
|
|
15
|
+
const parseSuffix = (value) => {
|
|
16
|
+
const lowerCaseValue = value.toLowerCase();
|
|
17
|
+
if (lowerCaseValue === "utils" ||
|
|
18
|
+
lowerCaseValue === "types" ||
|
|
19
|
+
lowerCaseValue === "constants" ||
|
|
20
|
+
lowerCaseValue === "helpers") {
|
|
21
|
+
return lowerCaseValue;
|
|
22
|
+
}
|
|
23
|
+
return null;
|
|
15
24
|
};
|
|
16
|
-
const
|
|
17
|
-
const
|
|
18
|
-
const
|
|
19
|
-
|
|
25
|
+
const parseSuffixFileName = (fileName) => {
|
|
26
|
+
const match = SUFFIX_FILE_PATTERN.exec(fileName);
|
|
27
|
+
const name = match?.[1];
|
|
28
|
+
const suffix = parseSuffix(match?.[2] ?? "");
|
|
29
|
+
const extension = match?.[3];
|
|
30
|
+
if (name === undefined || suffix === null || extension === undefined) {
|
|
20
31
|
return null;
|
|
21
32
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
33
|
+
return { name, suffix, extension };
|
|
34
|
+
};
|
|
35
|
+
const toCamelCase = (name) => `${name.charAt(0).toLowerCase()}${name.slice(1)}`;
|
|
36
|
+
const toPascalCase = (name) => `${name.charAt(0).toUpperCase()}${name.slice(1)}`;
|
|
37
|
+
/**
|
|
38
|
+
* Where a suffix file goes when it does not belong to a component folder, relative to `scopeDirectory`:
|
|
39
|
+
* `types/<Name>.types.<ext>`, `constants/<name>/<name>.<ext>`, or `utils/<name>/<name>.<ext>`.
|
|
40
|
+
*/
|
|
41
|
+
const getRoleTargetPath = (scopeDirectory, suffixFileName) => {
|
|
42
|
+
const { name, suffix, extension } = suffixFileName;
|
|
43
|
+
if (suffix === "types") {
|
|
44
|
+
return path.join(scopeDirectory, "types", `${toPascalCase(name)}.types.${extension}`);
|
|
26
45
|
}
|
|
27
|
-
|
|
46
|
+
const roleDirectory = suffix === "constants" ? "constants" : "utils";
|
|
47
|
+
const moduleName = toCamelCase(name);
|
|
48
|
+
return path.join(scopeDirectory, roleDirectory, moduleName, `${moduleName}.${extension}`);
|
|
28
49
|
};
|
|
29
|
-
/**
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
50
|
+
/**
|
|
51
|
+
* `<ownerBaseName>.tsx` or `.ts` in `directory`, matched ignoring case so every filesystem gives the
|
|
52
|
+
* same answer. An exact-case match wins.
|
|
53
|
+
*/
|
|
54
|
+
const findOwnerPath = (directory, ownerBaseName) => {
|
|
55
|
+
let entries;
|
|
56
|
+
try {
|
|
57
|
+
entries = fs.readdirSync(directory);
|
|
36
58
|
}
|
|
37
|
-
|
|
59
|
+
catch {
|
|
60
|
+
return undefined;
|
|
61
|
+
}
|
|
62
|
+
const candidates = OWNER_EXTENSIONS.map(extension => `${ownerBaseName}${extension}`);
|
|
63
|
+
const lowerCaseCandidates = candidates.map(candidate => candidate.toLowerCase());
|
|
64
|
+
const ownerFileName = candidates.find(candidate => entries.includes(candidate)) ??
|
|
65
|
+
lowerCaseCandidates
|
|
66
|
+
.map(candidate => entries.find(entry => entry.toLowerCase() === candidate))
|
|
67
|
+
.find(entry => entry !== undefined);
|
|
68
|
+
return ownerFileName === undefined ? undefined : path.join(directory, ownerFileName);
|
|
38
69
|
};
|
|
39
70
|
/**
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
* or `null` when the path is allowed.
|
|
71
|
+
* The PascalCase `<Name>/` folder in `directory` whose `<Name>.tsx` or `.ts` exports a component,
|
|
72
|
+
* matched ignoring case like `findOwnerPath`. An exact-case match wins. Lowercase folders are
|
|
73
|
+
* grouping folders, not component folders.
|
|
44
74
|
*/
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
75
|
+
const findComponentFolderPath = (directory, name) => {
|
|
76
|
+
let folderNames;
|
|
77
|
+
try {
|
|
78
|
+
folderNames = fs
|
|
79
|
+
.readdirSync(directory, { withFileTypes: true })
|
|
80
|
+
.filter(entry => entry.isDirectory())
|
|
81
|
+
.map(entry => entry.name);
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
return undefined;
|
|
85
|
+
}
|
|
86
|
+
const folderName = folderNames.find(candidate => candidate === name) ??
|
|
87
|
+
folderNames.find(candidate => candidate.toLowerCase() === name.toLowerCase());
|
|
88
|
+
if (folderName === undefined || !source_tree_utils_1.PASCAL_CASE_PATTERN.test(folderName)) {
|
|
89
|
+
return undefined;
|
|
90
|
+
}
|
|
91
|
+
const folderPath = path.join(directory, folderName);
|
|
92
|
+
const ownerPath = findOwnerPath(folderPath, folderName);
|
|
93
|
+
return ownerPath !== undefined && (0, react_module_summary_1.summarizeReactModuleFile)(ownerPath).exportsComponent ? folderPath : undefined;
|
|
94
|
+
};
|
|
95
|
+
/** `Button.tsx` → `Button`; a lowercase `button.tsx` takes the name of its exported component. */
|
|
96
|
+
const getComponentFolderName = (baseName, summary) => {
|
|
97
|
+
if (source_tree_utils_1.PASCAL_CASE_PATTERN.test(baseName)) {
|
|
98
|
+
return baseName;
|
|
99
|
+
}
|
|
100
|
+
return summary.exportedComponentNames[0] ?? toPascalCase(baseName);
|
|
101
|
+
};
|
|
102
|
+
const getModuleDirectory = (directory) => {
|
|
103
|
+
const name = path.basename(directory);
|
|
104
|
+
return MODULE_DIRECTORY_NAMES.has(name) ? { directory, name } : null;
|
|
105
|
+
};
|
|
106
|
+
const analyzeFlatSuffixFile = (normalizedPath, moduleDirectory, suffixFileName) => {
|
|
49
107
|
const fileName = path.basename(normalizedPath);
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
108
|
+
const ownerPath = findOwnerPath(moduleDirectory.directory, suffixFileName.name);
|
|
109
|
+
const ownerSummary = ownerPath === undefined ? null : (0, react_module_summary_1.summarizeReactModuleFile)(ownerPath);
|
|
110
|
+
if (ownerPath !== undefined && ownerSummary?.exportsComponent === true) {
|
|
111
|
+
// A flat page is not checked, so its suffix file stays beside it instead of moving away from it.
|
|
112
|
+
if (moduleDirectory.name !== COMPONENTS_DIRECTORY_NAME) {
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
const folderName = getComponentFolderName((0, source_tree_utils_1.getBaseName)(path.basename(ownerPath)), ownerSummary);
|
|
116
|
+
return {
|
|
117
|
+
kind: "flatComponentSuffixFile",
|
|
118
|
+
moduleDirectoryName: moduleDirectory.name,
|
|
119
|
+
targetPath: path.join(moduleDirectory.directory, folderName, fileName),
|
|
120
|
+
};
|
|
53
121
|
}
|
|
54
|
-
const
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
122
|
+
const componentFolderPath = ownerPath === undefined ? findComponentFolderPath(moduleDirectory.directory, suffixFileName.name) : undefined;
|
|
123
|
+
return {
|
|
124
|
+
kind: "flatComponentSuffixFile",
|
|
125
|
+
moduleDirectoryName: moduleDirectory.name,
|
|
126
|
+
targetPath: componentFolderPath === undefined
|
|
127
|
+
? getRoleTargetPath(path.dirname(moduleDirectory.directory), suffixFileName)
|
|
128
|
+
: path.join(componentFolderPath, fileName),
|
|
129
|
+
};
|
|
130
|
+
};
|
|
131
|
+
const analyzeFlatComponentFile = (normalizedPath, readOwnSummary) => {
|
|
132
|
+
const directory = path.dirname(normalizedPath);
|
|
133
|
+
const fileName = path.basename(normalizedPath);
|
|
134
|
+
const companionOwnerBaseName = COMPANION_FILE_PATTERN.exec(fileName)?.[1];
|
|
135
|
+
if (companionOwnerBaseName !== undefined) {
|
|
136
|
+
const ownerPath = findOwnerPath(directory, companionOwnerBaseName);
|
|
137
|
+
if (ownerPath === undefined) {
|
|
138
|
+
const componentFolderPath = findComponentFolderPath(directory, companionOwnerBaseName);
|
|
139
|
+
return componentFolderPath === undefined
|
|
140
|
+
? null
|
|
141
|
+
: { kind: "flatComponentFile", targetPath: path.join(componentFolderPath, fileName) };
|
|
63
142
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
componentName: suffixFileName.componentName,
|
|
68
|
-
folderName: enclosingModule.folderName,
|
|
69
|
-
suffix: suffixFileName.suffix,
|
|
70
|
-
};
|
|
143
|
+
const ownerSummary = (0, react_module_summary_1.summarizeReactModuleFile)(ownerPath);
|
|
144
|
+
if (!ownerSummary.exportsComponent) {
|
|
145
|
+
return null;
|
|
71
146
|
}
|
|
147
|
+
const ownerFolderName = getComponentFolderName((0, source_tree_utils_1.getBaseName)(path.basename(ownerPath)), ownerSummary);
|
|
148
|
+
return { kind: "flatComponentFile", targetPath: path.join(directory, ownerFolderName, fileName) };
|
|
149
|
+
}
|
|
150
|
+
const baseName = COMPONENT_FILE_PATTERN.exec(fileName)?.[1];
|
|
151
|
+
if (baseName === undefined) {
|
|
72
152
|
return null;
|
|
73
153
|
}
|
|
74
|
-
|
|
154
|
+
const summary = readOwnSummary();
|
|
155
|
+
if (!summary.exportsComponent) {
|
|
75
156
|
return null;
|
|
76
157
|
}
|
|
77
|
-
|
|
158
|
+
const folderName = getComponentFolderName(baseName, summary);
|
|
159
|
+
const targetFileName = source_tree_utils_1.PASCAL_CASE_PATTERN.test(baseName) ? fileName : `${folderName}${path.extname(fileName)}`;
|
|
160
|
+
return { kind: "flatComponentFile", targetPath: path.join(directory, folderName, targetFileName) };
|
|
161
|
+
};
|
|
162
|
+
/**
|
|
163
|
+
* Returns a folder-structure violation, or `null` when the path is allowed:
|
|
164
|
+
*
|
|
165
|
+
* - a file directly in `components/` that exports a component (by `one-component-per-file`'s
|
|
166
|
+
* detection), or the spec/story/docs of one, whatever the file name's case
|
|
167
|
+
* - a `*.utils|types|constants|helpers` file directly in `components/` or `pages/`, whatever its case,
|
|
168
|
+
* except one in `pages/` beside the flat page it belongs to
|
|
169
|
+
* - a suffix file inside `components/<Folder>/` or `pages/<Folder>/` whose name is not `<Folder>`,
|
|
170
|
+
* ignoring case
|
|
171
|
+
*
|
|
172
|
+
* `readOwnSummary` summarizes the file itself. A rule passes its parsed program; a codemod reads the file.
|
|
173
|
+
*/
|
|
174
|
+
const analyzeComponentFolderStructure = (filePath, readOwnSummary = () => (0, react_module_summary_1.summarizeReactModuleFile)(filePath)) => {
|
|
175
|
+
const normalizedPath = path.normalize(filePath);
|
|
176
|
+
const parentDirectory = path.dirname(normalizedPath);
|
|
177
|
+
const fileName = path.basename(normalizedPath);
|
|
178
|
+
if ((0, source_tree_utils_1.getBaseName)(fileName) === "index") {
|
|
78
179
|
return null;
|
|
79
180
|
}
|
|
80
|
-
const
|
|
81
|
-
const
|
|
82
|
-
if (
|
|
181
|
+
const suffixFileName = parseSuffixFileName(fileName);
|
|
182
|
+
const moduleDirectory = getModuleDirectory(parentDirectory);
|
|
183
|
+
if (suffixFileName !== null && moduleDirectory !== null) {
|
|
184
|
+
return analyzeFlatSuffixFile(normalizedPath, moduleDirectory, suffixFileName);
|
|
185
|
+
}
|
|
186
|
+
const enclosingModuleDirectory = getModuleDirectory(path.dirname(parentDirectory));
|
|
187
|
+
const folderName = path.basename(parentDirectory);
|
|
188
|
+
if (suffixFileName !== null && enclosingModuleDirectory !== null && source_tree_utils_1.PASCAL_CASE_PATTERN.test(folderName)) {
|
|
189
|
+
if (suffixFileName.name.toLowerCase() === folderName.toLowerCase()) {
|
|
190
|
+
return null;
|
|
191
|
+
}
|
|
83
192
|
return {
|
|
84
|
-
kind: "
|
|
85
|
-
|
|
193
|
+
kind: "suffixComponentNameMismatch",
|
|
194
|
+
folderName,
|
|
195
|
+
targetPath: getRoleTargetPath(parentDirectory, suffixFileName),
|
|
86
196
|
};
|
|
87
197
|
}
|
|
88
|
-
|
|
198
|
+
if (suffixFileName !== null || moduleDirectory?.name !== COMPONENTS_DIRECTORY_NAME) {
|
|
199
|
+
return null;
|
|
200
|
+
}
|
|
201
|
+
return analyzeFlatComponentFile(normalizedPath, readOwnSummary);
|
|
89
202
|
};
|
|
90
203
|
exports.analyzeComponentFolderStructure = analyzeComponentFolderStructure;
|
|
91
204
|
/**
|
|
92
|
-
* `
|
|
93
|
-
* `components/`
|
|
94
|
-
* either directly under `components/` or one level below it, so the target is
|
|
95
|
-
* the same in both cases.
|
|
96
|
-
*/
|
|
97
|
-
const getSuggestedComponentFolderPath = (filePath, componentName) => path.posix.join(COMPONENTS_DIRECTORY_NAME, componentName, path.basename(path.normalize(filePath)));
|
|
98
|
-
exports.getSuggestedComponentFolderPath = getSuggestedComponentFolderPath;
|
|
99
|
-
/**
|
|
100
|
-
* A suffix file inside `components/<Parent>/` that does not match `<Parent>`
|
|
101
|
-
* stays with that parent. Suggest `components/<Parent>/<role>/<name>/<name>.ts`
|
|
102
|
-
* rather than a new component folder named after the suffix prefix.
|
|
205
|
+
* `targetPath` relative to the library's `src/`, so `features/Auth/components/LoginForm/LoginForm.tsx`
|
|
206
|
+
* is not confused with another `components/` folder. Falls back to the file's grandparent outside `src/`.
|
|
103
207
|
*/
|
|
104
|
-
const
|
|
208
|
+
const formatSuggestedPath = (filePath, targetPath) => {
|
|
105
209
|
const normalizedPath = path.normalize(filePath);
|
|
106
|
-
const
|
|
107
|
-
|
|
108
|
-
const folderName = enclosingModule?.folderName ?? path.basename(path.dirname(normalizedPath));
|
|
109
|
-
const roleDirectory = ROLE_DIRECTORY_BY_SUFFIX[suffix] ?? suffix;
|
|
110
|
-
const roleName = `${componentName.charAt(0).toLowerCase()}${componentName.slice(1)}`;
|
|
111
|
-
return path.posix.join(moduleDirectoryName, folderName, roleDirectory, roleName, `${roleName}.ts`);
|
|
210
|
+
const root = (0, source_tree_utils_1.getSrcRoot)(normalizedPath) ?? path.dirname(path.dirname(normalizedPath));
|
|
211
|
+
return path.relative(root, targetPath).split(path.sep).join("/");
|
|
112
212
|
};
|
|
113
|
-
exports.
|
|
213
|
+
exports.formatSuggestedPath = formatSuggestedPath;
|
|
114
214
|
//# sourceMappingURL=component-directory-structure-utils.js.map
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* ESLint rule: require-hook-util-directory
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
4
|
+
* No source files directly inside a `hooks/` or `utils/` directory; each goes in a subfolder
|
|
5
|
+
* (e.g. `hooks/useFoo/useFoo.ts`) so its spec can colocate. Subfolder names are not checked.
|
|
6
6
|
*/
|
|
7
|
-
|
|
7
|
+
type MessageIds = "flatHookOrUtilFile" | "flatHookOrUtilFileWithSpec";
|
|
8
|
+
export declare const requireHookUtilDirectory: import("@typescript-eslint/utils/ts-eslint").RuleModule<MessageIds, [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
|
|
8
9
|
name: string;
|
|
9
10
|
};
|
|
11
|
+
export {};
|
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
/**
|
|
3
3
|
* ESLint rule: require-hook-util-directory
|
|
4
4
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
5
|
+
* No source files directly inside a `hooks/` or `utils/` directory; each goes in a subfolder
|
|
6
|
+
* (e.g. `hooks/useFoo/useFoo.ts`) so its spec can colocate. Subfolder names are not checked.
|
|
7
7
|
*/
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
9
|
exports.requireHookUtilDirectory = void 0;
|
|
@@ -16,11 +16,12 @@ exports.requireHookUtilDirectory = (0, create_rule_1.createRule)({
|
|
|
16
16
|
meta: {
|
|
17
17
|
type: "suggestion",
|
|
18
18
|
docs: {
|
|
19
|
-
description: "
|
|
19
|
+
description: "Disallow source files directly inside a `hooks/` or `utils/` directory; each goes in a subfolder. Subfolder names are not checked.",
|
|
20
20
|
},
|
|
21
21
|
schema: [],
|
|
22
22
|
messages: {
|
|
23
|
-
flatHookOrUtilFile: "
|
|
23
|
+
flatHookOrUtilFile: "No source files directly inside `{{roleFolder}}/`. Move `{{file}}` to `{{suggestedPath}}`.",
|
|
24
|
+
flatHookOrUtilFileWithSpec: "No source files directly inside `{{roleFolder}}/`. Move `{{file}}` to `{{suggestedPath}}` and colocate `{{specFile}}` there.",
|
|
24
25
|
},
|
|
25
26
|
},
|
|
26
27
|
defaultOptions: [],
|
|
@@ -31,17 +32,20 @@ exports.requireHookUtilDirectory = (0, create_rule_1.createRule)({
|
|
|
31
32
|
if (violation === null) {
|
|
32
33
|
return;
|
|
33
34
|
}
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
|
|
35
|
+
const data = {
|
|
36
|
+
file: path.basename(context.filename),
|
|
37
|
+
roleFolder: violation.roleFolder,
|
|
38
|
+
suggestedPath: (0, hook_util_directory_structure_utils_1.getSuggestedHookUtilFolderPath)(violation),
|
|
39
|
+
};
|
|
40
|
+
const specFile = (0, hook_util_directory_structure_utils_1.findSiblingSpecFile)(context.filename, violation.baseName);
|
|
41
|
+
if (specFile === undefined) {
|
|
42
|
+
context.report({ node, messageId: "flatHookOrUtilFile", data });
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
37
45
|
context.report({
|
|
38
46
|
node,
|
|
39
|
-
messageId: "
|
|
40
|
-
data: {
|
|
41
|
-
file: fileName,
|
|
42
|
-
suggestedPath,
|
|
43
|
-
specFile: `${violation.baseName}.spec${specExtension}`,
|
|
44
|
-
},
|
|
47
|
+
messageId: "flatHookOrUtilFileWithSpec",
|
|
48
|
+
data: { ...data, specFile: path.basename(specFile) },
|
|
45
49
|
});
|
|
46
50
|
},
|
|
47
51
|
};
|
|
@@ -2,14 +2,23 @@ export type HookUtilFolderViolation = {
|
|
|
2
2
|
readonly kind: "flatHookOrUtilFile";
|
|
3
3
|
readonly roleFolder: "hooks" | "utils";
|
|
4
4
|
readonly baseName: string;
|
|
5
|
+
/** Absolute path of `<role>/<folder>/<file>`. */
|
|
6
|
+
readonly targetPath: string;
|
|
5
7
|
};
|
|
8
|
+
/** Folder for a flat role file: its base name without a trailing `.utils|.helpers|.constants|.types`. */
|
|
9
|
+
export declare const getHookUtilFolderName: (baseName: string) => string;
|
|
6
10
|
/**
|
|
7
|
-
* Returns a violation when a
|
|
8
|
-
*
|
|
11
|
+
* Returns a violation when a source file sits directly in a `hooks/` or `utils/` directory
|
|
12
|
+
* instead of a subfolder. Subfolder names are not checked.
|
|
13
|
+
*
|
|
14
|
+
* In `hooks/`, only files named like a hook (`useFoo`) are checked, so mock helpers and test
|
|
15
|
+
* utilities can stay. `*.types.*` files, `index` files, specs, stories, and `__mocks__/` are skipped.
|
|
9
16
|
*/
|
|
10
17
|
export declare const analyzeHookUtilFolderStructure: (filePath: string) => HookUtilFolderViolation | null;
|
|
11
18
|
/**
|
|
12
19
|
* Suggested path relative to the parent of `hooks/` or `utils/`.
|
|
13
|
-
* `hooks/useFoo.ts` → `hooks/useFoo/useFoo.ts`.
|
|
20
|
+
* `hooks/useFoo.ts` → `hooks/useFoo/useFoo.ts`; `utils/Portal.utils.tsx` → `utils/Portal/Portal.utils.tsx`.
|
|
14
21
|
*/
|
|
15
|
-
export declare const getSuggestedHookUtilFolderPath: (
|
|
22
|
+
export declare const getSuggestedHookUtilFolderPath: (violation: HookUtilFolderViolation) => string;
|
|
23
|
+
/** The sibling `<baseName>.spec.ts` or `.spec.tsx`, if one exists. */
|
|
24
|
+
export declare const findSiblingSpecFile: (filePath: string, baseName: string) => string | undefined;
|
|
@@ -1,44 +1,60 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getSuggestedHookUtilFolderPath = exports.analyzeHookUtilFolderStructure = void 0;
|
|
3
|
+
exports.findSiblingSpecFile = exports.getSuggestedHookUtilFolderPath = exports.analyzeHookUtilFolderStructure = exports.getHookUtilFolderName = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
|
+
const fs = tslib_1.__importStar(require("fs"));
|
|
5
6
|
const path = tslib_1.__importStar(require("path"));
|
|
6
7
|
const source_tree_utils_1 = require("../../../../utils/source-tree-utils/source-tree-utils");
|
|
8
|
+
/** React's hook naming convention: `use` followed by an uppercase letter or digit. */
|
|
9
|
+
const HOOK_NAME_PATTERN = /^use[A-Z0-9]/;
|
|
10
|
+
const TYPES_FILE_PATTERN = /\.types$/;
|
|
11
|
+
const ROLE_FILE_SUFFIX_PATTERN = /\.(utils|helpers|constants|types)$/;
|
|
7
12
|
const isUnderMocksDirectory = (normalizedPath) => normalizedPath.split(path.sep).includes("__mocks__");
|
|
13
|
+
/** Folder for a flat role file: its base name without a trailing `.utils|.helpers|.constants|.types`. */
|
|
14
|
+
const getHookUtilFolderName = (baseName) => baseName.replace(ROLE_FILE_SUFFIX_PATTERN, "");
|
|
15
|
+
exports.getHookUtilFolderName = getHookUtilFolderName;
|
|
8
16
|
/**
|
|
9
|
-
* Returns a violation when a
|
|
10
|
-
*
|
|
17
|
+
* Returns a violation when a source file sits directly in a `hooks/` or `utils/` directory
|
|
18
|
+
* instead of a subfolder. Subfolder names are not checked.
|
|
19
|
+
*
|
|
20
|
+
* In `hooks/`, only files named like a hook (`useFoo`) are checked, so mock helpers and test
|
|
21
|
+
* utilities can stay. `*.types.*` files, `index` files, specs, stories, and `__mocks__/` are skipped.
|
|
11
22
|
*/
|
|
12
23
|
const analyzeHookUtilFolderStructure = (filePath) => {
|
|
13
24
|
const normalizedPath = path.normalize(filePath);
|
|
14
25
|
if (!(0, source_tree_utils_1.isSourceFile)(normalizedPath) || isUnderMocksDirectory(normalizedPath)) {
|
|
15
26
|
return null;
|
|
16
27
|
}
|
|
17
|
-
const
|
|
18
|
-
const
|
|
28
|
+
const fileName = path.basename(normalizedPath);
|
|
29
|
+
const baseName = (0, source_tree_utils_1.getBaseName)(fileName);
|
|
30
|
+
const parentDirectory = path.dirname(normalizedPath);
|
|
31
|
+
const parentDirectoryName = path.basename(parentDirectory);
|
|
19
32
|
if (parentDirectoryName !== "hooks" && parentDirectoryName !== "utils") {
|
|
20
33
|
return null;
|
|
21
34
|
}
|
|
22
|
-
if (baseName === "index") {
|
|
35
|
+
if (baseName === "index" || TYPES_FILE_PATTERN.test(baseName)) {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
if (parentDirectoryName === "hooks" && !HOOK_NAME_PATTERN.test(baseName)) {
|
|
23
39
|
return null;
|
|
24
40
|
}
|
|
25
41
|
return {
|
|
26
42
|
kind: "flatHookOrUtilFile",
|
|
27
|
-
roleFolder: parentDirectoryName
|
|
43
|
+
roleFolder: parentDirectoryName,
|
|
28
44
|
baseName,
|
|
45
|
+
targetPath: path.join(parentDirectory, (0, exports.getHookUtilFolderName)(baseName), fileName),
|
|
29
46
|
};
|
|
30
47
|
};
|
|
31
48
|
exports.analyzeHookUtilFolderStructure = analyzeHookUtilFolderStructure;
|
|
32
49
|
/**
|
|
33
50
|
* Suggested path relative to the parent of `hooks/` or `utils/`.
|
|
34
|
-
* `hooks/useFoo.ts` → `hooks/useFoo/useFoo.ts`.
|
|
51
|
+
* `hooks/useFoo.ts` → `hooks/useFoo/useFoo.ts`; `utils/Portal.utils.tsx` → `utils/Portal/Portal.utils.tsx`.
|
|
35
52
|
*/
|
|
36
|
-
const getSuggestedHookUtilFolderPath = (
|
|
37
|
-
const normalizedPath = path.normalize(filePath);
|
|
38
|
-
const fileName = path.basename(normalizedPath);
|
|
39
|
-
const baseName = (0, source_tree_utils_1.getBaseName)(fileName);
|
|
40
|
-
const parentDirectoryName = path.basename(path.dirname(normalizedPath));
|
|
41
|
-
return path.posix.join(parentDirectoryName, baseName, fileName);
|
|
42
|
-
};
|
|
53
|
+
const getSuggestedHookUtilFolderPath = (violation) => path.relative(path.dirname(path.dirname(path.dirname(violation.targetPath))), violation.targetPath).split(path.sep).join("/");
|
|
43
54
|
exports.getSuggestedHookUtilFolderPath = getSuggestedHookUtilFolderPath;
|
|
55
|
+
/** The sibling `<baseName>.spec.ts` or `.spec.tsx`, if one exists. */
|
|
56
|
+
const findSiblingSpecFile = (filePath, baseName) => [".spec.ts", ".spec.tsx"]
|
|
57
|
+
.map(extension => path.join(path.dirname(filePath), `${baseName}${extension}`))
|
|
58
|
+
.find(candidate => fs.existsSync(candidate));
|
|
59
|
+
exports.findSiblingSpecFile = findSiblingSpecFile;
|
|
44
60
|
//# sourceMappingURL=hook-util-directory-structure-utils.js.map
|
package/src/lib/rules/require-nested-components-directory/require-nested-components-directory.d.ts
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
* ESLint rule: require-nested-components-directory
|
|
3
3
|
*
|
|
4
4
|
* When a component module owns a child component, that child lives under
|
|
5
|
-
* `<Parent>/components/`.
|
|
5
|
+
* `<Parent>/components/`. Siblings that export no component, context modules
|
|
6
|
+
* (a top-level `createContext` call), and compound parts (`<Parent><Word>`) are not children.
|
|
6
7
|
*/
|
|
7
8
|
export declare const requireNestedComponentsDirectory: import("@typescript-eslint/utils/ts-eslint").RuleModule<"nestedComponentsNotInSubfolder", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
|
|
8
9
|
name: string;
|
package/src/lib/rules/require-nested-components-directory/require-nested-components-directory.js
CHANGED
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
* ESLint rule: require-nested-components-directory
|
|
4
4
|
*
|
|
5
5
|
* When a component module owns a child component, that child lives under
|
|
6
|
-
* `<Parent>/components/`.
|
|
6
|
+
* `<Parent>/components/`. Siblings that export no component, context modules
|
|
7
|
+
* (a top-level `createContext` call), and compound parts (`<Parent><Word>`) are not children.
|
|
7
8
|
*/
|
|
8
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
10
|
exports.requireNestedComponentsDirectory = void 0;
|
|
@@ -18,7 +19,7 @@ exports.requireNestedComponentsDirectory = (0, create_rule_1.createRule)({
|
|
|
18
19
|
},
|
|
19
20
|
schema: [],
|
|
20
21
|
messages: {
|
|
21
|
-
nestedComponentsNotInSubfolder: "Move child components
|
|
22
|
+
nestedComponentsNotInSubfolder: "Move child components of `{{parentComponentName}}` into `components/`: move {{moves}}.",
|
|
22
23
|
},
|
|
23
24
|
},
|
|
24
25
|
defaultOptions: [],
|
|
@@ -29,13 +30,13 @@ exports.requireNestedComponentsDirectory = (0, create_rule_1.createRule)({
|
|
|
29
30
|
if (violation === null) {
|
|
30
31
|
return;
|
|
31
32
|
}
|
|
33
|
+
const moves = violation.childComponents
|
|
34
|
+
.map(child => `\`${child.name}\` to \`${(0, nested_component_directory_utils_1.getSuggestedNestedComponentsPath)(violation.parentComponentName, child)}\``)
|
|
35
|
+
.join(", ");
|
|
32
36
|
context.report({
|
|
33
37
|
node,
|
|
34
38
|
messageId: "nestedComponentsNotInSubfolder",
|
|
35
|
-
data: {
|
|
36
|
-
parentComponentName: violation.parentComponentName,
|
|
37
|
-
childComponentNames: violation.childComponentNames.join(", "),
|
|
38
|
-
},
|
|
39
|
+
data: { parentComponentName: violation.parentComponentName, moves },
|
|
39
40
|
});
|
|
40
41
|
},
|
|
41
42
|
};
|
|
@@ -1,17 +1,21 @@
|
|
|
1
|
+
export type ChildComponent = {
|
|
2
|
+
readonly name: string;
|
|
3
|
+
/** `file` is a sibling `<Child>.tsx`; `folder` is a sibling `<Child>/` with `<Child>/<Child>.tsx`. */
|
|
4
|
+
readonly kind: "file" | "folder";
|
|
5
|
+
};
|
|
1
6
|
type NestedComponentsFolderViolation = {
|
|
2
7
|
readonly kind: "nestedComponentsNotInSubfolder";
|
|
3
8
|
readonly parentComponentName: string;
|
|
4
|
-
readonly
|
|
9
|
+
readonly childComponents: ReadonlyArray<ChildComponent>;
|
|
5
10
|
};
|
|
6
|
-
export declare const isParentComponentModule: (parentDirectory: string) => boolean;
|
|
7
11
|
/**
|
|
8
|
-
* Returns a violation when `filePath`
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* Route components are not children.
|
|
12
|
+
* Returns a violation when `filePath` is `<Parent>/<Parent>.tsx` and a sibling file or folder is a
|
|
13
|
+
* child component kept beside the parent instead of under `<Parent>/components/`.
|
|
14
|
+
*
|
|
15
|
+
* A sibling is a child when it exports a component (by `one-component-per-file`'s detection) and
|
|
16
|
+
* does not call React's `createContext` at top level. Compound parts stay beside the parent.
|
|
14
17
|
*/
|
|
15
18
|
export declare const analyzeNestedComponentsFolderStructure: (filePath: string) => NestedComponentsFolderViolation | null;
|
|
16
|
-
|
|
19
|
+
/** `Map` + file `Legend` → `Map/components/Legend/Legend.tsx`; folder `Legend` → `Map/components/Legend/`. */
|
|
20
|
+
export declare const getSuggestedNestedComponentsPath: (parentComponentName: string, child: ChildComponent) => string;
|
|
17
21
|
export {};
|