Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/CompilerOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@ export interface LuaPluginImport {

export type CompilerOptions = OmitIndexSignature<ts.CompilerOptions> & {
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<ts.PluginImport | TransformerImport>;
sourceMapTraceback?: boolean;
tstlVerbose?: boolean;
[option: string]: any;
};
Expand Down
12 changes: 11 additions & 1 deletion src/transpilation/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,24 @@ interface ResolutionResult {

class ResolutionContext {
private resultsCache = new Map<string, ResolutionResult>();
private noResolvePaths: Set<string>;

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) {
Expand Down
47 changes: 47 additions & 0 deletions test/transpile/module-resolution.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down