From 6017be9db7baee8dedcbbaddaf91fc22adfc50e7 Mon Sep 17 00:00:00 2001 From: Perryvw Date: Tue, 14 Sep 2021 21:42:04 +0200 Subject: [PATCH] Add noResolvePaths option --- src/CompilerOptions.ts | 7 ++-- src/transpilation/resolve.ts | 12 +++++- test/transpile/module-resolution.spec.ts | 47 ++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 4 deletions(-) diff --git a/src/CompilerOptions.ts b/src/CompilerOptions.ts index 9fc232f68..952de19ef 100644 --- a/src/CompilerOptions.ts +++ b/src/CompilerOptions.ts @@ -23,15 +23,16 @@ export interface LuaPluginImport { export type CompilerOptions = OmitIndexSignature & { buildMode?: BuildMode; - noImplicitSelf?: boolean; - noHeader?: boolean; luaBundle?: string; luaBundleEntry?: string; luaTarget?: LuaTarget; luaLibImport?: LuaLibImportKind; - sourceMapTraceback?: boolean; luaPlugins?: LuaPluginImport[]; + noImplicitSelf?: boolean; + noHeader?: boolean; + noResolvePaths?: string[]; plugins?: Array; + sourceMapTraceback?: boolean; tstlVerbose?: boolean; [option: string]: any; }; diff --git a/src/transpilation/resolve.ts b/src/transpilation/resolve.ts index 69895391b..bfdf44b06 100644 --- a/src/transpilation/resolve.ts +++ b/src/transpilation/resolve.ts @@ -23,14 +23,24 @@ interface ResolutionResult { class ResolutionContext { private resultsCache = new Map(); + private noResolvePaths: Set; constructor( public readonly program: ts.Program, public readonly options: CompilerOptions, private readonly emitHost: EmitHost - ) {} + ) { + this.noResolvePaths = new Set(options.noResolvePaths); + } public resolve(file: ProcessedFile, required: string): ResolutionResult { + if (this.noResolvePaths.has(required)) { + if (this.options.tstlVerbose) { + console.log(`Skipping module resolution of ${required} as it is in the tsconfig noResolvePaths.`); + } + return { resolvedFiles: [], diagnostics: [] }; + } + const resolvedDependency = resolveDependency(file, required, this.program, this.emitHost); if (resolvedDependency) { if (this.options.tstlVerbose) { diff --git a/test/transpile/module-resolution.spec.ts b/test/transpile/module-resolution.spec.ts index 0332d8c58..75d025218 100644 --- a/test/transpile/module-resolution.spec.ts +++ b/test/transpile/module-resolution.spec.ts @@ -330,6 +330,53 @@ test("module resolution should not try to resolve @noResolution annotation", () .expectToHaveNoDiagnostics(); }); +// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1101 +test("module resolution inline require of environment library workaround", () => { + util.testModule` + declare function require(this: void, module: string): any; + + const test = require("@NoResolution:luasource"); + test.foo(); + `.expectToHaveNoDiagnostics(); +}); + +// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1118 +describe("module resolution should not try to resolve modules in noResolvePaths", () => { + test("as used in direct import", () => { + util.testModule` + import * as lua from "directimport"; + lua.foo(); + ` + .addExtraFile( + "directimport.d.ts", + `declare module "directimport" { + export function foo(): void; + }` + ) + .setOptions({ noResolvePaths: ["directimport"] }) + .expectToHaveNoDiagnostics(); + }); + + test("as used in imported lua sources", () => { + util.testModule` + import * as lua from "./luasource"; + lua.foo(); + ` + .addExtraFile("luasource.d.ts", "export function foo(): void;") + .addExtraFile( + "luasource.lua", + ` + require("dontResolveThis") + require("a.b.c.foo") + + return { foo = function() return "bar" end } + ` + ) + .setOptions({ noResolvePaths: ["a.b.c.foo", "somethingExtra", "dontResolveThis"] }) + .expectToHaveNoDiagnostics(); + }); +}); + // https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1062 test("module resolution should not rewrite @NoResolution requires in library mode", () => { const { transpiledFiles } = util.testModule`