@trackunit/eslint-plugin-trackunit 0.7.13 → 0.7.14
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 +28 -0
- package/README.md +12 -0
- package/package.json +2 -2
- package/src/index.d.ts +1 -1
- package/src/index.js +2 -1
- package/src/lib/config/index.d.ts +20 -0
- package/src/lib/config/plugins.d.ts +23 -0
- package/src/lib/config/plugins.js +19 -1
- package/src/lib/config/presets/react.d.ts +20 -0
- package/src/lib/config/presets/react.js +4 -0
- package/src/lib/rules/no-unused-translation-key/used-strings.js +1 -1
- package/src/lib/{utils → rules/no-unused-translation-key/utils/program-utils}/program-utils.js +1 -1
- package/src/lib/rules/require-component-directory/require-component-directory.d.ts +16 -0
- package/src/lib/rules/require-component-directory/require-component-directory.js +240 -0
- package/src/lib/rules/require-component-directory/utils/component-directory-structure-utils/component-directory-structure-utils.d.ts +34 -0
- package/src/lib/rules/require-component-directory/utils/component-directory-structure-utils/component-directory-structure-utils.js +114 -0
- package/src/lib/rules/require-hook-util-directory/require-hook-util-directory.d.ts +9 -0
- package/src/lib/rules/require-hook-util-directory/require-hook-util-directory.js +50 -0
- package/src/lib/rules/require-hook-util-directory/utils/hook-util-directory-structure-utils/hook-util-directory-structure-utils.d.ts +15 -0
- package/src/lib/rules/require-hook-util-directory/utils/hook-util-directory-structure-utils/hook-util-directory-structure-utils.js +44 -0
- package/src/lib/rules/require-nested-components-directory/require-nested-components-directory.d.ts +9 -0
- package/src/lib/rules/require-nested-components-directory/require-nested-components-directory.js +44 -0
- package/src/lib/rules/require-nested-components-directory/utils/nested-component-directory-utils/nested-component-directory-utils.d.ts +17 -0
- package/src/lib/rules/require-nested-components-directory/utils/nested-component-directory-utils/nested-component-directory-utils.js +206 -0
- package/src/lib/rules/require-nested-components-privacy/require-nested-components-privacy.d.ts +9 -0
- package/src/lib/rules/require-nested-components-privacy/require-nested-components-privacy.js +63 -0
- package/src/lib/rules/require-nested-components-privacy/utils/nested-component-privacy-utils/nested-component-privacy-utils.d.ts +14 -0
- package/src/lib/rules/require-nested-components-privacy/utils/nested-component-privacy-utils/nested-component-privacy-utils.js +62 -0
- package/src/lib/rules/require-page-directory/require-page-directory.d.ts +8 -0
- package/src/lib/rules/require-page-directory/require-page-directory.js +44 -0
- package/src/lib/rules/require-page-directory/utils/page-directory-structure-utils/page-directory-structure-utils.d.ts +18 -0
- package/src/lib/rules/require-page-directory/utils/page-directory-structure-utils/page-directory-structure-utils.js +159 -0
- package/src/lib/utils/create-rule/create-rule.d.ts +7 -0
- package/src/lib/utils/create-rule/create-rule.js +9 -0
- package/src/lib/utils/source-tree-utils/source-tree-utils.d.ts +43 -0
- package/src/lib/utils/source-tree-utils/source-tree-utils.js +136 -0
- /package/src/lib/{utils → rules/no-unused-translation-key/utils/program-utils}/program-utils.d.ts +0 -0
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getSuggestedSuffixRolePath = exports.getSuggestedComponentFolderPath = exports.analyzeComponentFolderStructure = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const path = tslib_1.__importStar(require("path"));
|
|
6
|
+
const source_tree_utils_1 = require("../../../../utils/source-tree-utils/source-tree-utils");
|
|
7
|
+
const COMPONENTS_DIRECTORY_NAME = "components";
|
|
8
|
+
const MODULE_DIRECTORY_NAMES = new Set([COMPONENTS_DIRECTORY_NAME, "pages"]);
|
|
9
|
+
const FLAT_COMPONENT_SUFFIX_PATTERN = /\.(utils|types|constants|helpers)$/i;
|
|
10
|
+
const ROLE_DIRECTORY_BY_SUFFIX = {
|
|
11
|
+
utils: "utils",
|
|
12
|
+
helpers: "utils",
|
|
13
|
+
constants: "constants",
|
|
14
|
+
types: "types",
|
|
15
|
+
};
|
|
16
|
+
const PASCAL_CASE_FILE_PATTERN = /^([A-Z][a-zA-Z0-9]*)(?:\.(spec|stories|docs|variants|module))?\.(tsx|ts)$/;
|
|
17
|
+
const parseSuffixFileName = (baseName) => {
|
|
18
|
+
const suffixMatch = FLAT_COMPONENT_SUFFIX_PATTERN.exec(baseName);
|
|
19
|
+
if (suffixMatch === null) {
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
const componentName = baseName.slice(0, -suffixMatch[0].length);
|
|
23
|
+
const suffix = suffixMatch[1];
|
|
24
|
+
if (suffix === undefined || !source_tree_utils_1.PASCAL_CASE_PATTERN.test(componentName)) {
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
return { componentName, suffix };
|
|
28
|
+
};
|
|
29
|
+
/** `components/<PascalCase>/<file>` or `pages/<PascalCase>/<file>`. */
|
|
30
|
+
const getEnclosingModule = (normalizedPath) => {
|
|
31
|
+
const parentDirectory = path.dirname(normalizedPath);
|
|
32
|
+
const folderName = path.basename(parentDirectory);
|
|
33
|
+
const moduleDirectoryName = path.basename(path.dirname(parentDirectory));
|
|
34
|
+
if (!MODULE_DIRECTORY_NAMES.has(moduleDirectoryName) || !source_tree_utils_1.PASCAL_CASE_PATTERN.test(folderName)) {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
return { folderName, moduleDirectoryName };
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* Returns a folder-structure violation when `filePath` is a direct child of a
|
|
41
|
+
* `components/` directory (including scoped `components/` folders), or
|
|
42
|
+
* when a suffix file inside a component folder does not match that folder name,
|
|
43
|
+
* or `null` when the path is allowed.
|
|
44
|
+
*/
|
|
45
|
+
const analyzeComponentFolderStructure = (filePath) => {
|
|
46
|
+
const normalizedPath = path.normalize(filePath);
|
|
47
|
+
const parentDirectory = path.dirname(normalizedPath);
|
|
48
|
+
const parentDirectoryName = path.basename(parentDirectory);
|
|
49
|
+
const fileName = path.basename(normalizedPath);
|
|
50
|
+
const baseName = (0, source_tree_utils_1.getBaseName)(fileName);
|
|
51
|
+
if (baseName === "index") {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
const suffixFileName = parseSuffixFileName(baseName);
|
|
55
|
+
const enclosingModule = getEnclosingModule(normalizedPath);
|
|
56
|
+
if (suffixFileName !== null) {
|
|
57
|
+
if (parentDirectoryName === COMPONENTS_DIRECTORY_NAME) {
|
|
58
|
+
return {
|
|
59
|
+
kind: "flatComponentSuffixFile",
|
|
60
|
+
componentName: suffixFileName.componentName,
|
|
61
|
+
suffix: suffixFileName.suffix,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
if (enclosingModule !== null && suffixFileName.componentName !== enclosingModule.folderName) {
|
|
65
|
+
return {
|
|
66
|
+
kind: "suffixComponentNameMismatch",
|
|
67
|
+
componentName: suffixFileName.componentName,
|
|
68
|
+
folderName: enclosingModule.folderName,
|
|
69
|
+
suffix: suffixFileName.suffix,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
if (enclosingModule !== null) {
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
if (parentDirectoryName !== COMPONENTS_DIRECTORY_NAME) {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
const pascalCaseMatch = PASCAL_CASE_FILE_PATTERN.exec(fileName);
|
|
81
|
+
const componentName = pascalCaseMatch?.[1];
|
|
82
|
+
if (componentName !== undefined) {
|
|
83
|
+
return {
|
|
84
|
+
kind: "flatComponentFile",
|
|
85
|
+
componentName,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
return null;
|
|
89
|
+
};
|
|
90
|
+
exports.analyzeComponentFolderStructure = analyzeComponentFolderStructure;
|
|
91
|
+
/**
|
|
92
|
+
* `components/Button.tsx` → `components/Button/Button.tsx`, relative to the
|
|
93
|
+
* `components/` directory's parent. Every violation this rule reports lives
|
|
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.
|
|
103
|
+
*/
|
|
104
|
+
const getSuggestedSuffixRolePath = (filePath, componentName, suffix) => {
|
|
105
|
+
const normalizedPath = path.normalize(filePath);
|
|
106
|
+
const enclosingModule = getEnclosingModule(normalizedPath);
|
|
107
|
+
const moduleDirectoryName = enclosingModule?.moduleDirectoryName ?? COMPONENTS_DIRECTORY_NAME;
|
|
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`);
|
|
112
|
+
};
|
|
113
|
+
exports.getSuggestedSuffixRolePath = getSuggestedSuffixRolePath;
|
|
114
|
+
//# sourceMappingURL=component-directory-structure-utils.js.map
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ESLint rule: require-hook-util-directory
|
|
3
|
+
*
|
|
4
|
+
* Each hook or util source file must live in its own folder under `hooks/` or
|
|
5
|
+
* `utils/` so specs can colocate (e.g. `hooks/useFoo/useFoo.ts`).
|
|
6
|
+
*/
|
|
7
|
+
export declare const requireHookUtilDirectory: import("@typescript-eslint/utils/ts-eslint").RuleModule<"flatHookOrUtilFile", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
|
|
8
|
+
name: string;
|
|
9
|
+
};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* ESLint rule: require-hook-util-directory
|
|
4
|
+
*
|
|
5
|
+
* Each hook or util source file must live in its own folder under `hooks/` or
|
|
6
|
+
* `utils/` so specs can colocate (e.g. `hooks/useFoo/useFoo.ts`).
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.requireHookUtilDirectory = void 0;
|
|
10
|
+
const tslib_1 = require("tslib");
|
|
11
|
+
const path = tslib_1.__importStar(require("path"));
|
|
12
|
+
const create_rule_1 = require("../../utils/create-rule/create-rule");
|
|
13
|
+
const hook_util_directory_structure_utils_1 = require("./utils/hook-util-directory-structure-utils/hook-util-directory-structure-utils");
|
|
14
|
+
exports.requireHookUtilDirectory = (0, create_rule_1.createRule)({
|
|
15
|
+
name: "require-hook-util-directory",
|
|
16
|
+
meta: {
|
|
17
|
+
type: "suggestion",
|
|
18
|
+
docs: {
|
|
19
|
+
description: "Require hook and util files directly under `hooks/` or `utils/` to live in their own folder, one export per folder.",
|
|
20
|
+
},
|
|
21
|
+
schema: [],
|
|
22
|
+
messages: {
|
|
23
|
+
flatHookOrUtilFile: "Hook and util files must live in their own folder. Move `{{file}}` to `{{suggestedPath}}` and colocate `{{specFile}}` there.",
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
defaultOptions: [],
|
|
27
|
+
create(context) {
|
|
28
|
+
return {
|
|
29
|
+
Program(node) {
|
|
30
|
+
const violation = (0, hook_util_directory_structure_utils_1.analyzeHookUtilFolderStructure)(context.filename);
|
|
31
|
+
if (violation === null) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
const fileName = path.basename(context.filename);
|
|
35
|
+
const specExtension = path.extname(fileName) === ".tsx" ? ".tsx" : ".ts";
|
|
36
|
+
const suggestedPath = (0, hook_util_directory_structure_utils_1.getSuggestedHookUtilFolderPath)(context.filename);
|
|
37
|
+
context.report({
|
|
38
|
+
node,
|
|
39
|
+
messageId: "flatHookOrUtilFile",
|
|
40
|
+
data: {
|
|
41
|
+
file: fileName,
|
|
42
|
+
suggestedPath,
|
|
43
|
+
specFile: `${violation.baseName}.spec${specExtension}`,
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
//# sourceMappingURL=require-hook-util-directory.js.map
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export type HookUtilFolderViolation = {
|
|
2
|
+
readonly kind: "flatHookOrUtilFile";
|
|
3
|
+
readonly roleFolder: "hooks" | "utils";
|
|
4
|
+
readonly baseName: string;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* Returns a violation when a hook or util source file sits directly under
|
|
8
|
+
* `hooks/` or `utils/` instead of its own subfolder.
|
|
9
|
+
*/
|
|
10
|
+
export declare const analyzeHookUtilFolderStructure: (filePath: string) => HookUtilFolderViolation | null;
|
|
11
|
+
/**
|
|
12
|
+
* Suggested path relative to the parent of `hooks/` or `utils/`.
|
|
13
|
+
* `hooks/useFoo.ts` → `hooks/useFoo/useFoo.ts`.
|
|
14
|
+
*/
|
|
15
|
+
export declare const getSuggestedHookUtilFolderPath: (filePath: string) => string;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getSuggestedHookUtilFolderPath = exports.analyzeHookUtilFolderStructure = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const path = tslib_1.__importStar(require("path"));
|
|
6
|
+
const source_tree_utils_1 = require("../../../../utils/source-tree-utils/source-tree-utils");
|
|
7
|
+
const isUnderMocksDirectory = (normalizedPath) => normalizedPath.split(path.sep).includes("__mocks__");
|
|
8
|
+
/**
|
|
9
|
+
* Returns a violation when a hook or util source file sits directly under
|
|
10
|
+
* `hooks/` or `utils/` instead of its own subfolder.
|
|
11
|
+
*/
|
|
12
|
+
const analyzeHookUtilFolderStructure = (filePath) => {
|
|
13
|
+
const normalizedPath = path.normalize(filePath);
|
|
14
|
+
if (!(0, source_tree_utils_1.isSourceFile)(normalizedPath) || isUnderMocksDirectory(normalizedPath)) {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
const baseName = (0, source_tree_utils_1.getBaseName)(path.basename(normalizedPath));
|
|
18
|
+
const parentDirectoryName = path.basename(path.dirname(normalizedPath));
|
|
19
|
+
if (parentDirectoryName !== "hooks" && parentDirectoryName !== "utils") {
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
if (baseName === "index") {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
return {
|
|
26
|
+
kind: "flatHookOrUtilFile",
|
|
27
|
+
roleFolder: parentDirectoryName === "hooks" ? "hooks" : "utils",
|
|
28
|
+
baseName,
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
exports.analyzeHookUtilFolderStructure = analyzeHookUtilFolderStructure;
|
|
32
|
+
/**
|
|
33
|
+
* Suggested path relative to the parent of `hooks/` or `utils/`.
|
|
34
|
+
* `hooks/useFoo.ts` → `hooks/useFoo/useFoo.ts`.
|
|
35
|
+
*/
|
|
36
|
+
const getSuggestedHookUtilFolderPath = (filePath) => {
|
|
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
|
+
};
|
|
43
|
+
exports.getSuggestedHookUtilFolderPath = getSuggestedHookUtilFolderPath;
|
|
44
|
+
//# sourceMappingURL=hook-util-directory-structure-utils.js.map
|
package/src/lib/rules/require-nested-components-directory/require-nested-components-directory.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ESLint rule: require-nested-components-directory
|
|
3
|
+
*
|
|
4
|
+
* When a component module owns a child component, that child lives under
|
|
5
|
+
* `<Parent>/components/`. Contexts, reducers, and route components are excluded.
|
|
6
|
+
*/
|
|
7
|
+
export declare const requireNestedComponentsDirectory: import("@typescript-eslint/utils/ts-eslint").RuleModule<"nestedComponentsNotInSubfolder", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
|
|
8
|
+
name: string;
|
|
9
|
+
};
|
package/src/lib/rules/require-nested-components-directory/require-nested-components-directory.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* ESLint rule: require-nested-components-directory
|
|
4
|
+
*
|
|
5
|
+
* When a component module owns a child component, that child lives under
|
|
6
|
+
* `<Parent>/components/`. Contexts, reducers, and route components are excluded.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.requireNestedComponentsDirectory = void 0;
|
|
10
|
+
const create_rule_1 = require("../../utils/create-rule/create-rule");
|
|
11
|
+
const nested_component_directory_utils_1 = require("./utils/nested-component-directory-utils/nested-component-directory-utils");
|
|
12
|
+
exports.requireNestedComponentsDirectory = (0, create_rule_1.createRule)({
|
|
13
|
+
name: "require-nested-components-directory",
|
|
14
|
+
meta: {
|
|
15
|
+
type: "suggestion",
|
|
16
|
+
docs: {
|
|
17
|
+
description: "Require child components under `<Parent>/components/` instead of beside the parent.",
|
|
18
|
+
},
|
|
19
|
+
schema: [],
|
|
20
|
+
messages: {
|
|
21
|
+
nestedComponentsNotInSubfolder: "Move child components into `components/`. In `{{parentComponentName}}/`, move {{childComponentNames}} under `components/`.",
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
defaultOptions: [],
|
|
25
|
+
create(context) {
|
|
26
|
+
return {
|
|
27
|
+
Program(node) {
|
|
28
|
+
const violation = (0, nested_component_directory_utils_1.analyzeNestedComponentsFolderStructure)(context.filename);
|
|
29
|
+
if (violation === null) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
context.report({
|
|
33
|
+
node,
|
|
34
|
+
messageId: "nestedComponentsNotInSubfolder",
|
|
35
|
+
data: {
|
|
36
|
+
parentComponentName: violation.parentComponentName,
|
|
37
|
+
childComponentNames: violation.childComponentNames.join(", "),
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
//# sourceMappingURL=require-nested-components-directory.js.map
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
type NestedComponentsFolderViolation = {
|
|
2
|
+
readonly kind: "nestedComponentsNotInSubfolder";
|
|
3
|
+
readonly parentComponentName: string;
|
|
4
|
+
readonly childComponentNames: ReadonlyArray<string>;
|
|
5
|
+
};
|
|
6
|
+
export declare const isParentComponentModule: (parentDirectory: string) => boolean;
|
|
7
|
+
/**
|
|
8
|
+
* Returns a violation when `filePath` belongs to a component module that keeps
|
|
9
|
+
* a child component beside the parent instead of under `components/`.
|
|
10
|
+
* A sibling counts only when it exports a component. Compound parts, whose names
|
|
11
|
+
* extend the parent, stay beside the parent. Flat files that extend a plural
|
|
12
|
+
* parent's singular stem stay too. A child folder is not exempted by that stem.
|
|
13
|
+
* Route components are not children.
|
|
14
|
+
*/
|
|
15
|
+
export declare const analyzeNestedComponentsFolderStructure: (filePath: string) => NestedComponentsFolderViolation | null;
|
|
16
|
+
export declare const getSuggestedNestedComponentsPath: (parentDirectory: string, childComponentName: string) => string;
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getSuggestedNestedComponentsPath = exports.analyzeNestedComponentsFolderStructure = exports.isParentComponentModule = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const fs = tslib_1.__importStar(require("fs"));
|
|
6
|
+
const path = tslib_1.__importStar(require("path"));
|
|
7
|
+
const ts = tslib_1.__importStar(require("typescript"));
|
|
8
|
+
const source_tree_utils_1 = require("../../../../utils/source-tree-utils/source-tree-utils");
|
|
9
|
+
const page_directory_structure_utils_1 = require("../../../require-page-directory/utils/page-directory-structure-utils/page-directory-structure-utils");
|
|
10
|
+
const ROLE_SUBFOLDERS = new Set([
|
|
11
|
+
"hooks",
|
|
12
|
+
"utils",
|
|
13
|
+
"types",
|
|
14
|
+
"components",
|
|
15
|
+
"features",
|
|
16
|
+
"context",
|
|
17
|
+
"services",
|
|
18
|
+
"assets",
|
|
19
|
+
"api",
|
|
20
|
+
]);
|
|
21
|
+
const AGGREGATION_FOLDERS = new Set(["shared"]);
|
|
22
|
+
const PASCAL_CASE_TSX_FILE_PATTERN = /^[A-Z][a-zA-Z0-9]*\.tsx$/;
|
|
23
|
+
const REACT_WRAPPER_NAMES = new Set(["memo", "forwardRef"]);
|
|
24
|
+
const isComponentInitializer = (expression) => {
|
|
25
|
+
if (ts.isArrowFunction(expression) || ts.isFunctionExpression(expression)) {
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
return (ts.isCallExpression(expression) &&
|
|
29
|
+
ts.isIdentifier(expression.expression) &&
|
|
30
|
+
REACT_WRAPPER_NAMES.has(expression.expression.text));
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Same idea as `one-component-per-file`: a sibling counts only when it exports a
|
|
34
|
+
* PascalCase function component. A context object or reducer value does not.
|
|
35
|
+
*/
|
|
36
|
+
const hasExportModifier = (node) => ts.canHaveModifiers(node) &&
|
|
37
|
+
ts.getModifiers(node)?.some(modifier => modifier.kind === ts.SyntaxKind.ExportKeyword || modifier.kind === ts.SyntaxKind.DefaultKeyword) === true;
|
|
38
|
+
const sourceExportsComponent = (filePath) => {
|
|
39
|
+
let content;
|
|
40
|
+
try {
|
|
41
|
+
content = fs.readFileSync(filePath, "utf8");
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
const sourceFile = ts.createSourceFile(filePath, content, ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX);
|
|
47
|
+
const localComponentNames = new Set();
|
|
48
|
+
const exportedNames = [];
|
|
49
|
+
let exportsComponent = false;
|
|
50
|
+
let defaultExportName;
|
|
51
|
+
let defaultExportIsComponent = false;
|
|
52
|
+
const rememberComponent = (name) => {
|
|
53
|
+
if (!source_tree_utils_1.PASCAL_CASE_PATTERN.test(name)) {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
localComponentNames.add(name);
|
|
57
|
+
return true;
|
|
58
|
+
};
|
|
59
|
+
for (const statement of sourceFile.statements) {
|
|
60
|
+
if ((ts.isFunctionDeclaration(statement) || ts.isClassDeclaration(statement)) && statement.name !== undefined) {
|
|
61
|
+
if (rememberComponent(statement.name.text) && hasExportModifier(statement)) {
|
|
62
|
+
exportsComponent = true;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
if (ts.isVariableStatement(statement)) {
|
|
66
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
67
|
+
if (ts.isIdentifier(declaration.name) &&
|
|
68
|
+
declaration.initializer !== undefined &&
|
|
69
|
+
isComponentInitializer(declaration.initializer) &&
|
|
70
|
+
rememberComponent(declaration.name.text) &&
|
|
71
|
+
hasExportModifier(statement)) {
|
|
72
|
+
exportsComponent = true;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
if (ts.isExportAssignment(statement) && !statement.isExportEquals) {
|
|
77
|
+
if (ts.isIdentifier(statement.expression)) {
|
|
78
|
+
defaultExportName = statement.expression.text;
|
|
79
|
+
}
|
|
80
|
+
if (isComponentInitializer(statement.expression)) {
|
|
81
|
+
defaultExportIsComponent = true;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (ts.isExportDeclaration(statement) &&
|
|
85
|
+
statement.exportClause !== undefined &&
|
|
86
|
+
ts.isNamedExports(statement.exportClause)) {
|
|
87
|
+
for (const element of statement.exportClause.elements) {
|
|
88
|
+
const localName = element.propertyName ?? element.name;
|
|
89
|
+
if (ts.isIdentifier(localName)) {
|
|
90
|
+
exportedNames.push(localName.text);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
if (defaultExportIsComponent || (defaultExportName !== undefined && localComponentNames.has(defaultExportName))) {
|
|
96
|
+
return true;
|
|
97
|
+
}
|
|
98
|
+
return exportsComponent || exportedNames.some(name => localComponentNames.has(name));
|
|
99
|
+
};
|
|
100
|
+
const startsNewPascalWord = (character) => character !== character.toLowerCase() && character === character.toUpperCase();
|
|
101
|
+
/** `Card` + `CardBody` stays beside the parent, whether the part is a file or a folder. */
|
|
102
|
+
const extendsParentName = (parentComponentName, childComponentName) => childComponentName.startsWith(parentComponentName) && childComponentName.length > parentComponentName.length;
|
|
103
|
+
/**
|
|
104
|
+
* A plural parent (`Tabs`) keeps flat files that extend the singular stem
|
|
105
|
+
* (`Tab.tsx`, `TabList.tsx`, `TabContent.tsx`). Child folders do not use this
|
|
106
|
+
* exemption, so `Services/ServiceCard/` is still a private child.
|
|
107
|
+
*/
|
|
108
|
+
const extendsSingularStem = (parentComponentName, childComponentName) => {
|
|
109
|
+
if (!parentComponentName.endsWith("s")) {
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
const stem = parentComponentName.slice(0, -1);
|
|
113
|
+
if (childComponentName === stem) {
|
|
114
|
+
return true;
|
|
115
|
+
}
|
|
116
|
+
return (childComponentName.startsWith(stem) &&
|
|
117
|
+
childComponentName.length > stem.length &&
|
|
118
|
+
startsNewPascalWord(childComponentName.charAt(stem.length)));
|
|
119
|
+
};
|
|
120
|
+
const isComponentFolder = (directoryPath) => {
|
|
121
|
+
try {
|
|
122
|
+
const entries = fs.readdirSync(directoryPath, { withFileTypes: true });
|
|
123
|
+
return entries.some(entry => entry.isFile() && PASCAL_CASE_TSX_FILE_PATTERN.test(entry.name));
|
|
124
|
+
}
|
|
125
|
+
catch {
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
const getDirectChildComponentNames = (parentDirectory) => {
|
|
130
|
+
try {
|
|
131
|
+
const entries = fs.readdirSync(parentDirectory, { withFileTypes: true });
|
|
132
|
+
const parentComponentName = path.basename(parentDirectory);
|
|
133
|
+
const childNames = entries.flatMap(entry => {
|
|
134
|
+
if (ROLE_SUBFOLDERS.has(entry.name)) {
|
|
135
|
+
return [];
|
|
136
|
+
}
|
|
137
|
+
if (entry.isDirectory() &&
|
|
138
|
+
source_tree_utils_1.PASCAL_CASE_PATTERN.test(entry.name) &&
|
|
139
|
+
!extendsParentName(parentComponentName, entry.name) &&
|
|
140
|
+
isComponentFolder(path.join(parentDirectory, entry.name)) &&
|
|
141
|
+
!(0, page_directory_structure_utils_1.isRegisteredRouteComponentFile)(path.join(parentDirectory, entry.name, `${entry.name}.tsx`)) &&
|
|
142
|
+
sourceExportsComponent(path.join(parentDirectory, entry.name, `${entry.name}.tsx`))) {
|
|
143
|
+
return [entry.name];
|
|
144
|
+
}
|
|
145
|
+
if (entry.isFile() && PASCAL_CASE_TSX_FILE_PATTERN.test(entry.name)) {
|
|
146
|
+
const componentName = (0, source_tree_utils_1.getBaseName)(entry.name);
|
|
147
|
+
const childPath = path.join(parentDirectory, entry.name);
|
|
148
|
+
if (componentName === parentComponentName ||
|
|
149
|
+
extendsParentName(parentComponentName, componentName) ||
|
|
150
|
+
extendsSingularStem(parentComponentName, componentName)) {
|
|
151
|
+
return [];
|
|
152
|
+
}
|
|
153
|
+
if ((0, page_directory_structure_utils_1.isRegisteredRouteComponentFile)(childPath) || !sourceExportsComponent(childPath)) {
|
|
154
|
+
return [];
|
|
155
|
+
}
|
|
156
|
+
return [componentName];
|
|
157
|
+
}
|
|
158
|
+
return [];
|
|
159
|
+
});
|
|
160
|
+
return [...new Set(childNames)].sort();
|
|
161
|
+
}
|
|
162
|
+
catch {
|
|
163
|
+
return [];
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
const isParentComponentModule = (parentDirectory) => {
|
|
167
|
+
const parentComponentName = path.basename(parentDirectory);
|
|
168
|
+
if (!source_tree_utils_1.PASCAL_CASE_PATTERN.test(parentComponentName) || AGGREGATION_FOLDERS.has(parentComponentName)) {
|
|
169
|
+
return false;
|
|
170
|
+
}
|
|
171
|
+
return fs.existsSync(path.join(parentDirectory, `${parentComponentName}.tsx`));
|
|
172
|
+
};
|
|
173
|
+
exports.isParentComponentModule = isParentComponentModule;
|
|
174
|
+
/**
|
|
175
|
+
* Returns a violation when `filePath` belongs to a component module that keeps
|
|
176
|
+
* a child component beside the parent instead of under `components/`.
|
|
177
|
+
* A sibling counts only when it exports a component. Compound parts, whose names
|
|
178
|
+
* extend the parent, stay beside the parent. Flat files that extend a plural
|
|
179
|
+
* parent's singular stem stay too. A child folder is not exempted by that stem.
|
|
180
|
+
* Route components are not children.
|
|
181
|
+
*/
|
|
182
|
+
const analyzeNestedComponentsFolderStructure = (filePath) => {
|
|
183
|
+
const normalizedPath = path.normalize(filePath);
|
|
184
|
+
const parentDirectory = path.dirname(normalizedPath);
|
|
185
|
+
const fileName = path.basename(normalizedPath);
|
|
186
|
+
const parentComponentName = path.basename(parentDirectory);
|
|
187
|
+
if (fileName !== `${parentComponentName}.tsx`) {
|
|
188
|
+
return null;
|
|
189
|
+
}
|
|
190
|
+
if (!(0, exports.isParentComponentModule)(parentDirectory)) {
|
|
191
|
+
return null;
|
|
192
|
+
}
|
|
193
|
+
const directChildComponentNames = getDirectChildComponentNames(parentDirectory);
|
|
194
|
+
if (directChildComponentNames.length === 0) {
|
|
195
|
+
return null;
|
|
196
|
+
}
|
|
197
|
+
return {
|
|
198
|
+
kind: "nestedComponentsNotInSubfolder",
|
|
199
|
+
parentComponentName,
|
|
200
|
+
childComponentNames: directChildComponentNames,
|
|
201
|
+
};
|
|
202
|
+
};
|
|
203
|
+
exports.analyzeNestedComponentsFolderStructure = analyzeNestedComponentsFolderStructure;
|
|
204
|
+
const getSuggestedNestedComponentsPath = (parentDirectory, childComponentName) => path.join(parentDirectory, "components", childComponentName);
|
|
205
|
+
exports.getSuggestedNestedComponentsPath = getSuggestedNestedComponentsPath;
|
|
206
|
+
//# sourceMappingURL=nested-component-directory-utils.js.map
|
package/src/lib/rules/require-nested-components-privacy/require-nested-components-privacy.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ESLint rule: require-nested-components-privacy
|
|
3
|
+
*
|
|
4
|
+
* Child components under `<Parent>/components/` are private to the parent module.
|
|
5
|
+
* Blocks relative imports and re-exports of those children from outside the parent.
|
|
6
|
+
*/
|
|
7
|
+
export declare const requireNestedComponentsPrivacy: import("@typescript-eslint/utils/ts-eslint").RuleModule<"nestedComponentUsedOutsideParent", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
|
|
8
|
+
name: string;
|
|
9
|
+
};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* ESLint rule: require-nested-components-privacy
|
|
4
|
+
*
|
|
5
|
+
* Child components under `<Parent>/components/` are private to the parent module.
|
|
6
|
+
* Blocks relative imports and re-exports of those children from outside the parent.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.requireNestedComponentsPrivacy = void 0;
|
|
10
|
+
const create_rule_1 = require("../../utils/create-rule/create-rule");
|
|
11
|
+
const nested_component_privacy_utils_1 = require("./utils/nested-component-privacy-utils/nested-component-privacy-utils");
|
|
12
|
+
exports.requireNestedComponentsPrivacy = (0, create_rule_1.createRule)({
|
|
13
|
+
name: "require-nested-components-privacy",
|
|
14
|
+
meta: {
|
|
15
|
+
type: "suggestion",
|
|
16
|
+
docs: {
|
|
17
|
+
description: "Disallow importing child components from another component module's private `components/` subfolder.",
|
|
18
|
+
},
|
|
19
|
+
schema: [],
|
|
20
|
+
messages: {
|
|
21
|
+
nestedComponentUsedOutsideParent: "`{{childComponentName}}` is private to `{{parentComponentName}}`. Import it from a shared " +
|
|
22
|
+
"location instead of `{{parentComponentName}}/components/`.",
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
defaultOptions: [],
|
|
26
|
+
create(context) {
|
|
27
|
+
const reportPrivateImport = (source) => {
|
|
28
|
+
if (source.type !== "Literal" || typeof source.value !== "string") {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
const violation = (0, nested_component_privacy_utils_1.analyzeNestedComponentImport)(context.filename, source.value);
|
|
32
|
+
if (violation === null) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
context.report({
|
|
36
|
+
node: source,
|
|
37
|
+
messageId: "nestedComponentUsedOutsideParent",
|
|
38
|
+
data: {
|
|
39
|
+
parentComponentName: violation.parentComponentName,
|
|
40
|
+
childComponentName: violation.childComponentName,
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
return {
|
|
45
|
+
ImportDeclaration(node) {
|
|
46
|
+
reportPrivateImport(node.source);
|
|
47
|
+
},
|
|
48
|
+
ImportExpression(node) {
|
|
49
|
+
reportPrivateImport(node.source);
|
|
50
|
+
},
|
|
51
|
+
ExportNamedDeclaration(node) {
|
|
52
|
+
if (node.source === null) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
reportPrivateImport(node.source);
|
|
56
|
+
},
|
|
57
|
+
ExportAllDeclaration(node) {
|
|
58
|
+
reportPrivateImport(node.source);
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
//# sourceMappingURL=require-nested-components-privacy.js.map
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export type NestedComponentPrivacyViolation = {
|
|
2
|
+
readonly parentComponentName: string;
|
|
3
|
+
readonly childComponentName: string;
|
|
4
|
+
};
|
|
5
|
+
/**
|
|
6
|
+
* Returns a violation when `importerFilePath` imports a component from another
|
|
7
|
+
* component module's private `components/` subfolder via a relative specifier.
|
|
8
|
+
*
|
|
9
|
+
* Scope (by design):
|
|
10
|
+
* - Only `components/<Parent>/<Parent>.tsx` modules that contain a matching component file count as owners.
|
|
11
|
+
* - Only paths under `…/<Parent>/components/<Child>/` are treated as private children.
|
|
12
|
+
* - Does not cover `pages/`, `hooks/`, `utils/`, or package imports — promote shared UI to module `components/`.
|
|
13
|
+
*/
|
|
14
|
+
export declare const analyzeNestedComponentImport: (importerFilePath: string, importSource: string) => NestedComponentPrivacyViolation | null;
|