/
transformer.ts
executable file
·160 lines (141 loc) · 5.75 KB
/
transformer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import path from "path";
import ts, { CompilerOptions } from "typescript";
import { RunMode, TsNodeState, TsTransformPathsConfig, TsTransformPathsContext, VisitorContext } from "./types";
import { nodeVisitor } from "./visitor";
import { createHarmonyFactory } from "./harmony";
import { Minimatch } from "minimatch";
import { createSyntheticEmitHost, getTsNodeRegistrationProperties } from "./utils/ts-helpers";
import { TransformerExtras } from "ts-patch";
/* ****************************************************************************************************************** */
// region: Helpers
/* ****************************************************************************************************************** */
function getTsProperties(args: Parameters<typeof transformer>) {
let tsInstance: typeof ts;
let fileNames: readonly string[] | undefined;
let compilerOptions: CompilerOptions;
let runMode: RunMode;
let tsNodeState: TsNodeState | undefined;
const { 0: program, 2: extras, 3: manualTransformOptions } = args;
tsInstance = extras?.ts ?? ts;
if (program) compilerOptions = program.getCompilerOptions();
const tsNodeProps = getTsNodeRegistrationProperties(tsInstance);
/* Determine RunMode & Setup */
// Note: ts-node passes a Program with the paths property stripped, so we do some comparison to determine if it's the caller
const isTsNode =
tsNodeProps && (!program || compilerOptions!.configFilePath === tsNodeProps.compilerOptions.configFilePath);
// RunMode: Program
if (program && !isTsNode) {
runMode = RunMode.Program;
compilerOptions = compilerOptions!;
}
// RunMode: Manual
else if (manualTransformOptions) {
runMode = RunMode.Manual;
fileNames = manualTransformOptions.fileNames;
compilerOptions = manualTransformOptions.compilerOptions!;
}
// RunMode: TsNode
else if (isTsNode) {
fileNames = tsNodeProps.fileNames;
runMode = RunMode.TsNode;
tsNodeState =
!program ||
(fileNames.length > 1 && program?.getRootFileNames().length === 1) ||
(!compilerOptions!.paths && tsNodeProps!.compilerOptions.paths)
? TsNodeState.Stripped
: TsNodeState.Full;
compilerOptions =
tsNodeState === TsNodeState.Full
? compilerOptions!
: {
...(program?.getCompilerOptions() ?? {}),
...tsNodeProps!.compilerOptions,
};
} else {
throw new Error(
`Cannot transform without a Program, ts-node instance, or manual parameters supplied. ` +
`Make sure you're using ts-patch or ts-node with transpileOnly.`
);
}
return { tsInstance, compilerOptions, fileNames, runMode, tsNodeState };
}
// endregion
/* ****************************************************************************************************************** */
// region: Transformer
/* ****************************************************************************************************************** */
export default function transformer(
program?: ts.Program,
pluginConfig?: TsTransformPathsConfig,
transformerExtras?: TransformerExtras,
/**
* Supply if manually transforming with compiler API via 'transformNodes' / 'transformModule'
*/
manualTransformOptions?: {
compilerOptions?: ts.CompilerOptions;
fileNames?: string[];
}
) {
return (transformationContext: ts.TransformationContext) => {
// prettier-ignore
const {
tsInstance,
compilerOptions,
fileNames,
runMode,
tsNodeState
} = getTsProperties([ program, pluginConfig, transformerExtras, manualTransformOptions ]);
const rootDirs = compilerOptions.rootDirs?.filter(path.isAbsolute);
const config: TsTransformPathsConfig = pluginConfig ?? {};
const getCanonicalFileName = tsInstance.createGetCanonicalFileName(tsInstance.sys.useCaseSensitiveFileNames);
/* Add supplements for various run modes */
let emitHost = transformationContext.getEmitHost();
if (!emitHost || tsNodeState === TsNodeState.Stripped) {
if (!fileNames)
throw new Error(
`No EmitHost found and could not determine files to be processed. Please file an issue with a reproduction!`
);
emitHost = createSyntheticEmitHost(compilerOptions, tsInstance, getCanonicalFileName, fileNames as string[]);
}
/* Create Visitor Context */
const { configFile, paths } = compilerOptions;
const { tryParsePatterns } = tsInstance;
const [tsVersionMajor, tsVersionMinor] = tsInstance.versionMajorMinor.split(".").map((v) => +v);
const tsTransformPathsContext: TsTransformPathsContext = {
compilerOptions,
config,
elisionMap: new Map(),
tsFactory: transformationContext.factory,
program,
rootDirs,
transformationContext,
tsInstance,
tsVersionMajor,
tsVersionMinor,
emitHost,
runMode,
tsNodeState,
excludeMatchers: config.exclude?.map((globPattern) => new Minimatch(globPattern, { matchBase: true })),
outputFileNamesCache: new Map(),
// Get paths patterns appropriate for TS compiler version
pathsPatterns:
paths &&
(tryParsePatterns
? configFile?.configFileSpecs?.pathPatterns || tryParsePatterns(paths)
: tsInstance.getOwnKeys(paths)),
};
return (sourceFile: ts.SourceFile) => {
const visitorContext: VisitorContext = {
...tsTransformPathsContext,
sourceFile,
isDeclarationFile: sourceFile.isDeclarationFile,
originalSourceFile: (<typeof ts>tsInstance).getOriginalSourceFile(sourceFile),
getVisitor() {
return nodeVisitor.bind(this);
},
factory: createHarmonyFactory(tsTransformPathsContext),
};
return tsInstance.visitEachChild(sourceFile, visitorContext.getVisitor(), transformationContext);
};
};
}
// endregion