release: WRNexusJS 0.6.0
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
import { existsSync, realpathSync } from "node:fs";
|
||||
import { dirname, extname, join, resolve } from "node:path";
|
||||
import type { StructuredImportDecl } from "@wrnexus/syntax";
|
||||
|
||||
export type ImportMode = "legacy" | "compatible" | "explicit";
|
||||
export interface ImportResolverOptions {
|
||||
appRoot: string;
|
||||
mode?: ImportMode;
|
||||
aliases?: Record<string, string>;
|
||||
}
|
||||
export interface ResolvedImport {
|
||||
declaration: StructuredImportDecl;
|
||||
resolved?: string;
|
||||
diagnostic?: { code: string; message: string; severity: "error" | "warning" };
|
||||
}
|
||||
|
||||
function candidates(path: string): string[] {
|
||||
return extname(path)
|
||||
? [path]
|
||||
: [
|
||||
path,
|
||||
`${path}.wrn`,
|
||||
`${path}.ts`,
|
||||
`${path}.d.ts`,
|
||||
join(path, "index.wrn"),
|
||||
join(path, "index.ts"),
|
||||
];
|
||||
}
|
||||
|
||||
export function resolveWrnImport(
|
||||
declaration: StructuredImportDecl,
|
||||
importer: string,
|
||||
options: ImportResolverOptions,
|
||||
): ResolvedImport {
|
||||
const source = declaration.source;
|
||||
if (!source.startsWith(".") && !source.startsWith("@/")) return { declaration, resolved: source };
|
||||
const aliasRoot = options.aliases?.["@"] ?? "./app";
|
||||
const base = source.startsWith("@/")
|
||||
? resolve(options.appRoot, aliasRoot, source.slice(2))
|
||||
: resolve(dirname(importer), source);
|
||||
const found = candidates(base).find(existsSync);
|
||||
if (found) return { declaration, resolved: realpathSync(found) };
|
||||
const severity = (options.mode ?? "compatible") === "explicit" ? "error" : "warning";
|
||||
return {
|
||||
declaration,
|
||||
diagnostic: {
|
||||
code: "WRN-IMPORT-NOT-FOUND",
|
||||
message: `Cannot resolve import '${source}' from ${importer}`,
|
||||
severity,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function resolveWrnImports(
|
||||
declarations: StructuredImportDecl[],
|
||||
importer: string,
|
||||
options: ImportResolverOptions,
|
||||
): ResolvedImport[] {
|
||||
return declarations.map((declaration) => resolveWrnImport(declaration, importer, options));
|
||||
}
|
||||
Reference in New Issue
Block a user