@dloizides/frontend-devtools 1.0.1 → 1.0.3
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/dist/eslint/index.d.mts +77 -0
- package/dist/eslint/index.d.ts +77 -0
- package/dist/eslint/index.js +2000 -0
- package/dist/eslint/index.js.map +1 -0
- package/dist/eslint/index.mjs +1990 -0
- package/dist/eslint/index.mjs.map +1 -0
- package/dist/index.d.mts +69 -0
- package/dist/index.d.ts +69 -0
- package/dist/index.js +37 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +31 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +6 -1
- package/.eslintrc.js +0 -34
- package/jest.config.js +0 -24
- package/publish.ps1 +0 -107
- package/src/eslint/index.ts +0 -16
- package/src/eslint/legacy.ts +0 -44
- package/src/eslint/plugin.test.ts +0 -60
- package/src/eslint/plugin.ts +0 -87
- package/src/eslint/rules.behaviour.test.ts +0 -86
- package/src/eslint/rules.d.ts +0 -16
- package/src/eslint/types.ts +0 -27
- package/src/i18n/initI18n.test.ts +0 -101
- package/src/i18n/initI18n.ts +0 -109
- package/src/index.ts +0 -31
- package/src/rules/enforce-function-style.js +0 -278
- package/src/rules/enforce-module-structure.js +0 -323
- package/src/rules/enforce-route-preload.js +0 -288
- package/src/rules/enforce-test-colocation.js +0 -82
- package/src/rules/enum-file-isolation.js +0 -149
- package/src/rules/i18n-interpolation.js +0 -185
- package/src/rules/i18n-param-names.js +0 -119
- package/src/rules/no-barrel-companion-file.js +0 -142
- package/src/rules/no-duplicate-nav-prefix.js +0 -107
- package/src/rules/no-duplicate-shared-patterns.js +0 -166
- package/src/rules/no-generated-models-barrel-value-import.js +0 -70
- package/src/rules/no-null-check.js +0 -171
- package/src/rules/no-optional-undefined.js +0 -99
- package/src/rules/no-product-imports-in-shared.js +0 -164
- package/src/rules/prefer-const-enum.js +0 -127
- package/src/rules/require-stable-hook-args.js +0 -372
- package/src/rules/smart-max-lines.js +0 -344
- package/tsconfig.json +0 -25
- package/tsconfig.test.json +0 -11
- package/tsup.config.ts +0 -14
|
@@ -1,323 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Custom ESLint Plugin: Enforce Module Structure
|
|
3
|
-
*
|
|
4
|
-
* When a component/feature directory contains 4+ non-test source files,
|
|
5
|
-
* this rule enforces a standard internal structure:
|
|
6
|
-
*
|
|
7
|
-
* hooks/ — Custom hooks (use*.ts)
|
|
8
|
-
* components/ — Sub-components (non-index .tsx files)
|
|
9
|
-
* utils/ — Pure helper functions (non-hook, non-component .ts files)
|
|
10
|
-
* data/ — Static/mock data files (*Data.ts, *.data.ts)
|
|
11
|
-
*
|
|
12
|
-
* Files that may stay at the directory root:
|
|
13
|
-
* index.tsx, index.ts, types.ts, constants.ts, schema.ts, columns.ts
|
|
14
|
-
*
|
|
15
|
-
* The rule also allows domain-specific subdirectories (e.g. filters/,
|
|
16
|
-
* columnMenu/, pagination/) — files inside those are exempt because
|
|
17
|
-
* they are already organized by concern.
|
|
18
|
-
*
|
|
19
|
-
* Special directory patterns exempt from sub-component checks:
|
|
20
|
-
* sections/ — Feature page sections (.tsx files are the primary content)
|
|
21
|
-
* forms/ — Form components (.tsx are the primary content)
|
|
22
|
-
*
|
|
23
|
-
* Threshold: only enforced when a directory has >= fileThreshold source
|
|
24
|
-
* files (default 4). Simple components with 1-3 files are left alone.
|
|
25
|
-
*
|
|
26
|
-
* Examples:
|
|
27
|
-
* BAD: DataGrid/useGridCallbacks.ts (hook at root, 8 siblings)
|
|
28
|
-
* GOOD: DataGrid/hooks/useGridCallbacks.ts (hook in hooks/ subdir)
|
|
29
|
-
*
|
|
30
|
-
* BAD: sections/data.ts (data mixed with sections)
|
|
31
|
-
* GOOD: data/gridData.ts (data in dedicated subdir)
|
|
32
|
-
*
|
|
33
|
-
* OK: sections/BasicGridSection.tsx (sections dir, .tsx exempt)
|
|
34
|
-
* OK: TableNative/columnMenu/useColumnMenu.ts (domain subdir, exempt)
|
|
35
|
-
* OK: Button/index.tsx (only 2 files, below threshold)
|
|
36
|
-
*/
|
|
37
|
-
|
|
38
|
-
import { readdirSync } from 'fs';
|
|
39
|
-
import { dirname, basename, sep, posix } from 'path';
|
|
40
|
-
|
|
41
|
-
const FILE_THRESHOLD_DEFAULT = 4;
|
|
42
|
-
|
|
43
|
-
/** Exact file names allowed to stay at the root of a module directory. */
|
|
44
|
-
const ROOT_ALLOWED_BASENAMES = new Set([
|
|
45
|
-
'index.tsx',
|
|
46
|
-
'index.ts',
|
|
47
|
-
'types.ts',
|
|
48
|
-
'constants.ts',
|
|
49
|
-
'schema.ts',
|
|
50
|
-
'columns.ts',
|
|
51
|
-
]);
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* File name patterns that are allowed at root.
|
|
55
|
-
* - *Types.ts: type definition files (same purpose as types.ts)
|
|
56
|
-
* - Files containing a single const enum are required by
|
|
57
|
-
* enum-file-isolation to be standalone — don't force them into utils/.
|
|
58
|
-
* We detect these by common naming patterns for enum files.
|
|
59
|
-
*/
|
|
60
|
-
const ROOT_ALLOWED_PATTERNS = [
|
|
61
|
-
/^[a-zA-Z]+Types\.ts$/, // alertTypes.ts, buttonTypes.ts, etc.
|
|
62
|
-
];
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Directory names where PascalCase .tsx files are the primary content
|
|
66
|
-
* and should NOT be flagged as "move to components/".
|
|
67
|
-
* Hooks, utils, and data files are still flagged in these directories.
|
|
68
|
-
*/
|
|
69
|
-
const COMPONENT_EXEMPT_DIRS = new Set([
|
|
70
|
-
'sections',
|
|
71
|
-
'forms',
|
|
72
|
-
'Forms',
|
|
73
|
-
]);
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Directory names that are collections of standalone files by design.
|
|
77
|
-
* Files in these directories should not be reorganized into subdirs.
|
|
78
|
-
* Example: shared/ contains type files and enums for multiple consumers.
|
|
79
|
-
*/
|
|
80
|
-
const FLAT_EXEMPT_DIRS = new Set([
|
|
81
|
-
// Common (shared with SyncfusionThemeStudio)
|
|
82
|
-
'shared',
|
|
83
|
-
'presets',
|
|
84
|
-
'defaults',
|
|
85
|
-
'injectors',
|
|
86
|
-
'types',
|
|
87
|
-
'actions',
|
|
88
|
-
'icons',
|
|
89
|
-
'errors',
|
|
90
|
-
'interceptors',
|
|
91
|
-
'events',
|
|
92
|
-
'routes',
|
|
93
|
-
'utils',
|
|
94
|
-
'components',
|
|
95
|
-
'form-fields',
|
|
96
|
-
// BaseClient-specific
|
|
97
|
-
'enums',
|
|
98
|
-
'testIds',
|
|
99
|
-
'constants',
|
|
100
|
-
'slices',
|
|
101
|
-
'schemas',
|
|
102
|
-
'locales',
|
|
103
|
-
'mutators',
|
|
104
|
-
'customHooks',
|
|
105
|
-
'swagger',
|
|
106
|
-
'models',
|
|
107
|
-
'handlers',
|
|
108
|
-
'menuHandlers',
|
|
109
|
-
'userHandlers',
|
|
110
|
-
]);
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* Count non-test, non-directory source files in a directory (shallow).
|
|
114
|
-
* Returns only .ts/.tsx files that are not tests or declarations.
|
|
115
|
-
*/
|
|
116
|
-
function countSourceFiles(dirPath) {
|
|
117
|
-
try {
|
|
118
|
-
const entries = readdirSync(dirPath, { withFileTypes: true });
|
|
119
|
-
return entries.filter((e) => {
|
|
120
|
-
if (!e.isFile()) return false;
|
|
121
|
-
const name = e.name;
|
|
122
|
-
if (!name.endsWith('.ts') && !name.endsWith('.tsx')) return false;
|
|
123
|
-
if (name.endsWith('.test.ts') || name.endsWith('.test.tsx')) return false;
|
|
124
|
-
if (name.endsWith('.d.ts')) return false;
|
|
125
|
-
return true;
|
|
126
|
-
}).length;
|
|
127
|
-
} catch {
|
|
128
|
-
return 0;
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
/**
|
|
133
|
-
* Check whether the file is inside a non-root subdirectory of its
|
|
134
|
-
* module directory (e.g. hooks/, components/, filters/, columnMenu/).
|
|
135
|
-
* If so, it's already organized and should be exempt.
|
|
136
|
-
*/
|
|
137
|
-
function isInsideSubdirectory(filePath, moduleRoot) {
|
|
138
|
-
const rel = filePath
|
|
139
|
-
.split(sep)
|
|
140
|
-
.join(posix.sep)
|
|
141
|
-
.slice(moduleRoot.split(sep).join(posix.sep).length + 1);
|
|
142
|
-
// rel looks like "hooks/useX.ts" or "index.tsx"
|
|
143
|
-
return rel.includes('/');
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
/**
|
|
147
|
-
* Determine the "module root" — the nearest ancestor directory that
|
|
148
|
-
* contains an index.ts or index.tsx file. This is the component or
|
|
149
|
-
* page directory boundary.
|
|
150
|
-
*/
|
|
151
|
-
function findModuleRoot(filePath) {
|
|
152
|
-
let dir = dirname(filePath);
|
|
153
|
-
const maxDepth = 10;
|
|
154
|
-
for (let i = 0; i < maxDepth; i++) {
|
|
155
|
-
try {
|
|
156
|
-
const entries = readdirSync(dir);
|
|
157
|
-
if (entries.includes('index.ts') || entries.includes('index.tsx'))
|
|
158
|
-
return dir;
|
|
159
|
-
} catch {
|
|
160
|
-
return null;
|
|
161
|
-
}
|
|
162
|
-
const parent = dirname(dir);
|
|
163
|
-
if (parent === dir) return null;
|
|
164
|
-
dir = parent;
|
|
165
|
-
}
|
|
166
|
-
return null;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
const enforceModuleStructureRule = {
|
|
170
|
-
meta: {
|
|
171
|
-
type: 'suggestion',
|
|
172
|
-
docs: {
|
|
173
|
-
description:
|
|
174
|
-
'Enforce standard module structure: hooks in hooks/, sub-components in components/, ' +
|
|
175
|
-
'utils in utils/, data in data/. Only applies when directory has 4+ source files.',
|
|
176
|
-
category: 'Best Practices',
|
|
177
|
-
recommended: true,
|
|
178
|
-
},
|
|
179
|
-
schema: [
|
|
180
|
-
{
|
|
181
|
-
type: 'object',
|
|
182
|
-
properties: {
|
|
183
|
-
fileThreshold: {
|
|
184
|
-
type: 'integer',
|
|
185
|
-
minimum: 2,
|
|
186
|
-
description:
|
|
187
|
-
'Minimum number of non-test source files before structure is enforced',
|
|
188
|
-
},
|
|
189
|
-
},
|
|
190
|
-
additionalProperties: false,
|
|
191
|
-
},
|
|
192
|
-
],
|
|
193
|
-
messages: {
|
|
194
|
-
hookAtRoot:
|
|
195
|
-
'Hook "{{fileName}}" should be in a hooks/ subdirectory. ' +
|
|
196
|
-
'Move to {{suggestedPath}}.',
|
|
197
|
-
componentAtRoot:
|
|
198
|
-
'Sub-component "{{fileName}}" should be in a components/ subdirectory. ' +
|
|
199
|
-
'Move to {{suggestedPath}}.',
|
|
200
|
-
utilAtRoot:
|
|
201
|
-
'Utility file "{{fileName}}" should be in a utils/ subdirectory. ' +
|
|
202
|
-
'Move to {{suggestedPath}}.',
|
|
203
|
-
dataInSections:
|
|
204
|
-
'Data file "{{fileName}}" should be in a data/ subdirectory, ' +
|
|
205
|
-
'not mixed with section components. Move to {{suggestedPath}}.',
|
|
206
|
-
},
|
|
207
|
-
},
|
|
208
|
-
|
|
209
|
-
create(context) {
|
|
210
|
-
const options = context.options[0] || {};
|
|
211
|
-
const fileThreshold = options.fileThreshold ?? FILE_THRESHOLD_DEFAULT;
|
|
212
|
-
|
|
213
|
-
return {
|
|
214
|
-
Program() {
|
|
215
|
-
const filePath = context.filename || context.getFilename();
|
|
216
|
-
if (!filePath) return;
|
|
217
|
-
|
|
218
|
-
// Only check .ts/.tsx files
|
|
219
|
-
if (!filePath.endsWith('.ts') && !filePath.endsWith('.tsx')) return;
|
|
220
|
-
if (filePath.endsWith('.d.ts')) return;
|
|
221
|
-
|
|
222
|
-
const fileName = basename(filePath);
|
|
223
|
-
|
|
224
|
-
// Skip test files — they follow their source files
|
|
225
|
-
if (fileName.endsWith('.test.ts') || fileName.endsWith('.test.tsx'))
|
|
226
|
-
return;
|
|
227
|
-
|
|
228
|
-
// Skip root-allowed files (exact matches and patterns)
|
|
229
|
-
if (ROOT_ALLOWED_BASENAMES.has(fileName)) return;
|
|
230
|
-
if (ROOT_ALLOWED_PATTERNS.some((p) => p.test(fileName))) return;
|
|
231
|
-
|
|
232
|
-
const dir = dirname(filePath);
|
|
233
|
-
const dirName = basename(dir);
|
|
234
|
-
|
|
235
|
-
// Skip files in flat-exempt directories (shared/, presets/, etc.)
|
|
236
|
-
if (FLAT_EXEMPT_DIRS.has(dirName)) return;
|
|
237
|
-
|
|
238
|
-
// Find the module root (directory with index.ts/tsx)
|
|
239
|
-
const moduleRoot = findModuleRoot(filePath);
|
|
240
|
-
if (!moduleRoot) return;
|
|
241
|
-
|
|
242
|
-
// If the file is already inside a subdirectory of the module root,
|
|
243
|
-
// it's organized — skip
|
|
244
|
-
if (isInsideSubdirectory(filePath, moduleRoot)) return;
|
|
245
|
-
|
|
246
|
-
// Count source files in the module root to check threshold
|
|
247
|
-
const sourceFileCount = countSourceFiles(moduleRoot);
|
|
248
|
-
if (sourceFileCount < fileThreshold) return;
|
|
249
|
-
|
|
250
|
-
// --- Classify the file and report ---
|
|
251
|
-
|
|
252
|
-
const isHook =
|
|
253
|
-
/^use[A-Z]/.test(fileName) &&
|
|
254
|
-
(fileName.endsWith('.ts') || fileName.endsWith('.tsx'));
|
|
255
|
-
|
|
256
|
-
const isComponent =
|
|
257
|
-
fileName.endsWith('.tsx') && /^[A-Z]/.test(fileName);
|
|
258
|
-
|
|
259
|
-
const isDataFile =
|
|
260
|
-
/[Dd]ata\.tsx?$/.test(fileName) ||
|
|
261
|
-
/\.data\.tsx?$/.test(fileName);
|
|
262
|
-
|
|
263
|
-
// Data files in sections/ directory → suggest data/ subdir
|
|
264
|
-
if (isDataFile && dirName === 'sections') {
|
|
265
|
-
context.report({
|
|
266
|
-
loc: { line: 1, column: 0 },
|
|
267
|
-
messageId: 'dataInSections',
|
|
268
|
-
data: {
|
|
269
|
-
fileName,
|
|
270
|
-
suggestedPath: `data/${fileName}`,
|
|
271
|
-
},
|
|
272
|
-
});
|
|
273
|
-
return;
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
if (isHook) {
|
|
277
|
-
context.report({
|
|
278
|
-
loc: { line: 1, column: 0 },
|
|
279
|
-
messageId: 'hookAtRoot',
|
|
280
|
-
data: {
|
|
281
|
-
fileName,
|
|
282
|
-
suggestedPath: `hooks/${fileName}`,
|
|
283
|
-
},
|
|
284
|
-
});
|
|
285
|
-
return;
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
// In sections/ and forms/ dirs, .tsx files are the primary content
|
|
289
|
-
// and should NOT be flagged as needing components/ subdir
|
|
290
|
-
if (isComponent && !COMPONENT_EXEMPT_DIRS.has(dirName)) {
|
|
291
|
-
context.report({
|
|
292
|
-
loc: { line: 1, column: 0 },
|
|
293
|
-
messageId: 'componentAtRoot',
|
|
294
|
-
data: {
|
|
295
|
-
fileName,
|
|
296
|
-
suggestedPath: `components/${fileName}`,
|
|
297
|
-
},
|
|
298
|
-
});
|
|
299
|
-
return;
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
// Remaining .ts files that aren't root-allowed → utils
|
|
303
|
-
// Skip .tsx files in exempt dirs (they're section/form components)
|
|
304
|
-
if (fileName.endsWith('.ts') && !isDataFile) {
|
|
305
|
-
context.report({
|
|
306
|
-
loc: { line: 1, column: 0 },
|
|
307
|
-
messageId: 'utilAtRoot',
|
|
308
|
-
data: {
|
|
309
|
-
fileName,
|
|
310
|
-
suggestedPath: `utils/${fileName}`,
|
|
311
|
-
},
|
|
312
|
-
});
|
|
313
|
-
}
|
|
314
|
-
},
|
|
315
|
-
};
|
|
316
|
-
},
|
|
317
|
-
};
|
|
318
|
-
|
|
319
|
-
export default {
|
|
320
|
-
rules: {
|
|
321
|
-
'enforce-module-structure': enforceModuleStructureRule,
|
|
322
|
-
},
|
|
323
|
-
};
|
|
@@ -1,288 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Custom ESLint Plugin: Enforce Route Page Preloading
|
|
3
|
-
*
|
|
4
|
-
* Runs on a preloader file (e.g., src/config/routePreloader.ts) and ensures
|
|
5
|
-
* every Expo Router route page in the configured directory has a matching
|
|
6
|
-
* dynamic import() expression. This prevents developers from adding new
|
|
7
|
-
* route pages without preloading them, which would cause slower navigation.
|
|
8
|
-
*
|
|
9
|
-
* Reports:
|
|
10
|
-
* - missingPreload: route page file has no matching import() in the preloader
|
|
11
|
-
* - stalePreload: import path does not match any route page file
|
|
12
|
-
* - noImportsFound: preloader file has no dynamic import() expressions at all
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
import fs from 'fs';
|
|
16
|
-
import path from 'path';
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Recursively discover route page files in the given directory.
|
|
20
|
-
*
|
|
21
|
-
* A file qualifies as a route page if:
|
|
22
|
-
* - It is a .tsx file
|
|
23
|
-
* - It is NOT a _layout.tsx file
|
|
24
|
-
* - It is NOT inside an excluded directory (e.g., __tests__)
|
|
25
|
-
* - If it lives in a directory that already has an index.tsx, only index.tsx
|
|
26
|
-
* itself counts (sibling .tsx files are sub-components, not route entries)
|
|
27
|
-
* - Direct .tsx files whose name does NOT start with _ are also route pages
|
|
28
|
-
*/
|
|
29
|
-
function discoverRoutePages(baseDir, options) {
|
|
30
|
-
const {
|
|
31
|
-
excludeFiles = ['_layout.tsx'],
|
|
32
|
-
excludeDirs = ['__tests__'],
|
|
33
|
-
rootIndexIsRedirect = true,
|
|
34
|
-
} = options;
|
|
35
|
-
|
|
36
|
-
const routePages = [];
|
|
37
|
-
|
|
38
|
-
function walk(dir, relativePrefix) {
|
|
39
|
-
let entries;
|
|
40
|
-
try {
|
|
41
|
-
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
42
|
-
} catch {
|
|
43
|
-
return;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
for (const entry of entries) {
|
|
47
|
-
const fullPath = path.join(dir, entry.name);
|
|
48
|
-
const relativePath = path.join(relativePrefix, entry.name);
|
|
49
|
-
|
|
50
|
-
if (entry.isDirectory()) {
|
|
51
|
-
// Skip excluded directories
|
|
52
|
-
if (excludeDirs.includes(entry.name)) continue;
|
|
53
|
-
|
|
54
|
-
walk(fullPath, relativePath);
|
|
55
|
-
continue;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// Only consider .tsx files
|
|
59
|
-
if (!entry.name.endsWith('.tsx')) continue;
|
|
60
|
-
|
|
61
|
-
// Skip excluded file names
|
|
62
|
-
if (excludeFiles.includes(entry.name)) continue;
|
|
63
|
-
|
|
64
|
-
// Skip files starting with _ (e.g., _layout.tsx already excluded above,
|
|
65
|
-
// but also any other underscore-prefixed files)
|
|
66
|
-
if (entry.name.startsWith('_')) continue;
|
|
67
|
-
|
|
68
|
-
// Skip root index if configured as redirect
|
|
69
|
-
const normalizedRelative = relativePath.replace(/\\/g, '/');
|
|
70
|
-
if (rootIndexIsRedirect && normalizedRelative === 'index.tsx') continue;
|
|
71
|
-
|
|
72
|
-
// Strip .tsx extension for the normalized path
|
|
73
|
-
const withoutExt = normalizedRelative.replace(/\.tsx$/, '');
|
|
74
|
-
routePages.push(withoutExt);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
walk(baseDir, '');
|
|
79
|
-
return routePages;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Recursively collect all ImportExpression source paths within an AST subtree.
|
|
84
|
-
*/
|
|
85
|
-
function collectImportPaths(node) {
|
|
86
|
-
const paths = [];
|
|
87
|
-
|
|
88
|
-
function walk(n) {
|
|
89
|
-
if (!n || typeof n !== 'object') return;
|
|
90
|
-
|
|
91
|
-
if (n.type === 'ImportExpression' && n.source?.type === 'Literal')
|
|
92
|
-
paths.push({ value: n.source.value, node: n });
|
|
93
|
-
|
|
94
|
-
for (const key of Object.keys(n)) {
|
|
95
|
-
if (key === 'parent') continue;
|
|
96
|
-
const child = n[key];
|
|
97
|
-
if (Array.isArray(child))
|
|
98
|
-
child.forEach(walk);
|
|
99
|
-
else if (child?.type)
|
|
100
|
-
walk(child);
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
walk(node);
|
|
105
|
-
return paths;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Resolve a relative import path to an absolute normalized form,
|
|
110
|
-
* then strip the project root prefix to get a project-relative path.
|
|
111
|
-
*/
|
|
112
|
-
function resolveImportToProjectRelative(importPath, fileDir, projectRoot) {
|
|
113
|
-
// Resolve relative to the file being linted
|
|
114
|
-
const absoluteResolved = path.resolve(fileDir, importPath);
|
|
115
|
-
// Make it relative to the project root
|
|
116
|
-
const projectRelative = path.relative(projectRoot, absoluteResolved);
|
|
117
|
-
// Normalize to forward slashes
|
|
118
|
-
return projectRelative.replace(/\\/g, '/');
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
const enforceRoutePreloadRule = {
|
|
122
|
-
meta: {
|
|
123
|
-
type: 'problem',
|
|
124
|
-
docs: {
|
|
125
|
-
description:
|
|
126
|
-
'Ensure every route page in the configured directory has a matching dynamic import() in the preloader file',
|
|
127
|
-
category: 'Best Practices',
|
|
128
|
-
recommended: true,
|
|
129
|
-
},
|
|
130
|
-
schema: [
|
|
131
|
-
{
|
|
132
|
-
type: 'object',
|
|
133
|
-
properties: {
|
|
134
|
-
routeDir: {
|
|
135
|
-
type: 'string',
|
|
136
|
-
description:
|
|
137
|
-
'Directory to scan for route pages, relative to project root',
|
|
138
|
-
default: 'app/(protected)',
|
|
139
|
-
},
|
|
140
|
-
excludeFiles: {
|
|
141
|
-
type: 'array',
|
|
142
|
-
items: { type: 'string' },
|
|
143
|
-
description: 'Filename patterns to skip',
|
|
144
|
-
default: ['_layout.tsx'],
|
|
145
|
-
},
|
|
146
|
-
excludeDirs: {
|
|
147
|
-
type: 'array',
|
|
148
|
-
items: { type: 'string' },
|
|
149
|
-
description: 'Directory names to skip',
|
|
150
|
-
default: ['__tests__'],
|
|
151
|
-
},
|
|
152
|
-
rootIndexIsRedirect: {
|
|
153
|
-
type: 'boolean',
|
|
154
|
-
description:
|
|
155
|
-
'Whether the root index.tsx in routeDir is just a redirect and should be skipped',
|
|
156
|
-
default: true,
|
|
157
|
-
},
|
|
158
|
-
},
|
|
159
|
-
additionalProperties: false,
|
|
160
|
-
},
|
|
161
|
-
],
|
|
162
|
-
messages: {
|
|
163
|
-
missingPreload:
|
|
164
|
-
'Route page "{{routeFile}}" is not preloaded. Add: import(\'{{importPath}}\').catch(() => undefined);',
|
|
165
|
-
stalePreload:
|
|
166
|
-
'Preload import "{{importPath}}" does not match any route page in {{routeDir}}/. Remove it or add the corresponding route page.',
|
|
167
|
-
noImportsFound:
|
|
168
|
-
'Preloader file has no dynamic import() expressions. Add imports for all route pages in {{routeDir}}/.',
|
|
169
|
-
},
|
|
170
|
-
},
|
|
171
|
-
|
|
172
|
-
create(context) {
|
|
173
|
-
const options = context.options[0] ?? {};
|
|
174
|
-
const routeDir = options.routeDir ?? 'app/(protected)';
|
|
175
|
-
const excludeFiles = options.excludeFiles ?? ['_layout.tsx'];
|
|
176
|
-
const excludeDirs = options.excludeDirs ?? ['__tests__'];
|
|
177
|
-
const rootIndexIsRedirect = options.rootIndexIsRedirect ?? true;
|
|
178
|
-
|
|
179
|
-
// Determine project root (directory containing eslint.config.mjs)
|
|
180
|
-
const cwd = context.cwd ?? context.getCwd?.() ?? process.cwd();
|
|
181
|
-
const projectRoot = cwd;
|
|
182
|
-
|
|
183
|
-
// Determine the directory of the file being linted
|
|
184
|
-
const filename = context.filename ?? context.getFilename?.();
|
|
185
|
-
const fileDir = path.dirname(filename);
|
|
186
|
-
|
|
187
|
-
// Discover all route page files
|
|
188
|
-
const routeDirAbsolute = path.resolve(projectRoot, routeDir);
|
|
189
|
-
const routePages = discoverRoutePages(routeDirAbsolute, {
|
|
190
|
-
excludeFiles,
|
|
191
|
-
excludeDirs,
|
|
192
|
-
rootIndexIsRedirect,
|
|
193
|
-
});
|
|
194
|
-
|
|
195
|
-
// Build the set of expected normalized paths: "app/(protected)/tenants/index"
|
|
196
|
-
const expectedRouteSet = new Set(
|
|
197
|
-
routePages.map((rp) => `${routeDir}/${rp}`.replace(/\\/g, '/')),
|
|
198
|
-
);
|
|
199
|
-
|
|
200
|
-
// Collected during traversal
|
|
201
|
-
const importExpressions = []; // { resolvedPath: string, rawPath: string, node: ASTNode }
|
|
202
|
-
|
|
203
|
-
return {
|
|
204
|
-
// Collect all dynamic import() expressions in the file
|
|
205
|
-
ImportExpression(node) {
|
|
206
|
-
if (node.source?.type !== 'Literal') return;
|
|
207
|
-
const rawPath = node.source.value;
|
|
208
|
-
const resolvedPath = resolveImportToProjectRelative(
|
|
209
|
-
rawPath,
|
|
210
|
-
fileDir,
|
|
211
|
-
projectRoot,
|
|
212
|
-
);
|
|
213
|
-
importExpressions.push({ resolvedPath, rawPath, node });
|
|
214
|
-
},
|
|
215
|
-
|
|
216
|
-
'Program:exit'(programNode) {
|
|
217
|
-
// If no route pages were discovered, nothing to check
|
|
218
|
-
if (expectedRouteSet.size === 0) return;
|
|
219
|
-
|
|
220
|
-
// If preloader has zero imports, report a single error
|
|
221
|
-
if (importExpressions.length === 0) {
|
|
222
|
-
context.report({
|
|
223
|
-
node: programNode,
|
|
224
|
-
messageId: 'noImportsFound',
|
|
225
|
-
data: { routeDir },
|
|
226
|
-
});
|
|
227
|
-
return;
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
// Build set of resolved import paths (normalized)
|
|
231
|
-
const importedPaths = new Set(
|
|
232
|
-
importExpressions.map((i) => i.resolvedPath),
|
|
233
|
-
);
|
|
234
|
-
|
|
235
|
-
// Check for missing preloads: route pages not covered by any import
|
|
236
|
-
for (const routePath of expectedRouteSet) {
|
|
237
|
-
if (!importedPaths.has(routePath)) {
|
|
238
|
-
// Compute the relative import path from the preloader file to the route
|
|
239
|
-
const routeAbsolute = path.resolve(projectRoot, `${routePath}.tsx`);
|
|
240
|
-
let relativePath = path.relative(fileDir, routeAbsolute);
|
|
241
|
-
// Normalize to forward slashes
|
|
242
|
-
relativePath = relativePath.replace(/\\/g, '/');
|
|
243
|
-
// Ensure it starts with ./
|
|
244
|
-
if (!relativePath.startsWith('.'))
|
|
245
|
-
relativePath = `./${relativePath}`;
|
|
246
|
-
// Strip .tsx extension
|
|
247
|
-
relativePath = relativePath.replace(/\.tsx$/, '');
|
|
248
|
-
|
|
249
|
-
context.report({
|
|
250
|
-
node: programNode,
|
|
251
|
-
messageId: 'missingPreload',
|
|
252
|
-
data: {
|
|
253
|
-
routeFile: routePath,
|
|
254
|
-
importPath: relativePath,
|
|
255
|
-
},
|
|
256
|
-
});
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
// Check for stale preloads: imports that don't match any route page
|
|
261
|
-
for (const { resolvedPath, rawPath, node } of importExpressions) {
|
|
262
|
-
// Only flag imports that point into the routeDir
|
|
263
|
-
const normalizedRouteDir = routeDir.replace(/\\/g, '/');
|
|
264
|
-
if (!resolvedPath.startsWith(normalizedRouteDir)) continue;
|
|
265
|
-
|
|
266
|
-
if (!expectedRouteSet.has(resolvedPath))
|
|
267
|
-
context.report({
|
|
268
|
-
node,
|
|
269
|
-
messageId: 'stalePreload',
|
|
270
|
-
data: {
|
|
271
|
-
importPath: rawPath,
|
|
272
|
-
routeDir,
|
|
273
|
-
},
|
|
274
|
-
});
|
|
275
|
-
}
|
|
276
|
-
},
|
|
277
|
-
};
|
|
278
|
-
},
|
|
279
|
-
};
|
|
280
|
-
|
|
281
|
-
/**
|
|
282
|
-
* Export the plugin
|
|
283
|
-
*/
|
|
284
|
-
export default {
|
|
285
|
-
rules: {
|
|
286
|
-
'enforce-route-preload': enforceRoutePreloadRule,
|
|
287
|
-
},
|
|
288
|
-
};
|
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Custom ESLint Plugin: Enforce Test Colocation
|
|
3
|
-
*
|
|
4
|
-
* Flags test files that live in __tests__/ directories instead of being
|
|
5
|
-
* co-located next to the source file they test.
|
|
6
|
-
*
|
|
7
|
-
* Co-located tests are easier to find, easier to maintain, and make it
|
|
8
|
-
* obvious which files have test coverage at a glance.
|
|
9
|
-
*
|
|
10
|
-
* Examples:
|
|
11
|
-
* BAD: DataGrid/__tests__/useGridCallbacks.test.ts
|
|
12
|
-
* GOOD: DataGrid/hooks/useGridCallbacks.test.ts (next to source)
|
|
13
|
-
*
|
|
14
|
-
* BAD: lib/api/errors/__tests__/apiError.test.ts
|
|
15
|
-
* GOOD: lib/api/errors/apiError.test.ts (next to source)
|
|
16
|
-
*
|
|
17
|
-
* OK: hooks/useGridCallbacks.test.ts (already co-located)
|
|
18
|
-
*/
|
|
19
|
-
|
|
20
|
-
import { dirname, basename, sep, posix } from 'path';
|
|
21
|
-
|
|
22
|
-
const enforceTestColocationRule = {
|
|
23
|
-
meta: {
|
|
24
|
-
type: 'suggestion',
|
|
25
|
-
docs: {
|
|
26
|
-
description:
|
|
27
|
-
'Test files should be co-located next to their source file, ' +
|
|
28
|
-
'not placed in a __tests__/ directory.',
|
|
29
|
-
category: 'Best Practices',
|
|
30
|
-
recommended: true,
|
|
31
|
-
},
|
|
32
|
-
schema: [],
|
|
33
|
-
messages: {
|
|
34
|
-
testInTestsDir:
|
|
35
|
-
'Test file "{{fileName}}" is inside a __tests__/ directory. ' +
|
|
36
|
-
'Move it next to its source file for co-location. ' +
|
|
37
|
-
'Suggested: {{suggestedPath}}',
|
|
38
|
-
},
|
|
39
|
-
},
|
|
40
|
-
|
|
41
|
-
create(context) {
|
|
42
|
-
return {
|
|
43
|
-
Program() {
|
|
44
|
-
const filePath = context.filename || context.getFilename();
|
|
45
|
-
if (!filePath) return;
|
|
46
|
-
|
|
47
|
-
const fileName = basename(filePath);
|
|
48
|
-
|
|
49
|
-
// Only check test files
|
|
50
|
-
if (!fileName.endsWith('.test.ts') && !fileName.endsWith('.test.tsx'))
|
|
51
|
-
return;
|
|
52
|
-
|
|
53
|
-
// Normalize path separators for consistent checking
|
|
54
|
-
const normalized = filePath.split(sep).join(posix.sep);
|
|
55
|
-
|
|
56
|
-
// Check if the file is inside a __tests__/ directory
|
|
57
|
-
if (!normalized.includes('/__tests__/')) return;
|
|
58
|
-
|
|
59
|
-
// Build the suggested co-located path:
|
|
60
|
-
// __tests__/foo.test.ts → ../foo.test.ts (next to source)
|
|
61
|
-
const dir = dirname(filePath);
|
|
62
|
-
const parentDir = dirname(dir);
|
|
63
|
-
const parentDirName = basename(parentDir);
|
|
64
|
-
|
|
65
|
-
context.report({
|
|
66
|
-
loc: { line: 1, column: 0 },
|
|
67
|
-
messageId: 'testInTestsDir',
|
|
68
|
-
data: {
|
|
69
|
-
fileName,
|
|
70
|
-
suggestedPath: `${parentDirName}/${fileName}`,
|
|
71
|
-
},
|
|
72
|
-
});
|
|
73
|
-
},
|
|
74
|
-
};
|
|
75
|
-
},
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
export default {
|
|
79
|
-
rules: {
|
|
80
|
-
'enforce-test-colocation': enforceTestColocationRule,
|
|
81
|
-
},
|
|
82
|
-
};
|