@trackunit/eslint-plugin-trackunit 0.7.19 → 0.7.20-alpha-1872e1df7fb.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/CHANGELOG.md +0 -92
  2. package/README.md +0 -13
  3. package/package.json +2 -2
  4. package/src/index.d.ts +1 -1
  5. package/src/index.js +1 -2
  6. package/src/lib/config/index.d.ts +0 -32
  7. package/src/lib/config/plugins.d.ts +0 -26
  8. package/src/lib/config/plugins.js +1 -19
  9. package/src/lib/config/presets/base.d.ts +0 -9
  10. package/src/lib/config/presets/react.d.ts +0 -23
  11. package/src/lib/config/presets/react.js +0 -4
  12. package/src/lib/rules/no-unused-translation-key/used-strings.js +1 -1
  13. package/src/lib/rules-map.d.ts +0 -3
  14. package/src/lib/rules-map.js +0 -2
  15. package/src/lib/{rules/no-unused-translation-key/utils/program-utils → utils}/program-utils.js +1 -1
  16. package/src/lib/rules/require-component-directory/require-component-directory.d.ts +0 -16
  17. package/src/lib/rules/require-component-directory/require-component-directory.js +0 -240
  18. package/src/lib/rules/require-component-directory/utils/component-directory-structure-utils/component-directory-structure-utils.d.ts +0 -34
  19. package/src/lib/rules/require-component-directory/utils/component-directory-structure-utils/component-directory-structure-utils.js +0 -114
  20. package/src/lib/rules/require-hook-util-directory/require-hook-util-directory.d.ts +0 -9
  21. package/src/lib/rules/require-hook-util-directory/require-hook-util-directory.js +0 -50
  22. package/src/lib/rules/require-hook-util-directory/utils/hook-util-directory-structure-utils/hook-util-directory-structure-utils.d.ts +0 -15
  23. package/src/lib/rules/require-hook-util-directory/utils/hook-util-directory-structure-utils/hook-util-directory-structure-utils.js +0 -44
  24. package/src/lib/rules/require-iris-app-ai-navigation/require-iris-app-ai-navigation.d.ts +0 -4
  25. package/src/lib/rules/require-iris-app-ai-navigation/require-iris-app-ai-navigation.js +0 -61
  26. package/src/lib/rules/require-nested-components-directory/require-nested-components-directory.d.ts +0 -9
  27. package/src/lib/rules/require-nested-components-directory/require-nested-components-directory.js +0 -44
  28. package/src/lib/rules/require-nested-components-directory/utils/nested-component-directory-utils/nested-component-directory-utils.d.ts +0 -17
  29. package/src/lib/rules/require-nested-components-directory/utils/nested-component-directory-utils/nested-component-directory-utils.js +0 -206
  30. package/src/lib/rules/require-nested-components-privacy/require-nested-components-privacy.d.ts +0 -9
  31. package/src/lib/rules/require-nested-components-privacy/require-nested-components-privacy.js +0 -63
  32. package/src/lib/rules/require-nested-components-privacy/utils/nested-component-privacy-utils/nested-component-privacy-utils.d.ts +0 -14
  33. package/src/lib/rules/require-nested-components-privacy/utils/nested-component-privacy-utils/nested-component-privacy-utils.js +0 -62
  34. package/src/lib/rules/require-page-directory/require-page-directory.d.ts +0 -8
  35. package/src/lib/rules/require-page-directory/require-page-directory.js +0 -44
  36. package/src/lib/rules/require-page-directory/utils/page-directory-structure-utils/page-directory-structure-utils.d.ts +0 -18
  37. package/src/lib/rules/require-page-directory/utils/page-directory-structure-utils/page-directory-structure-utils.js +0 -159
  38. package/src/lib/utils/create-rule/create-rule.d.ts +0 -7
  39. package/src/lib/utils/create-rule/create-rule.js +0 -9
  40. package/src/lib/utils/source-tree-utils/source-tree-utils.d.ts +0 -43
  41. package/src/lib/utils/source-tree-utils/source-tree-utils.js +0 -136
  42. /package/src/lib/{rules/no-unused-translation-key/utils/program-utils → utils}/program-utils.d.ts +0 -0
