Skip to content

Commit c94da5f

Browse files
committed
Initial implementation of the "--diagnostics" report
1 parent 08e0424 commit c94da5f

6 files changed

Lines changed: 104 additions & 1 deletion

File tree

‎apps/api-extractor/src/api/ConsoleMessageId.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,10 @@ export const enum ConsoleMessageId {
5858
/**
5959
* "Unable to create the API report file. Please make sure the target folder exists: ___"
6060
*/
61-
ApiReportFolderMissing = 'console-api-report-folder-missing'
61+
ApiReportFolderMissing = 'console-api-report-folder-missing',
62+
63+
/**
64+
* Used for the information printed when the "--diagnostics" flag is enabled.
65+
*/
66+
Diagnostics = 'console-diagnostics'
6267
}

‎apps/api-extractor/src/api/Extractor.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,20 @@ export class Extractor {
193193
showDiagnostics: !!options.showDiagnostics
194194
});
195195

196+
if (messageRouter.showDiagnostics) {
197+
messageRouter.logVerbose(ConsoleMessageId.Diagnostics, MessageRouter.DIAGNOSTICS_HEADER);
198+
199+
messageRouter.logVerbose(ConsoleMessageId.Diagnostics, 'Final prepared ExtractorConfig:');
200+
messageRouter.logVerbose(ConsoleMessageId.Diagnostics, extractorConfig.getDiagnosticDump());
201+
202+
messageRouter.logVerbose(ConsoleMessageId.Diagnostics, '\nCompiler options:');
203+
const serializedOptions: object = MessageRouter.buildJsonDumpObject(compilerState.program.getCompilerOptions());
204+
messageRouter.logVerbose(ConsoleMessageId.Diagnostics,
205+
JSON.stringify(serializedOptions, undefined, 2));
206+
207+
messageRouter.logVerbose(ConsoleMessageId.Diagnostics, MessageRouter.DIAGNOSTICS_FOOTER);
208+
}
209+
196210
const collector: Collector = new Collector({
197211
program: compilerState.program,
198212
messageRouter,

‎apps/api-extractor/src/api/ExtractorConfig.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
IExtractorMessagesConfig
2222
} from './IConfigFile';
2323
import { PackageMetadataManager } from '../analyzer/PackageMetadataManager';
24+
import { MessageRouter } from '../collector/MessageRouter';
2425

2526
/**
2627
* Tokens used during variable expansion of path fields from api-extractor.json.
@@ -220,6 +221,19 @@ export class ExtractorConfig {
220221
this.testMode = parameters.testMode;
221222
}
222223

224+
/**
225+
* Returns a JSON-like string representing the `ExtractorConfig` state, which can be printed to a console
226+
* for diagnostic purposes.
227+
*
228+
* @remarks
229+
* This is used by the "--diagnostics" command-line option. The string is not intended to be deserialized;
230+
* its format may be changed at any time.
231+
*/
232+
public getDiagnosticDump(): string {
233+
const result: object = MessageRouter.buildJsonDumpObject(this);
234+
return JSON.stringify(result, undefined, 2);
235+
}
236+
223237
/**
224238
* Returns a simplified file path for use in error messages.
225239
* @internal

‎apps/api-extractor/src/collector/Collector.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { TypeScriptInternals } from '../analyzer/TypeScriptInternals';
2929
import { MessageRouter } from './MessageRouter';
3030
import { AstReferenceResolver } from '../analyzer/AstReferenceResolver';
3131
import { ExtractorConfig } from '../api/ExtractorConfig';
32+
import { ConsoleMessageId } from '../api/ConsoleMessageId';
3233

3334
/**
3435
* Options for Collector constructor.
@@ -167,6 +168,22 @@ export class Collector {
167168
this.messageRouter.addCompilerDiagnostic(diagnostic);
168169
}
169170

171+
if (this.messageRouter.showDiagnostics) {
172+
173+
this.messageRouter.logVerbose(ConsoleMessageId.Diagnostics,
174+
MessageRouter.DIAGNOSTICS_HEADER + '\nThe compiler analyzed these files:');
175+
for (const sourceFile of this.program.getSourceFiles()) {
176+
this.messageRouter.logVerbose(ConsoleMessageId.Diagnostics, sourceFile.fileName);
177+
}
178+
179+
this.messageRouter.logVerbose(ConsoleMessageId.Diagnostics, '\nRoot filenames:');
180+
for (const fileName of this.program.getRootFileNames()) {
181+
this.messageRouter.logVerbose(ConsoleMessageId.Diagnostics, fileName);
182+
}
183+
184+
this.messageRouter.logVerbose(ConsoleMessageId.Diagnostics, MessageRouter.DIAGNOSTICS_FOOTER);
185+
}
186+
170187
// Build the entry point
171188
const entryPointSourceFile: ts.SourceFile = this.workingPackage.entryPointSourceFile;
172189

‎apps/api-extractor/src/collector/MessageRouter.ts‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ export interface IMessageRouterOptions {
3939
}
4040

4141
export class MessageRouter {
42+
public static readonly DIAGNOSTICS_HEADER: string = '==[DIAGNOSTICS]===================================';
43+
public static readonly DIAGNOSTICS_FOOTER: string = '==================================================\n';
44+
4245
private readonly _workingPackageFolder: string | undefined;
4346
private readonly _messageCallback: ((message: ExtractorMessage) => void) | undefined;
4447

@@ -240,6 +243,54 @@ export class MessageRouter {
240243
}
241244
}
242245

246+
/**
247+
* Recursively collects the primitive members (numbers, strings, arrays, etc) into an object that
248+
* is JSON serializable. This is used by the "--diagnostics" feature to dump the state of configuration objects.
249+
*/
250+
// tslint:disable-next-line:no-any
251+
public static buildJsonDumpObject(input: any): any {
252+
if (input === null || input === undefined) {
253+
// tslint:disable-next-line:no-null-keyword
254+
return null; // JSON uses null instead of undefined
255+
}
256+
257+
switch (typeof input) {
258+
case 'boolean':
259+
case 'number':
260+
case 'string':
261+
return input;
262+
case 'object':
263+
if (Array.isArray(input)) {
264+
// tslint:disable-next-line:no-any
265+
const outputArray: any[] = [];
266+
for (const element of input) {
267+
// tslint:disable-next-line:no-any
268+
const serializedElement: any = MessageRouter.buildJsonDumpObject(element);
269+
if (serializedElement !== undefined) {
270+
outputArray.push(serializedElement);
271+
}
272+
}
273+
return outputArray;
274+
}
275+
276+
const outputObject: object = { };
277+
for (const key of Object.getOwnPropertyNames(input)) {
278+
// tslint:disable-next-line:no-any
279+
const value: any = input[key];
280+
281+
// tslint:disable-next-line:no-any
282+
const serializedValue: any = MessageRouter.buildJsonDumpObject(value);
283+
284+
if (serializedValue !== undefined) {
285+
outputObject[key] = serializedValue;
286+
}
287+
}
288+
return outputObject;
289+
}
290+
291+
return undefined;
292+
}
293+
243294
/**
244295
* Record this message in _associatedMessagesForAstDeclaration
245296
*/

‎common/reviews/api/api-extractor.api.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export const enum ConsoleMessageId {
2222
ApiReportFolderMissing = "console-api-report-folder-missing",
2323
ApiReportNotCopied = "console-api-report-not-copied",
2424
ApiReportUnchanged = "console-api-report-unchanged",
25+
Diagnostics = "console-diagnostics",
2526
FoundTSDocMetadata = "console-found-tsdoc-metadata",
2627
WritingDocModelFile = "console-writing-doc-model-file",
2728
WritingDtsRollup = "console-writing-dts-rollup"
@@ -42,6 +43,7 @@ export class ExtractorConfig {
4243
readonly betaTrimmedFilePath: string;
4344
readonly docModelEnabled: boolean;
4445
static readonly FILENAME: string;
46+
getDiagnosticDump(): string;
4547
// @internal
4648
_getShortFilePath(absolutePath: string): string;
4749
static hasDtsFileExtension(filePath: string): boolean;

0 commit comments

Comments
 (0)