Skip to content

Commit 4cf746c

Browse files
committed
Enable listFiles and listEmittedFiles as build option
1 parent 0d60348 commit 4cf746c

File tree

4 files changed

+96
-29
lines changed

4 files changed

+96
-29
lines changed

src/compiler/commandLineParser.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,18 @@ namespace ts {
8484
category: Diagnostics.Command_line_Options,
8585
description: Diagnostics.Whether_to_keep_outdated_console_output_in_watch_mode_instead_of_clearing_the_screen,
8686
},
87+
{
88+
name: "listFiles",
89+
type: "boolean",
90+
category: Diagnostics.Advanced_Options,
91+
description: Diagnostics.Print_names_of_files_part_of_the_compilation
92+
},
93+
{
94+
name: "listEmittedFiles",
95+
type: "boolean",
96+
category: Diagnostics.Advanced_Options,
97+
description: Diagnostics.Print_names_of_generated_files_part_of_the_compilation
98+
},
8799
{
88100
name: "watch",
89101
shortName: "w",
@@ -562,18 +574,6 @@ namespace ts {
562574
category: Diagnostics.Advanced_Options,
563575
description: Diagnostics.Include_modules_imported_with_json_extension
564576
},
565-
{
566-
name: "listFiles",
567-
type: "boolean",
568-
category: Diagnostics.Advanced_Options,
569-
description: Diagnostics.Print_names_of_files_part_of_the_compilation
570-
},
571-
{
572-
name: "listEmittedFiles",
573-
type: "boolean",
574-
category: Diagnostics.Advanced_Options,
575-
description: Diagnostics.Print_names_of_generated_files_part_of_the_compilation
576-
},
577577

578578
{
579579
name: "out",

src/compiler/tsbuild.ts

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,14 @@ namespace ts {
2121
dry?: boolean;
2222
force?: boolean;
2323
verbose?: boolean;
24+
2425
/*@internal*/ clean?: boolean;
2526
/*@internal*/ watch?: boolean;
2627
/*@internal*/ help?: boolean;
28+
2729
preserveWatchOutput?: boolean;
30+
listEmittedFiles?: boolean;
31+
listFiles?: boolean;
2832
}
2933

3034
enum BuildResultFlags {
@@ -44,8 +48,9 @@ namespace ts {
4448
SyntaxErrors = 1 << 3,
4549
TypeErrors = 1 << 4,
4650
DeclarationEmitErrors = 1 << 5,
51+
EmitErrors = 1 << 6,
4752

48-
AnyErrors = ConfigFileErrors | SyntaxErrors | TypeErrors | DeclarationEmitErrors
53+
AnyErrors = ConfigFileErrors | SyntaxErrors | TypeErrors | DeclarationEmitErrors | EmitErrors
4954
}
5055

5156
export enum UpToDateStatusType {
@@ -401,6 +406,7 @@ namespace ts {
401406
const projectStatus = createFileMap<UpToDateStatus>(toPath);
402407
const missingRoots = createMap<true>();
403408
let globalDependencyGraph: DependencyGraph | undefined;
409+
const writeFileName = (s: string) => host.trace && host.trace(s);
404410

405411
// Watch state
406412
const diagnostics = createFileMap<ReadonlyArray<Diagnostic>>(toPath);
@@ -1014,35 +1020,28 @@ namespace ts {
10141020
...program.getConfigFileParsingDiagnostics(),
10151021
...program.getSyntacticDiagnostics()];
10161022
if (syntaxDiagnostics.length) {
1017-
resultFlags |= BuildResultFlags.SyntaxErrors;
1018-
reportAndStoreErrors(proj, syntaxDiagnostics);
1019-
projectStatus.setValue(proj, { type: UpToDateStatusType.Unbuildable, reason: "Syntactic errors" });
1020-
return resultFlags;
1023+
return buildErrors(syntaxDiagnostics, BuildResultFlags.SyntaxErrors, "Syntactic");
10211024
}
10221025

10231026
// Don't emit .d.ts if there are decl file errors
10241027
if (getEmitDeclarations(program.getCompilerOptions())) {
10251028
const declDiagnostics = program.getDeclarationDiagnostics();
10261029
if (declDiagnostics.length) {
1027-
resultFlags |= BuildResultFlags.DeclarationEmitErrors;
1028-
reportAndStoreErrors(proj, declDiagnostics);
1029-
projectStatus.setValue(proj, { type: UpToDateStatusType.Unbuildable, reason: "Declaration file errors" });
1030-
return resultFlags;
1030+
return buildErrors(declDiagnostics, BuildResultFlags.DeclarationEmitErrors, "Declaration file");
10311031
}
10321032
}
10331033

10341034
// Same as above but now for semantic diagnostics
10351035
const semanticDiagnostics = program.getSemanticDiagnostics();
10361036
if (semanticDiagnostics.length) {
1037-
resultFlags |= BuildResultFlags.TypeErrors;
1038-
reportAndStoreErrors(proj, semanticDiagnostics);
1039-
projectStatus.setValue(proj, { type: UpToDateStatusType.Unbuildable, reason: "Semantic errors" });
1040-
return resultFlags;
1037+
return buildErrors(semanticDiagnostics, BuildResultFlags.TypeErrors, "Semantic");
10411038
}
10421039

10431040
let newestDeclarationFileContentChangedTime = minimumDate;
10441041
let anyDtsChanged = false;
1045-
program.emit(/*targetSourceFile*/ undefined, (fileName, content, writeBom, onError) => {
1042+
let emitDiagnostics: Diagnostic[] | undefined;
1043+
const reportEmitDiagnostic = (d: Diagnostic) => (emitDiagnostics || (emitDiagnostics = [])).push(d);
1044+
emitFilesAndReportErrors(program, reportEmitDiagnostic, writeFileName, /*reportSummary*/ undefined, (fileName, content, writeBom, onError) => {
10461045
let priorChangeTime: Date | undefined;
10471046
if (!anyDtsChanged && isDeclarationFile(fileName)) {
10481047
// Check for unchanged .d.ts files
@@ -1062,12 +1061,23 @@ namespace ts {
10621061
}
10631062
});
10641063

1064+
if (emitDiagnostics) {
1065+
return buildErrors(emitDiagnostics, BuildResultFlags.EmitErrors, "Emit");
1066+
}
1067+
10651068
const status: UpToDateStatus = {
10661069
type: UpToDateStatusType.UpToDate,
10671070
newestDeclarationFileContentChangedTime: anyDtsChanged ? maximumDate : newestDeclarationFileContentChangedTime
10681071
};
10691072
projectStatus.setValue(proj, status);
10701073
return resultFlags;
1074+
1075+
function buildErrors(diagnostics: ReadonlyArray<Diagnostic>, errorFlags: BuildResultFlags, errorType: string) {
1076+
resultFlags |= errorFlags;
1077+
reportAndStoreErrors(proj, diagnostics);
1078+
projectStatus.setValue(proj, { type: UpToDateStatusType.Unbuildable, reason: `${errorType} errors` });
1079+
return resultFlags;
1080+
}
10711081
}
10721082

10731083
function updateOutputTimestamps(proj: ParsedCommandLine) {

src/compiler/watch.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,15 @@ namespace ts {
101101
getGlobalDiagnostics(): ReadonlyArray<Diagnostic>;
102102
getSemanticDiagnostics(): ReadonlyArray<Diagnostic>;
103103
getConfigFileParsingDiagnostics(): ReadonlyArray<Diagnostic>;
104-
emit(): EmitResult;
104+
emit(targetSourceFile?: SourceFile, writeFile?: WriteFileCallback): EmitResult;
105105
}
106106

107107
export type ReportEmitErrorSummary = (errorCount: number) => void;
108108

109109
/**
110110
* Helper that emit files, report diagnostics and lists emitted and/or source files depending on compiler options
111111
*/
112-
export function emitFilesAndReportErrors(program: ProgramToEmitFilesAndReportErrors, reportDiagnostic: DiagnosticReporter, writeFileName?: (s: string) => void, reportSummary?: ReportEmitErrorSummary) {
112+
export function emitFilesAndReportErrors(program: ProgramToEmitFilesAndReportErrors, reportDiagnostic: DiagnosticReporter, writeFileName?: (s: string) => void, reportSummary?: ReportEmitErrorSummary, writeFile?: WriteFileCallback) {
113113
// First get and report any syntactic errors.
114114
const diagnostics = program.getConfigFileParsingDiagnostics().slice();
115115
const configFileParsingDiagnosticsLength = diagnostics.length;
@@ -128,7 +128,7 @@ namespace ts {
128128
}
129129

130130
// Emit and report any errors we ran into.
131-
const { emittedFiles, emitSkipped, diagnostics: emitDiagnostics } = program.emit();
131+
const { emittedFiles, emitSkipped, diagnostics: emitDiagnostics } = program.emit(/*targetSourceFile*/ undefined, writeFile);
132132
addRange(diagnostics, emitDiagnostics);
133133

134134
if (reportSemanticDiagnostics) {

src/testRunner/unittests/tsbuild.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,63 @@ export class cNew {}`);
264264
verifyProjectWithResolveJsonModule("/src/tests/tsconfig_withIncludeAndFiles.json");
265265
});
266266
});
267+
268+
describe("tsbuild - lists files", () => {
269+
it("listFiles", () => {
270+
const fs = projFs.shadow();
271+
const host = new fakes.SolutionBuilderHost(fs);
272+
const builder = createSolutionBuilder(host, ["/src/tests"], { listFiles: true });
273+
builder.buildAllProjects();
274+
assert.deepEqual(host.traces, [
275+
...getLibs(),
276+
"/src/core/anotherModule.ts",
277+
"/src/core/index.ts",
278+
"/src/core/some_decl.d.ts",
279+
...getLibs(),
280+
...getCoreOutputs(),
281+
"/src/logic/index.ts",
282+
...getLibs(),
283+
...getCoreOutputs(),
284+
"/src/logic/index.d.ts",
285+
"/src/tests/index.ts"
286+
]);
287+
288+
function getLibs() {
289+
return [
290+
"/lib/lib.d.ts",
291+
"/lib/lib.es5.d.ts",
292+
"/lib/lib.dom.d.ts",
293+
"/lib/lib.webworker.importscripts.d.ts",
294+
"/lib/lib.scripthost.d.ts"
295+
];
296+
}
297+
298+
function getCoreOutputs() {
299+
return [
300+
"/src/core/index.d.ts",
301+
"/src/core/anotherModule.d.ts"
302+
];
303+
}
304+
});
305+
306+
it("listEmittedFiles", () => {
307+
const fs = projFs.shadow();
308+
const host = new fakes.SolutionBuilderHost(fs);
309+
const builder = createSolutionBuilder(host, ["/src/tests"], { listEmittedFiles: true });
310+
builder.buildAllProjects();
311+
assert.deepEqual(host.traces, [
312+
"TSFILE: /src/core/anotherModule.js",
313+
"TSFILE: /src/core/anotherModule.d.ts",
314+
"TSFILE: /src/core/index.js",
315+
"TSFILE: /src/core/index.d.ts",
316+
"TSFILE: /src/logic/index.js",
317+
"TSFILE: /src/logic/index.js.map",
318+
"TSFILE: /src/logic/index.d.ts",
319+
"TSFILE: /src/tests/index.js",
320+
"TSFILE: /src/tests/index.d.ts",
321+
]);
322+
});
323+
});
267324
}
268325

269326
export namespace OutFile {

0 commit comments

Comments
 (0)