From 3631e811eccd09a3b23573b19de761d308d97801 Mon Sep 17 00:00:00 2001 From: Curve Date: Thu, 24 Aug 2023 12:32:52 +0200 Subject: [PATCH 1/2] Add support for Glob-Patterns in "noResolvePaths" --- package.json | 2 ++ src/transpilation/resolve.ts | 11 +++++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index bd1c5ebd4..0463fb57e 100644 --- a/package.json +++ b/package.json @@ -47,10 +47,12 @@ "dependencies": { "@typescript-to-lua/language-extensions": "1.0.0", "enhanced-resolve": "^5.8.2", + "picomatch": "^2.3.1", "resolve": "^1.15.1", "source-map": "^0.7.3" }, "devDependencies": { + "@types/picomatch": "^2.3.0", "@types/fs-extra": "^8.1.0", "@types/glob": "^7.1.1", "@types/jest": "^27.5.2", diff --git a/src/transpilation/resolve.ts b/src/transpilation/resolve.ts index 5768714a8..803efb99a 100644 --- a/src/transpilation/resolve.ts +++ b/src/transpilation/resolve.ts @@ -10,11 +10,12 @@ import { couldNotReadDependency, couldNotResolveRequire } from "./diagnostics"; import { BuildMode, CompilerOptions } from "../CompilerOptions"; import { findLuaRequires, LuaRequire } from "./find-lua-requires"; import { Plugin } from "./plugins"; +import * as picomatch from "picomatch"; const resolver = resolve.ResolverFactory.createResolver({ extensions: [".lua"], enforceExtension: true, // Resolved file must be a lua file - fileSystem: { ...new resolve.CachedInputFileSystem(fs) }, + fileSystem: { ...new resolve.CachedInputFileSystem(fs, 0) }, useSyncFileSystemCalls: true, conditionNames: ["require", "node", "tstl", "default"], symlinks: false, // Do not resolve symlinks to their original paths (that breaks node_modules detection) @@ -26,7 +27,7 @@ interface ResolutionResult { } class ResolutionContext { - private noResolvePaths: Set; + private noResolvePaths: picomatch.Matcher[]; public diagnostics: ts.Diagnostic[] = []; public resolvedFiles = new Map(); @@ -37,7 +38,9 @@ class ResolutionContext { private readonly emitHost: EmitHost, private readonly plugins: Plugin[] ) { - this.noResolvePaths = new Set(options.noResolvePaths); + const unique = [...new Set(options.noResolvePaths)]; + const matchers = unique.map(x => picomatch(x)); + this.noResolvePaths = matchers; } public addAndResolveDependencies(file: ProcessedFile): void { @@ -70,7 +73,7 @@ class ResolutionContext { return; } - if (this.noResolvePaths.has(required.requirePath)) { + if (this.noResolvePaths.find(isMatch => isMatch(required.requirePath))) { if (this.options.tstlVerbose) { console.log( `Skipping module resolution of ${required.requirePath} as it is in the tsconfig noResolvePaths.` From 78eb5f914f3db1b6809d9b9be916bcdf60168308 Mon Sep 17 00:00:00 2001 From: Curve Date: Thu, 24 Aug 2023 18:09:55 +0200 Subject: [PATCH 2/2] Add Tests for Glob noResolvePaths --- test/transpile/module-resolution.spec.ts | 59 ++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/test/transpile/module-resolution.spec.ts b/test/transpile/module-resolution.spec.ts index c2de18933..e97b4a146 100644 --- a/test/transpile/module-resolution.spec.ts +++ b/test/transpile/module-resolution.spec.ts @@ -441,6 +441,65 @@ describe("module resolution should not try to resolve modules in noResolvePaths" .setOptions({ noResolvePaths: ["a.b.c.foo", "somethingExtra", "dontResolveThis"] }) .expectToHaveNoDiagnostics(); }); + + test("can ignore specific files with glob pattern", () => { + util.testModule` + // Pre-Load as to not error out at runtime + import "preload"; + + import "ignoreme"; + import * as b from "./actualfile"; + + export const result = b.foo(); + ` + .addExtraFile("preload.lua", 'package.preload["ignoreme"] = function() return nil end') + .addExtraFile( + "actualfile.ts", + `export function foo() + { + return 'foo'; + }` + ) + .addExtraFile( + "ignoreme.d.ts", + `declare module "ignoreme" { + export function foo(): void; + }` + ) + .setOptions({ noResolvePaths: ["ignore*"] }) + .expectToHaveNoDiagnostics() + .expectToEqual({ result: "foo" }); + }); + + test("can ignore all files with glob pattern in require", () => { + util.testModule` + declare function require(this: void, module: string): any; + + const a = require("a") + const b = require("b/b") + const c = require("c/c/c") + const d = require("!:?somefile") + ` + .setOptions({ noResolvePaths: ["**"] }) + .expectToHaveNoDiagnostics(); + }); + + test("can ignore all files with glob pattern 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("ignoreme!") + require("i.g.n.o.r.e") + ` + ) + .setOptions({ noResolvePaths: ["**"] }) + .expectToHaveNoDiagnostics(); + }); }); // https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1062