Skip to content

Commit

Permalink
feat(matcher): add custom module resolver for remote module
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed Jan 2, 2022
1 parent be0abf8 commit 8ee5af0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
1 change: 0 additions & 1 deletion helper/_option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ function makeHostOption(
libMap: Map<string, string>,
compilerOptions: CompilerOptions,
): CompilerHost {
console.log(libMap, compilerOptions);
return {
readFile,
useCaseSensitiveFileNames,
Expand Down
39 changes: 22 additions & 17 deletions helper/_runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type {
CompilerHost,
CompilerOptions,
ModuleResolutionHost,
ResolvedModule,
} from "../deps.ts";
import { ts } from "../deps.ts";

Expand Down Expand Up @@ -93,17 +92,32 @@ type ResolveModuleNames = Exclude<
undefined
>;

const reRemoteModule = /^https?:\/\//;

function remoteModuleExists(value: string[]): boolean {
return value.some((v) => reRemoteModule.test(v));
}

function makeResolveModuleNames(
compilerOptions: CompilerOptions,
): typeof resolveModuleNames {
function resolveModuleNames(
...[moduleNames, containingFile]: Parameters<ResolveModuleNames>
): ReturnType<ResolveModuleNames> {
const resolvedModules: ResolvedModule[] = [];

for (const moduleName of moduleNames.map(removeTsExtension)) {
const result = ts.resolveModuleName(
moduleName,
const tmpPath = remoteModuleExists(moduleNames)
? (() =>
Deno.makeTempFileSync({
suffix: ".ts",
}))()
: undefined;

return moduleNames.map((moduleName) => {
const fixedModulePath = reRemoteModule.test(moduleName)
? removeTsExtension(tmpPath!)
: removeTsExtension(moduleName);

const { resolvedModule } = ts.resolveModuleName(
fixedModulePath,
containingFile,
compilerOptions,
{
Expand All @@ -115,18 +129,9 @@ function makeResolveModuleNames(
realpath,
},
);
console.log({
moduleName,
containingFile,
compilerOptions,
result,
});
if (result.resolvedModule) {
resolvedModules.push(result.resolvedModule);
}
}

return resolvedModules;
return resolvedModule;
});
}
return resolveModuleNames;
}
Expand Down
8 changes: 7 additions & 1 deletion matcher/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import { isNotAssignable } from "./is_not_assignable.ts";
import type { Matcher } from "./types.ts";
import type { RequiredByKeys } from "../_types.ts";

const IS_NOT_MODULE = 2306;
const ignoreDiagnosticsCodes = new Set([IS_NOT_MODULE]);

const assertionMap = {
expectType: isIdentical,
expectNotType: isNotIdentical,
Expand Down Expand Up @@ -48,7 +51,10 @@ function inspect(program: Program): Result[] {
].filter(({ file }) => !!file).filter(({ messageText }) => {
// for lib.es2021.d.ts <reference lib="es2021.intl" />
return messageText !== "File '/lib.es2021.intl.d.ts' not found.";
}) as RequiredByKeys<TSDiagnostic, "file">[];
}).filter(({ code }) => !ignoreDiagnosticsCodes.has(code)) as RequiredByKeys<
TSDiagnostic,
"file"
>[];
const typeChecker = program.getTypeChecker();

const assertions = extractAssertions(program);
Expand Down

0 comments on commit 8ee5af0

Please sign in to comment.