Skip to content

Commit e9b2a9f

Browse files
authored
fix: Accommodate TS 4.4 paths pattern caching (fixes #114) (#116)
fix: Accommodate TS 4.4 paths pattern caching (fixes #114)
1 parent 3e250a8 commit e9b2a9f

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

src/transformer.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ export default function transformer(
2828

2929
if (!pathsBasePath || !compilerOptions.paths) return (sourceFile: ts.SourceFile) => sourceFile;
3030

31+
const { configFile, paths } = compilerOptions;
32+
// TODO - Remove typecast when tryParsePatterns is recognized (probably after ts v4.4)
33+
const { tryParsePatterns } = tsInstance as any;
34+
3135
const tsTransformPathsContext: TsTransformPathsContext = {
3236
compilerOptions,
3337
config,
@@ -43,6 +47,11 @@ export default function transformer(
4347
excludeMatchers: config.exclude?.map((globPattern) => new Minimatch(globPattern, { matchBase: true })),
4448
parsedCommandLine: createParsedCommandLineForProgram(tsInstance, program),
4549
outputFileNamesCache: new Map(),
50+
// Get paths patterns appropriate for TS compiler version
51+
pathsPatterns: tryParsePatterns
52+
// TODO - Remove typecast when pathPatterns is recognized (probably after ts v4.4)
53+
? (configFile?.configFileSpecs as any)?.pathPatterns || tryParsePatterns(paths)
54+
: tsInstance.getOwnKeys(paths)
4655
};
4756

4857
return (sourceFile: ts.SourceFile) => {

src/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import tsThree from "./declarations/typescript3";
2-
import ts, { CompilerOptions, GetCanonicalFileName, ParsedCommandLine } from "typescript";
2+
import ts, { CompilerOptions, GetCanonicalFileName, ParsedCommandLine, Pattern } from "typescript";
33
import { PluginConfig } from "ts-patch";
44
import { HarmonyFactory } from "./utils/harmony-factory";
55
import { IMinimatch } from "minimatch";
@@ -51,6 +51,7 @@ export interface TsTransformPathsContext {
5151
readonly outputFileNamesCache: Map<string, string>;
5252
readonly pathsBasePath: string;
5353
readonly getCanonicalFileName: GetCanonicalFileName;
54+
readonly pathsPatterns: (string | Pattern)[]
5455
}
5556

5657
export interface VisitorContext extends TsTransformPathsContext {

src/utils/ts-helpers.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@ export function getOutputFile(context: VisitorContext, fileName: string): string
6565
* Determine if moduleName matches config in paths
6666
*/
6767
export function isModulePathsMatch(context: VisitorContext, moduleName: string): boolean {
68-
const { matchPatternOrExact, getOwnKeys } = context.tsInstance;
69-
return !!matchPatternOrExact(getOwnKeys(context.compilerOptions.paths!), moduleName);
68+
const { pathsPatterns, tsInstance: { matchPatternOrExact }} = context
69+
// TODO - Remove typecast after ts v4.4
70+
return !!matchPatternOrExact((pathsPatterns as any), moduleName);
7071
}
7172

7273
export function isTsProjectSourceFile(context: VisitorContext, filePath: string): boolean {

0 commit comments

Comments
 (0)