Skip to content

Commit ae762b6

Browse files
authored
Add noResolvePaths option (#1139)
1 parent 0bfdaf5 commit ae762b6

File tree

3 files changed

+62
-4
lines changed

3 files changed

+62
-4
lines changed

src/CompilerOptions.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@ export interface LuaPluginImport {
2323

2424
export type CompilerOptions = OmitIndexSignature<ts.CompilerOptions> & {
2525
buildMode?: BuildMode;
26-
noImplicitSelf?: boolean;
27-
noHeader?: boolean;
2826
luaBundle?: string;
2927
luaBundleEntry?: string;
3028
luaTarget?: LuaTarget;
3129
luaLibImport?: LuaLibImportKind;
32-
sourceMapTraceback?: boolean;
3330
luaPlugins?: LuaPluginImport[];
31+
noImplicitSelf?: boolean;
32+
noHeader?: boolean;
33+
noResolvePaths?: string[];
3434
plugins?: Array<ts.PluginImport | TransformerImport>;
35+
sourceMapTraceback?: boolean;
3536
tstlVerbose?: boolean;
3637
[option: string]: any;
3738
};

src/transpilation/resolve.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,24 @@ interface ResolutionResult {
2323

2424
class ResolutionContext {
2525
private resultsCache = new Map<string, ResolutionResult>();
26+
private noResolvePaths: Set<string>;
2627

2728
constructor(
2829
public readonly program: ts.Program,
2930
public readonly options: CompilerOptions,
3031
private readonly emitHost: EmitHost
31-
) {}
32+
) {
33+
this.noResolvePaths = new Set(options.noResolvePaths);
34+
}
3235

3336
public resolve(file: ProcessedFile, required: string): ResolutionResult {
37+
if (this.noResolvePaths.has(required)) {
38+
if (this.options.tstlVerbose) {
39+
console.log(`Skipping module resolution of ${required} as it is in the tsconfig noResolvePaths.`);
40+
}
41+
return { resolvedFiles: [], diagnostics: [] };
42+
}
43+
3444
const resolvedDependency = resolveDependency(file, required, this.program, this.emitHost);
3545
if (resolvedDependency) {
3646
if (this.options.tstlVerbose) {

test/transpile/module-resolution.spec.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,53 @@ test("module resolution should not try to resolve @noResolution annotation", ()
330330
.expectToHaveNoDiagnostics();
331331
});
332332

333+
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1101
334+
test("module resolution inline require of environment library workaround", () => {
335+
util.testModule`
336+
declare function require(this: void, module: string): any;
337+
338+
const test = require("@NoResolution:luasource");
339+
test.foo();
340+
`.expectToHaveNoDiagnostics();
341+
});
342+
343+
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1118
344+
describe("module resolution should not try to resolve modules in noResolvePaths", () => {
345+
test("as used in direct import", () => {
346+
util.testModule`
347+
import * as lua from "directimport";
348+
lua.foo();
349+
`
350+
.addExtraFile(
351+
"directimport.d.ts",
352+
`declare module "directimport" {
353+
export function foo(): void;
354+
}`
355+
)
356+
.setOptions({ noResolvePaths: ["directimport"] })
357+
.expectToHaveNoDiagnostics();
358+
});
359+
360+
test("as used in imported lua sources", () => {
361+
util.testModule`
362+
import * as lua from "./luasource";
363+
lua.foo();
364+
`
365+
.addExtraFile("luasource.d.ts", "export function foo(): void;")
366+
.addExtraFile(
367+
"luasource.lua",
368+
`
369+
require("dontResolveThis")
370+
require("a.b.c.foo")
371+
372+
return { foo = function() return "bar" end }
373+
`
374+
)
375+
.setOptions({ noResolvePaths: ["a.b.c.foo", "somethingExtra", "dontResolveThis"] })
376+
.expectToHaveNoDiagnostics();
377+
});
378+
});
379+
333380
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1062
334381
test("module resolution should not rewrite @NoResolution requires in library mode", () => {
335382
const { transpiledFiles } = util.testModule`

0 commit comments

Comments
 (0)