@@ -1,206 +0,0 @@
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
@@ -1,9 +0,0 @@
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
- };
@@ -1,63 +0,0 @@
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
@@ -1,14 +0,0 @@
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;
@@ -1,62 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.analyzeNestedComponentImport = 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 nested_component_directory_utils_1 = require("../../../require-nested-components-directory/utils/nested-component-directory-utils/nested-component-directory-utils");
8
- const AGGREGATION_FOLDERS = new Set(["shared"]);
9
- /**
10
- * Returns a violation when `importerFilePath` imports a component from another
11
- * component module's private `components/` subfolder via a relative specifier.
12
- *
13
- * Scope (by design):
14
- * - Only `components/<Parent>/<Parent>.tsx` modules that contain a matching component file count as owners.
15
- * - Only paths under `…/<Parent>/components/<Child>/` are treated as private children.
16
- * - Does not cover `pages/`, `hooks/`, `utils/`, or package imports — promote shared UI to module `components/`.
17
- */
18
- const analyzeNestedComponentImport = (importerFilePath, importSource) => {
19
- if (!importSource.startsWith(".")) {
20
- return null;
21
- }
22
- const normalizedImporterPath = path.normalize(importerFilePath);
23
- const importerDirectory = path.dirname(normalizedImporterPath);
24
- const resolvedImportPath = path.normalize(path.resolve(importerDirectory, importSource));
25
- const segments = resolvedImportPath.split(path.sep);
26
- let parentComponentName;
27
- let childComponentName;
28
- let parentComponentDirectory = "";
29
- for (let index = 1; index < segments.length - 1; index += 1) {
30
- const childName = segments[index + 1];
31
- const parentName = segments[index - 1];
32
- if (segments[index] !== "components" || parentName === undefined || childName === undefined) {
33
- continue;
34
- }
35
- if (!source_tree_utils_1.PASCAL_CASE_PATTERN.test(childName) || !source_tree_utils_1.PASCAL_CASE_PATTERN.test(parentName)) {
36
- continue;
37
- }
38
- const ownerComponentDirectory = segments.slice(0, index).join(path.sep);
39
- if (path.basename(path.dirname(ownerComponentDirectory)) !== "components" ||
40
- !(0, nested_component_directory_utils_1.isParentComponentModule)(ownerComponentDirectory) ||
41
- AGGREGATION_FOLDERS.has(parentName)) {
42
- continue;
43
- }
44
- parentComponentName = parentName;
45
- childComponentName = childName;
46
- parentComponentDirectory = ownerComponentDirectory;
47
- }
48
- if (parentComponentName === undefined || childComponentName === undefined) {
49
- return null;
50
- }
51
- const importerWithinParent = normalizedImporterPath === parentComponentDirectory ||
52
- normalizedImporterPath.startsWith(`${parentComponentDirectory}${path.sep}`);
53
- if (importerWithinParent) {
54
- return null;
55
- }
56
- return {
57
- parentComponentName,
58
- childComponentName,
59
- };
60
- };
61
- exports.analyzeNestedComponentImport = analyzeNestedComponentImport;
62
- //# sourceMappingURL=nested-component-privacy-utils.js.map
@@ -1,8 +0,0 @@
1
- /**
2
- * ESLint rule: require-page-directory
3
- *
4
- * Components passed to a route as `component` must live under a `pages/` directory.
5
- */
6
- export declare const requirePageDirectory: import("@typescript-eslint/utils/ts-eslint").RuleModule<"routeComponentOutsidePagesFolder", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
7
- name: string;
8
- };
@@ -1,44 +0,0 @@
1
- "use strict";
2
- /**
3
- * ESLint rule: require-page-directory
4
- *
5
- * Components passed to a route as `component` must live under a `pages/` directory.
6
- */
7
- Object.defineProperty(exports, "__esModule", { value: true });
8
- exports.requirePageDirectory = void 0;
9
- const create_rule_1 = require("../../utils/create-rule/create-rule");
10
- const page_directory_structure_utils_1 = require("./utils/page-directory-structure-utils/page-directory-structure-utils");
11
- exports.requirePageDirectory = (0, create_rule_1.createRule)({
12
- name: "require-page-directory",
13
- meta: {
14
- type: "suggestion",
15
- docs: {
16
- description: "Require route components to live under a `pages/` directory.",
17
- },
18
- schema: [],
19
- messages: {
20
- routeComponentOutsidePagesFolder: "`{{pageComponentName}}` is used as a route component in `{{routeFilePath}}`. Move it to `{{suggestedRelativePath}}`.",
21
- },
22
- },
23
- defaultOptions: [],
24
- create(context) {
25
- return {
26
- Program(node) {
27
- const violation = (0, page_directory_structure_utils_1.analyzePageFolderStructure)(context.filename);
28
- if (violation?.kind !== "routeComponentOutsidePagesFolder") {
29
- return;
30
- }
31
- context.report({
32
- node,
33
- messageId: "routeComponentOutsidePagesFolder",
34
- data: {
35
- pageComponentName: violation.pageComponentName,
36
- routeFilePath: violation.routeFilePath,
37
- suggestedRelativePath: violation.suggestedRelativePath,
38
- },
39
- });
40
- },
41
- };
42
- },
43
- });
44
- //# sourceMappingURL=require-page-directory.js.map
@@ -1,18 +0,0 @@
1
- type PageFolderViolation = {
2
- readonly kind: "routeComponentOutsidePagesFolder";
3
- readonly pageComponentName: string;
4
- readonly routeFilePath: string;
5
- readonly suggestedRelativePath: string;
6
- };
7
- /** True when `filePath` is the file a route registers as `component`. */
8
- export declare const isRegisteredRouteComponentFile: (filePath: string) => boolean;
9
- /**
10
- * Returns a violation when `filePath` is the `component` of a route but does not
11
- * live in `pages/<Name>/<Name>.tsx` (including `<Scope>/pages/<Name>/`).
12
- *
13
- * `App` is not treated as a page. A nested file stays exempt only while the route
14
- * component file is still in that folder. After the page moves, a child left
15
- * behind is outside `pages/`.
16
- */
17
- export declare const analyzePageFolderStructure: (filePath: string) => PageFolderViolation | null;
18
- export {};
@@ -1,159 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.analyzePageFolderStructure = exports.isRegisteredRouteComponentFile = void 0;
4
- const tslib_1 = require("tslib");
5
- const path = tslib_1.__importStar(require("path"));
6
- const ts = tslib_1.__importStar(require("typescript"));
7
- const source_tree_utils_1 = require("../../../../utils/source-tree-utils/source-tree-utils");
8
- const ROUTE_FILE_NAMES = new Set(["router.tsx", "routeTree.tsx", "routes.tsx"]);
9
- const isRouteDefinitionFile = (filePath) => {
10
- const fileName = path.basename(filePath);
11
- return (ROUTE_FILE_NAMES.has(fileName) ||
12
- fileName.endsWith(".routes.ts") ||
13
- fileName.endsWith(".routes.tsx") ||
14
- fileName.endsWith("Routes.tsx") ||
15
- fileName.endsWith("Route.tsx"));
16
- };
17
- const isPageComponentName = (componentName) => componentName !== "App";
18
- const isNamedPageFile = (srcRoot, filePath) => {
19
- const segments = path.relative(srcRoot, filePath).split(path.sep);
20
- const pagesIndex = segments.lastIndexOf("pages");
21
- const fileName = segments.at(-1);
22
- const folderName = segments.at(-2);
23
- if (pagesIndex === -1 || fileName === undefined || folderName === undefined) {
24
- return false;
25
- }
26
- return folderName === (0, source_tree_utils_1.getBaseName)(fileName);
27
- };
28
- const parseRouteFile = (filePath, content) => {
29
- const sourceFile = ts.createSourceFile(filePath, content, ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX);
30
- const importMap = new Map();
31
- const localComponentBindings = new Map();
32
- const componentNames = [];
33
- const visit = (node) => {
34
- if (ts.isImportDeclaration(node) && ts.isStringLiteral(node.moduleSpecifier) && node.importClause !== undefined) {
35
- const source = node.moduleSpecifier.text;
36
- if (node.importClause.name !== undefined) {
37
- importMap.set(node.importClause.name.text, source);
38
- }
39
- const bindings = node.importClause.namedBindings;
40
- if (bindings !== undefined && ts.isNamedImports(bindings)) {
41
- for (const element of bindings.elements) {
42
- importMap.set(element.name.text, source);
43
- }
44
- }
45
- }
46
- if (ts.isVariableDeclaration(node) &&
47
- ts.isIdentifier(node.name) &&
48
- node.initializer !== undefined &&
49
- ts.isIdentifier(node.initializer)) {
50
- localComponentBindings.set(node.name.text, node.initializer.text);
51
- }
52
- if (ts.isPropertyAssignment(node) &&
53
- ts.isIdentifier(node.name) &&
54
- node.name.text === "component" &&
55
- ts.isIdentifier(node.initializer)) {
56
- componentNames.push(node.initializer.text);
57
- }
58
- if (ts.isShorthandPropertyAssignment(node) && node.name.text === "component") {
59
- const resolvedName = localComponentBindings.get("component") ?? node.name.text;
60
- componentNames.push(resolvedName);
61
- }
62
- ts.forEachChild(node, visit);
63
- };
64
- visit(sourceFile);
65
- return { importMap, componentNames };
66
- };
67
- const buildRouteComponentIndex = (srcRoot) => {
68
- const routeFileByComponent = new Map();
69
- for (const sourceFile of (0, source_tree_utils_1.walkSourceFiles)(srcRoot)) {
70
- if (!isRouteDefinitionFile(sourceFile)) {
71
- continue;
72
- }
73
- const content = (0, source_tree_utils_1.readSourceFile)(sourceFile);
74
- if (content === null) {
75
- continue;
76
- }
77
- const { importMap, componentNames } = parseRouteFile(sourceFile, content);
78
- for (const componentName of componentNames) {
79
- if (!isPageComponentName(componentName)) {
80
- continue;
81
- }
82
- const importSource = importMap.get(componentName);
83
- const resolvedPath = importSource === undefined ? null : (0, source_tree_utils_1.resolveRelativeImportPath)(sourceFile, importSource);
84
- if (resolvedPath !== null && !routeFileByComponent.has(resolvedPath)) {
85
- routeFileByComponent.set(resolvedPath, sourceFile);
86
- }
87
- }
88
- }
89
- return routeFileByComponent;
90
- };
91
- const routeComponentIndexCache = (0, source_tree_utils_1.createSrcRootCache)(buildRouteComponentIndex);
92
- /** True when `filePath` is the file a route registers as `component`. */
93
- const isRegisteredRouteComponentFile = (filePath) => {
94
- const srcRoot = (0, source_tree_utils_1.getSrcRoot)(filePath);
95
- if (srcRoot === null) {
96
- return false;
97
- }
98
- return routeComponentIndexCache.get(srcRoot).has(path.normalize(filePath));
99
- };
100
- exports.isRegisteredRouteComponentFile = isRegisteredRouteComponentFile;
101
- const isNestedUnderRouteComponentModuleFolder = (srcRoot, filePath, routeFileByComponent) => {
102
- const routeComponentNames = new Set();
103
- for (const routeComponentPath of routeFileByComponent.keys()) {
104
- routeComponentNames.add((0, source_tree_utils_1.getBaseName)(path.basename(routeComponentPath)));
105
- }
106
- const relativeDirectory = path.relative(srcRoot, path.dirname(filePath));
107
- const segments = relativeDirectory.split(path.sep).filter(segment => segment.length > 0);
108
- for (let index = 0; index < segments.length; index += 1) {
109
- const segment = segments[index];
110
- if (segment === undefined || !routeComponentNames.has(segment)) {
111
- continue;
112
- }
113
- const moduleFolder = path.join(srcRoot, ...segments.slice(0, index + 1));
114
- const routeComponentFile = path.join(moduleFolder, `${segment}.tsx`);
115
- if (!routeFileByComponent.has(routeComponentFile)) {
116
- continue;
117
- }
118
- if (filePath !== routeComponentFile && filePath.startsWith(`${moduleFolder}${path.sep}`)) {
119
- return true;
120
- }
121
- }
122
- return false;
123
- };
124
- const getSuggestedPagePath = (srcRoot, componentFilePath) => {
125
- const relativeSegments = path.relative(srcRoot, componentFilePath).split(path.sep);
126
- const fileName = path.basename(componentFilePath);
127
- const componentsIndex = relativeSegments.indexOf("components");
128
- const scope = componentsIndex > 0 ? relativeSegments.slice(0, componentsIndex) : [];
129
- return path.join(...scope, "pages", (0, source_tree_utils_1.getBaseName)(fileName), fileName);
130
- };
131
- /**
132
- * Returns a violation when `filePath` is the `component` of a route but does not
133
- * live in `pages/<Name>/<Name>.tsx` (including `<Scope>/pages/<Name>/`).
134
- *
135
- * `App` is not treated as a page. A nested file stays exempt only while the route
136
- * component file is still in that folder. After the page moves, a child left
137
- * behind is outside `pages/`.
138
- */
139
- const analyzePageFolderStructure = (filePath) => {
140
- const normalizedPath = path.normalize(filePath);
141
- const srcRoot = (0, source_tree_utils_1.getSrcRoot)(normalizedPath);
142
- if (srcRoot === null || isNamedPageFile(srcRoot, normalizedPath)) {
143
- return null;
144
- }
145
- const routeFileByComponent = routeComponentIndexCache.get(srcRoot);
146
- const routeFilePath = routeFileByComponent.get(normalizedPath);
147
- if (routeFilePath === undefined ||
148
- isNestedUnderRouteComponentModuleFolder(srcRoot, normalizedPath, routeFileByComponent)) {
149
- return null;
150
- }
151
- return {
152
- kind: "routeComponentOutsidePagesFolder",
153
- pageComponentName: (0, source_tree_utils_1.getBaseName)(path.basename(normalizedPath)),
154
- routeFilePath: path.relative(srcRoot, routeFilePath),
155
- suggestedRelativePath: getSuggestedPagePath(srcRoot, normalizedPath),
156
- };
157
- };
158
- exports.analyzePageFolderStructure = analyzePageFolderStructure;
159
- //# sourceMappingURL=page-directory-structure-utils.js.map
@@ -1,7 +0,0 @@
1
- import { ESLintUtils } from "@typescript-eslint/utils";
2
- /**
3
- * Shared rule factory so every rule links to its own source on the default branch.
4
- */
5
- export declare const createRule: <Options extends readonly unknown[], MessageIds extends string>({ meta, name, ...rule }: Readonly<ESLintUtils.RuleWithMetaAndName<Options, MessageIds, unknown>>) => ESLintUtils.RuleModule<MessageIds, Options, unknown, ESLintUtils.RuleListener> & {
6
- name: string;
7
- };
@@ -1,9 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createRule = void 0;
4
- const utils_1 = require("@typescript-eslint/utils");
5
- /**
6
- * Shared rule factory so every rule links to its own source on the default branch.
7
- */
8
- exports.createRule = utils_1.ESLintUtils.RuleCreator(name => `https://github.com/Trackunit/manager/blob/master/libs/eslint/plugin-trackunit/src/lib/rules/${name}/${name}.ts`);
9
- //# sourceMappingURL=create-rule.js.map
@@ -1,43 +0,0 @@
1
- /**
2
- * Shared helpers for folder-structure rules that need to look beyond the file
3
- * ESLint is currently linting: walking a library's `src/` tree and resolving
4
- * relative imports.
5
- *
6
- * ## Main Functions
7
- * - `getSrcRoot()` - Locate the `src/` directory that owns a file
8
- * - `walkSourceFiles()` - List every non-spec/story source file under a directory
9
- * - `resolveRelativeImportPath()` - Resolve `./foo` the way TypeScript would (`.ts`, `.tsx`, `index.*`)
10
- * - `createSrcRootCache()` - Memoise an expensive per-`src/`-root computation
11
- * - `clearSourceTreeCaches()` - Reset every cache created here. Useful for testing.
12
- */
13
- export declare const SOURCE_FILE_EXTENSIONS: readonly [".ts", ".tsx", ".js", ".jsx"];
14
- export declare const PASCAL_CASE_PATTERN: RegExp;
15
- export declare const isSourceFile: (filePath: string) => boolean;
16
- /** `useFoo.spec.tsx` → `useFoo.spec`; `Button.tsx` → `Button`. */
17
- export declare const getBaseName: (fileName: string) => string;
18
- /** The innermost `…/src` directory containing `filePath`, or `null` when there is none. */
19
- export declare const getSrcRoot: (filePath: string) => string | null;
20
- export declare const readSourceFile: (filePath: string) => string | null;
21
- export declare const walkSourceFiles: (directory: string) => ReadonlyArray<string>;
22
- /**
23
- * Resolve a relative import the way TypeScript does: exact path, then each
24
- * source extension, then a directory `index.*`. Non-relative specifiers
25
- * (packages, path aliases) resolve to `null`.
26
- */
27
- export declare const resolveRelativeImportPath: (importerFilePath: string, importSource: string) => string | null;
28
- type SrcRootCache<TValue> = {
29
- readonly get: (srcRoot: string) => TValue;
30
- };
31
- /**
32
- * Memoise `compute` per `src/` root for the lifetime of the ESLint process.
33
- *
34
- * Rules that inspect the whole tree would otherwise redo the same walk for every
35
- * file they lint. The trade-off is that a long-lived editor ESLint server sees a
36
- * snapshot from its first lint of each library until it is restarted.
37
- */
38
- export declare const createSrcRootCache: <TValue>(compute: (srcRoot: string) => TValue) => SrcRootCache<TValue>;
39
- /**
40
- * Clear every cache created with `createSrcRootCache`. Useful for testing.
41
- */
42
- export declare const clearSourceTreeCaches: () => void;
43
- export {};