-
Notifications
You must be signed in to change notification settings - Fork 109
/
tsickle.ts
318 lines (286 loc) · 11.1 KB
/
tsickle.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import * as ts from 'typescript';
import {AnnotatorHost} from './annotator_host';
import {assertAbsolute} from './cli_support';
import * as clutz from './clutz';
import {decoratorDownlevelTransformer} from './decorator_downlevel_transformer';
import {transformDecoratorJsdoc, transformDecoratorsOutputForClosurePropertyRenaming} from './decorators';
import {enumTransformer} from './enum_transformer';
import {generateExterns} from './externs';
import {transformFileoverviewCommentFactory} from './fileoverview_comment_transformer';
import * as googmodule from './googmodule';
import {jsdocTransformer, removeTypeAssertions} from './jsdoc_transformer';
import {ModulesManifest} from './modules_manifest';
import {namespaceTransformer} from './ns_transformer';
import {FileSummary, SummaryGenerationProcessorHost} from './summary';
import {isDtsFileName} from './transformer_util';
import * as tsmes from './ts_migration_exports_shim';
import {makeTsickleDeclarationMarkerTransformerFactory} from './tsickle_declaration_marker';
// Exported for users as a default impl of pathToModuleName.
export {pathToModuleName} from './cli_support';
// Retained here for API compatibility.
export {getGeneratedExterns} from './externs';
export {FileMap, ModulesManifest} from './modules_manifest';
export {FileSummary, ModuleType, Symbol, Type} from './summary';
export interface TsickleHost extends googmodule.GoogModuleProcessorHost,
tsmes.TsMigrationExportsShimProcessorHost,
AnnotatorHost,
SummaryGenerationProcessorHost {
/**
* Whether to downlevel decorators
*/
transformDecorators?: boolean;
/**
* Whether to convert types to closure
*/
transformTypesToClosure?: boolean;
/** Are tsMigrationExports calls allowed and should shim files be emitted? */
generateTsMigrationExportsShim?: boolean;
/**
* Whether to add aliases to the .d.ts files to add the exports to the
* ಠ_ಠ.clutz namespace.
*/
addDtsClutzAliases?: boolean;
/**
* If true, tsickle and decorator downlevel processing will be skipped for
* that file.
*/
shouldSkipTsickleProcessing(fileName: string): boolean;
/**
* Tsickle treats warnings as errors, if true, ignore warnings. This might be
* useful for e.g. third party code.
*/
shouldIgnoreWarningsForPath(filePath: string): boolean;
/**
* Whether to convert CommonJS require() imports to goog.module() and
* goog.require() calls.
*/
googmodule: boolean;
/**
* Whether to transform declaration merging namespaces.
*/
useDeclarationMergingTransformation?: boolean;
/**
* Whether to add suppressions by default.
*/
generateExtraSuppressions: boolean;
/**
* Whether to generate summaries.
*/
generateSummary?: boolean;
}
export function mergeEmitResults(emitResults: EmitResult[]): EmitResult {
const diagnostics: ts.Diagnostic[] = [];
let emitSkipped = true;
const emittedFiles: string[] = [];
const externs:
{[fileName: string]: {output: string, moduleNamespace: string}} = {};
const modulesManifest = new ModulesManifest();
const tsMigrationExportsShimFiles = new Map<string, string>();
const fileSummaries = new Map<string, FileSummary>();
for (const er of emitResults) {
diagnostics.push(...er.diagnostics);
emitSkipped = emitSkipped || er.emitSkipped;
if (er.emittedFiles) {
emittedFiles.push(...er.emittedFiles);
}
Object.assign(externs, er.externs);
modulesManifest.addManifest(er.modulesManifest);
for (const [k, v] of er.tsMigrationExportsShimFiles) {
tsMigrationExportsShimFiles.set(k, v);
}
for (const [k, v] of er.fileSummaries) {
fileSummaries.set(k, v);
}
}
return {
diagnostics,
emitSkipped,
emittedFiles,
externs,
tsMigrationExportsShimFiles,
modulesManifest,
fileSummaries,
};
}
export interface EmitResult extends ts.EmitResult {
// The manifest of JS modules output by the compiler.
modulesManifest: ModulesManifest;
/**
* externs.js files produced by tsickle, if any. module IDs are relative paths
* from fileNameToModuleId.
*/
externs: {[moduleId: string]: {output: string, moduleNamespace: string}};
/**
* Content for the generated files, keyed by their intended filename.
* Filenames are google3 relative.
*/
tsMigrationExportsShimFiles: tsmes.TsMigrationExportsShimFileMap;
fileSummaries: Map<string, FileSummary>;
}
export interface EmitTransformers {
/** Custom transformers to evaluate before built-in .js transformations. */
beforeTs?: ts.CustomTransformers['before'];
/** Custom transformers to evaluate after built-in .js transformations. */
afterTs?: ts.CustomTransformers['after'];
/** Custom transformers to evaluate after built-in .d.ts transformations. */
afterDeclarations?: ts.CustomTransformers['afterDeclarations'];
}
/**
* @deprecated Exposed for backward compat with Angular. Use emit() instead.
*/
export function emitWithTsickle(
program: ts.Program, host: TsickleHost, tsHost: ts.CompilerHost,
tsOptions: ts.CompilerOptions, targetSourceFile?: ts.SourceFile,
writeFile?: ts.WriteFileCallback, cancellationToken?: ts.CancellationToken,
emitOnlyDtsFiles?: boolean,
customTransformers: EmitTransformers = {}): EmitResult {
return emit(
program, host, writeFile || tsHost.writeFile.bind(tsHost),
targetSourceFile, cancellationToken, emitOnlyDtsFiles,
customTransformers);
}
export function emit(
program: ts.Program, host: TsickleHost, writeFile: ts.WriteFileCallback,
targetSourceFile?: ts.SourceFile, cancellationToken?: ts.CancellationToken,
emitOnlyDtsFiles?: boolean,
customTransformers: EmitTransformers = {}): EmitResult {
for (const sf of program.getSourceFiles()) {
assertAbsolute(sf.fileName);
}
let tsickleDiagnostics: ts.Diagnostic[] = [];
const typeChecker = program.getTypeChecker();
const tsOptions = program.getCompilerOptions();
if (!tsOptions.rootDir) {
// Various places within tsickle assume rootDir is always present,
// so return an error here if it wasn't provided.
return {
emitSkipped: false,
diagnostics: [{
category: ts.DiagnosticCategory.Error,
code: 0,
file: undefined,
start: undefined,
length: undefined,
messageText: 'TypeScript options must specify rootDir',
}],
modulesManifest: new ModulesManifest(),
externs: {},
tsMigrationExportsShimFiles: new Map(),
fileSummaries: new Map(),
};
}
const modulesManifest = new ModulesManifest();
const tsMigrationExportsShimFiles = new Map<string, string>();
const tsickleSourceTransformers: Array<ts.TransformerFactory<ts.SourceFile>> =
[];
const fileSummaries = new Map<string, FileSummary>();
tsickleSourceTransformers.push(
tsmes.createTsMigrationExportsShimTransformerFactory(
typeChecker, host, modulesManifest, tsickleDiagnostics,
tsMigrationExportsShimFiles, fileSummaries));
if (host.transformTypesToClosure) {
// Only add @suppress {checkTypes} comments when also adding type
// annotations.
tsickleSourceTransformers.push(transformFileoverviewCommentFactory(
tsOptions, tsickleDiagnostics, host.generateExtraSuppressions));
if (host.useDeclarationMergingTransformation) {
tsickleSourceTransformers.push(namespaceTransformer(
host, tsOptions, typeChecker, tsickleDiagnostics));
}
tsickleSourceTransformers.push(
jsdocTransformer(host, tsOptions, typeChecker, tsickleDiagnostics));
tsickleSourceTransformers.push(enumTransformer(typeChecker));
}
if (host.transformDecorators) {
tsickleSourceTransformers.push(
decoratorDownlevelTransformer(typeChecker, tsickleDiagnostics));
}
const tsTransformers: ts.CustomTransformers = {
before: [
...(tsickleSourceTransformers || [])
.map(tf => skipTransformForSourceFileIfNeeded(host, tf)),
...(customTransformers.beforeTs || []),
],
after: [...(customTransformers.afterTs || [])],
afterDeclarations: [...(customTransformers.afterDeclarations || [])]
};
if (host.transformTypesToClosure) {
// See comment on removeTypeAssertions.
tsTransformers.before!.push(removeTypeAssertions());
}
if (host.googmodule) {
tsTransformers.after!.push(googmodule.commonJsToGoogmoduleTransformer(
host, modulesManifest, typeChecker));
tsTransformers.after!.push(
transformDecoratorsOutputForClosurePropertyRenaming(
tsickleDiagnostics));
tsTransformers.after!.push(transformDecoratorJsdoc());
}
if (host.addDtsClutzAliases) {
tsTransformers.afterDeclarations!.push(
clutz.makeDeclarationTransformerFactory(typeChecker, host));
}
// Adds a marker to the top of tsickle-generated .d.ts files, should always go
// last
tsTransformers.afterDeclarations!.push(
makeTsickleDeclarationMarkerTransformerFactory(tsOptions));
const {diagnostics: tsDiagnostics, emitSkipped, emittedFiles} = program.emit(
targetSourceFile, writeFile, cancellationToken, emitOnlyDtsFiles,
tsTransformers);
const externs:
{[fileName: string]: {output: string, moduleNamespace: string}} = {};
if (host.transformTypesToClosure) {
const sourceFiles =
targetSourceFile ? [targetSourceFile] : program.getSourceFiles();
for (const sourceFile of sourceFiles) {
const isDts = isDtsFileName(sourceFile.fileName);
if (isDts && host.shouldSkipTsickleProcessing(sourceFile.fileName)) {
continue;
}
const {output, diagnostics, moduleNamespace} =
generateExterns(typeChecker, sourceFile, host);
if (output) {
externs[sourceFile.fileName] = {output, moduleNamespace};
}
if (diagnostics) {
tsickleDiagnostics.push(...diagnostics);
}
}
}
// All diagnostics (including warnings) are treated as errors.
// If the host decides to ignore warnings, just discard them.
// Warnings include stuff like "don't use @type in your jsdoc"; tsickle
// warns and then fixes up the code to be Closure-compatible anyway.
tsickleDiagnostics = tsickleDiagnostics.filter(
d => d.category === ts.DiagnosticCategory.Error ||
!host.shouldIgnoreWarningsForPath(d.file!.fileName));
return {
modulesManifest,
emitSkipped,
emittedFiles: emittedFiles || [],
diagnostics: [...tsDiagnostics, ...tsickleDiagnostics],
externs,
tsMigrationExportsShimFiles,
fileSummaries,
};
}
function skipTransformForSourceFileIfNeeded(
host: TsickleHost, delegateFactory: ts.TransformerFactory<ts.SourceFile>):
ts.TransformerFactory<ts.SourceFile> {
return (context: ts.TransformationContext) => {
const delegate = delegateFactory(context);
return (sourceFile: ts.SourceFile) => {
if (host.shouldSkipTsickleProcessing(sourceFile.fileName)) {
return sourceFile;
}
return delegate(sourceFile);
};
};
}