@hkdigital/lib-core 0.5.79 → 0.5.81
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/package.json +1 -1
- package/scripts/validate-imports.mjs +80 -28
package/package.json
CHANGED
|
@@ -86,29 +86,28 @@ function detectUnsafeAliases() {
|
|
|
86
86
|
const resolvedPath = isAbsolute(target) ?
|
|
87
87
|
target : join(PROJECT_ROOT, target);
|
|
88
88
|
|
|
89
|
-
//
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
const isInsideProject = normalizedResolved.startsWith(normalizedRoot);
|
|
95
|
-
|
|
96
|
-
if (!isInsideProject) {
|
|
97
|
-
// Path is outside project - check if it's in node_modules
|
|
98
|
-
if (resolvedPath.includes('/node_modules/')) {
|
|
99
|
-
const packageName = extractPackageNameFromPath(resolvedPath);
|
|
100
|
-
if (packageName) {
|
|
101
|
-
suggestion = packageName;
|
|
102
|
-
}
|
|
103
|
-
} else {
|
|
104
|
-
// Outside project but not node_modules
|
|
105
|
-
suggestion = '(path outside project - use direct imports)';
|
|
89
|
+
// Check if path points to node_modules (breaks in libraries)
|
|
90
|
+
if (resolvedPath.includes('/node_modules/')) {
|
|
91
|
+
const packageName = extractPackageNameFromPath(resolvedPath);
|
|
92
|
+
if (packageName) {
|
|
93
|
+
suggestion = packageName;
|
|
106
94
|
}
|
|
95
|
+
} else {
|
|
96
|
+
// Not node_modules - check if it's outside the project
|
|
97
|
+
const normalizedResolved = resolve(resolvedPath);
|
|
98
|
+
const normalizedRoot = resolve(PROJECT_ROOT);
|
|
107
99
|
|
|
108
|
-
|
|
109
|
-
|
|
100
|
+
const isInsideProject = normalizedResolved.startsWith(normalizedRoot);
|
|
101
|
+
|
|
102
|
+
if (!isInsideProject) {
|
|
103
|
+
// Outside project and not node_modules
|
|
104
|
+
suggestion = '(path outside project - use direct imports)';
|
|
110
105
|
}
|
|
111
106
|
}
|
|
107
|
+
|
|
108
|
+
if (suggestion) {
|
|
109
|
+
UNSAFE_ALIASES.set(alias, suggestion);
|
|
110
|
+
}
|
|
112
111
|
}
|
|
113
112
|
}
|
|
114
113
|
|
|
@@ -560,28 +559,81 @@ async function validateFile(filePath) {
|
|
|
560
559
|
const isInLib = filePath.includes('/src/lib/');
|
|
561
560
|
const isInRoutes = filePath.includes('/src/routes/');
|
|
562
561
|
|
|
563
|
-
// Check each line for import statements
|
|
562
|
+
// Check each line for import statements and JSDoc type imports
|
|
564
563
|
const lines = content.split('\n');
|
|
565
564
|
for (let index = 0; index < lines.length; index++) {
|
|
566
565
|
const line = lines[index];
|
|
567
566
|
const lineNum = index + 1;
|
|
568
567
|
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
568
|
+
let importPathRaw = null;
|
|
569
|
+
let isJSDocImport = false;
|
|
570
|
+
|
|
571
|
+
// Check for regular import statements
|
|
572
|
+
if (line.trim().startsWith('import ')) {
|
|
573
|
+
const importMatch = line.match(/from ['"]([^'"]+)['"]/);
|
|
574
|
+
if (importMatch) {
|
|
575
|
+
importPathRaw = importMatch[1];
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
// Check for JSDoc type imports: import('path')
|
|
579
|
+
else if (line.includes('import(')) {
|
|
580
|
+
// Match import('...') or import("...")
|
|
581
|
+
const jsdocMatch = line.match(/import\(['"]([^'"]+)['"]\)/);
|
|
582
|
+
if (jsdocMatch) {
|
|
583
|
+
importPathRaw = jsdocMatch[1];
|
|
584
|
+
isJSDocImport = true;
|
|
585
|
+
}
|
|
572
586
|
}
|
|
573
587
|
|
|
574
|
-
//
|
|
575
|
-
|
|
576
|
-
if (!importMatch) {
|
|
588
|
+
// Skip if no import found
|
|
589
|
+
if (!importPathRaw) {
|
|
577
590
|
continue;
|
|
578
591
|
}
|
|
579
592
|
|
|
580
|
-
const importPathRaw = importMatch[1];
|
|
581
|
-
|
|
582
593
|
// Strip query parameters (Vite asset imports like ?preset=render)
|
|
583
594
|
let importPath = importPathRaw.split('?')[0];
|
|
584
595
|
|
|
596
|
+
// For JSDoc imports: only check unsafe aliases, skip other validations
|
|
597
|
+
if (isJSDocImport) {
|
|
598
|
+
// Check if using an unsafe alias
|
|
599
|
+
const isAliasImport = Object.keys(PROJECT_ALIASES).some(
|
|
600
|
+
alias => importPath === alias || importPath.startsWith(alias + '/')
|
|
601
|
+
);
|
|
602
|
+
|
|
603
|
+
if (isAliasImport && isInLib) {
|
|
604
|
+
let matchedAlias = null;
|
|
605
|
+
for (const alias of Object.keys(PROJECT_ALIASES)) {
|
|
606
|
+
if (importPath === alias || importPath.startsWith(alias + '/')) {
|
|
607
|
+
matchedAlias = alias;
|
|
608
|
+
break;
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
if (matchedAlias && UNSAFE_ALIASES.has(matchedAlias)) {
|
|
613
|
+
const suggestion = UNSAFE_ALIASES.get(matchedAlias);
|
|
614
|
+
const pathAfterAlias = importPath.slice(matchedAlias.length);
|
|
615
|
+
|
|
616
|
+
let errorMsg;
|
|
617
|
+
if (suggestion.startsWith('(')) {
|
|
618
|
+
errorMsg = `${relativePath}:${lineNum}\n` +
|
|
619
|
+
` JSDoc import('${importPath}')\n` +
|
|
620
|
+
` => ${suggestion}`;
|
|
621
|
+
} else {
|
|
622
|
+
const suggestedImport = suggestion + pathAfterAlias;
|
|
623
|
+
errorMsg = `${relativePath}:${lineNum}\n` +
|
|
624
|
+
` JSDoc import('${importPath}')\n` +
|
|
625
|
+
` => import('${suggestedImport}') ` +
|
|
626
|
+
`(alias resolves outside project)`;
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
errors.push(errorMsg);
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
// Skip all other validations for JSDoc imports
|
|
634
|
+
continue;
|
|
635
|
+
}
|
|
636
|
+
|
|
585
637
|
// Check if using $src/lib when $lib is available (built-in SvelteKit)
|
|
586
638
|
// Report the issue and normalize the path for further validation
|
|
587
639
|
let hasSrcLibIssue = false;
|