@hkdigital/lib-core 0.5.65 → 0.5.66
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/README.md +37 -0
- package/package.json +1 -1
- package/scripts/validate-imports.mjs +28 -18
package/README.md
CHANGED
|
@@ -277,6 +277,43 @@ pnpm run upgrade:all # Update all packages
|
|
|
277
277
|
pnpm run publish:npm # Version bump and publish to npm
|
|
278
278
|
```
|
|
279
279
|
|
|
280
|
+
### Import Validation
|
|
281
|
+
|
|
282
|
+
The library includes a validation script to enforce consistent import
|
|
283
|
+
patterns. Run it in your project:
|
|
284
|
+
|
|
285
|
+
```bash
|
|
286
|
+
node node_modules/@hkdigital/lib-core/scripts/validate-imports.mjs
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
**Validation rules (enforced for `src/lib/` files only):**
|
|
290
|
+
|
|
291
|
+
1. **Cross-domain imports** - Use `$lib/` instead of `../../../`
|
|
292
|
+
2. **Parent index.js imports** - Use `$lib/` or import specific files
|
|
293
|
+
3. **Non-standard extensions** - Include full extension (e.g.,
|
|
294
|
+
`.svelte.js`)
|
|
295
|
+
4. **Directory imports** - Write explicitly (e.g., `./path/index.js`)
|
|
296
|
+
5. **File existence** - All import paths must resolve to existing files
|
|
297
|
+
|
|
298
|
+
**Routes are exempt from strict rules:**
|
|
299
|
+
|
|
300
|
+
Files in `src/routes/` can use relative imports freely, including parent
|
|
301
|
+
navigation and index.js imports. Since SvelteKit doesn't provide a
|
|
302
|
+
`$routes` alias, relative imports are the standard pattern for route
|
|
303
|
+
files.
|
|
304
|
+
|
|
305
|
+
**Example output:**
|
|
306
|
+
|
|
307
|
+
```
|
|
308
|
+
src/lib/ui/components/Button.svelte:7
|
|
309
|
+
from '$lib/ui/primitives/buttons'
|
|
310
|
+
=> from '$lib/ui/primitives/buttons/index.js'
|
|
311
|
+
|
|
312
|
+
src/routes/explorer/[...path]/+page.svelte:4
|
|
313
|
+
from '../components/index.js'
|
|
314
|
+
✅ Allowed in routes
|
|
315
|
+
```
|
|
316
|
+
|
|
280
317
|
### Import Patterns and Export Structure
|
|
281
318
|
|
|
282
319
|
**Public exports use domain-specific files matching folder names:**
|
package/package.json
CHANGED
|
@@ -94,6 +94,8 @@ async function validateFile(filePath) {
|
|
|
94
94
|
const relativePath = relative(PROJECT_ROOT, filePath);
|
|
95
95
|
const isTestFile = filePath.endsWith('.test.js') ||
|
|
96
96
|
filePath.endsWith('.spec.js');
|
|
97
|
+
const isInLib = filePath.includes('/src/lib/');
|
|
98
|
+
const isInRoutes = filePath.includes('/src/routes/');
|
|
97
99
|
|
|
98
100
|
// Check each line for import statements
|
|
99
101
|
const lines = content.split('\n');
|
|
@@ -125,17 +127,20 @@ async function validateFile(filePath) {
|
|
|
125
127
|
}
|
|
126
128
|
|
|
127
129
|
// Check 1: Cross-domain relative imports (3+ levels up)
|
|
128
|
-
|
|
130
|
+
// Only enforce for lib files
|
|
131
|
+
if (isInLib && importPath.match(/^\.\.\/\.\.\/\.\.\//)) {
|
|
129
132
|
errors.push(
|
|
130
|
-
`${relativePath}:${lineNum}
|
|
131
|
-
`
|
|
133
|
+
`${relativePath}:${lineNum}\n` +
|
|
134
|
+
` from '${importPath}'\n` +
|
|
135
|
+
` => Use $lib/ for cross-domain imports`
|
|
132
136
|
);
|
|
133
137
|
continue;
|
|
134
138
|
}
|
|
135
139
|
|
|
136
|
-
// Check 2: Local index.js imports (skip for test files)
|
|
140
|
+
// Check 2: Local index.js imports (skip for test files and routes)
|
|
137
141
|
// Allow ./index.js and ./subfolder/index.js but flag parent navigation
|
|
138
|
-
|
|
142
|
+
// Only enforce for lib files
|
|
143
|
+
if (isInLib && !isTestFile && importPath.match(/\/index\.js$/)) {
|
|
139
144
|
// Allow same-directory and child directory imports
|
|
140
145
|
// Examples: ./index.js, ./subfolder/index.js, ./(meta)/index.js
|
|
141
146
|
// Flag parent navigation: ../index.js, ../../index.js
|
|
@@ -143,8 +148,9 @@ async function validateFile(filePath) {
|
|
|
143
148
|
|
|
144
149
|
if (isParentNavigation) {
|
|
145
150
|
errors.push(
|
|
146
|
-
`${relativePath}:${lineNum}
|
|
147
|
-
`
|
|
151
|
+
`${relativePath}:${lineNum}\n` +
|
|
152
|
+
` from '${importPath}'\n` +
|
|
153
|
+
` => Use $lib/ or import specific file instead`
|
|
148
154
|
);
|
|
149
155
|
continue;
|
|
150
156
|
}
|
|
@@ -254,8 +260,9 @@ async function validateFile(filePath) {
|
|
|
254
260
|
if (stats.isFile()) {
|
|
255
261
|
const correctImport = baseImportPath + ext;
|
|
256
262
|
errors.push(
|
|
257
|
-
`${relativePath}:${lineNum}
|
|
258
|
-
`
|
|
263
|
+
`${relativePath}:${lineNum}\n` +
|
|
264
|
+
` from '${importPath}'\n` +
|
|
265
|
+
` => from '${correctImport}'`
|
|
259
266
|
);
|
|
260
267
|
foundNonStandard = true;
|
|
261
268
|
break;
|
|
@@ -286,9 +293,10 @@ async function validateFile(filePath) {
|
|
|
286
293
|
|
|
287
294
|
if (wouldBeParentNavigation) {
|
|
288
295
|
errors.push(
|
|
289
|
-
`${relativePath}:${lineNum}
|
|
290
|
-
`
|
|
291
|
-
`
|
|
296
|
+
`${relativePath}:${lineNum}\n` +
|
|
297
|
+
` from '${importPath}'\n` +
|
|
298
|
+
` => Create export file like '${importPath}.js' or ` +
|
|
299
|
+
`import specific file`
|
|
292
300
|
);
|
|
293
301
|
continue;
|
|
294
302
|
}
|
|
@@ -296,8 +304,9 @@ async function validateFile(filePath) {
|
|
|
296
304
|
const suggestion = importPath.endsWith('/') ?
|
|
297
305
|
`${importPath}index.js` : `${importPath}/index.js`;
|
|
298
306
|
errors.push(
|
|
299
|
-
`${relativePath}:${lineNum}
|
|
300
|
-
`
|
|
307
|
+
`${relativePath}:${lineNum}\n` +
|
|
308
|
+
` from '${importPath}'\n` +
|
|
309
|
+
` => from '${suggestion}'`
|
|
301
310
|
);
|
|
302
311
|
continue;
|
|
303
312
|
}
|
|
@@ -336,8 +345,9 @@ async function validateFile(filePath) {
|
|
|
336
345
|
|
|
337
346
|
if (!fileExists) {
|
|
338
347
|
errors.push(
|
|
339
|
-
`${relativePath}:${lineNum}
|
|
340
|
-
`'${importPath}'`
|
|
348
|
+
`${relativePath}:${lineNum}\n` +
|
|
349
|
+
` from '${importPath}'\n` +
|
|
350
|
+
` => Import path does not exist`
|
|
341
351
|
);
|
|
342
352
|
}
|
|
343
353
|
}
|
|
@@ -361,8 +371,8 @@ async function main() {
|
|
|
361
371
|
|
|
362
372
|
if (allErrors.length > 0) {
|
|
363
373
|
console.error('❌ Found import path violations:\n');
|
|
364
|
-
allErrors.forEach(error => console.error(
|
|
365
|
-
console.error(
|
|
374
|
+
allErrors.forEach(error => console.error(`${error}\n`));
|
|
375
|
+
console.error(`${allErrors.length} error(s) found.`);
|
|
366
376
|
process.exit(1);
|
|
367
377
|
}
|
|
368
378
|
|