@@ -30,8 +30,18 @@ interface IReportingRule {
3030 addToApiReportFile : boolean ;
3131}
3232
33+ export interface IMessageRouterOptions {
34+ workingPackageFolder : string | undefined ;
35+ messageCallback : ( ( message : ExtractorMessage ) => void ) | undefined ;
36+ messagesConfig : IExtractorMessagesConfig ;
37+ showVerboseMessages : boolean ;
38+ showDiagnostics : boolean ;
39+ }
40+
3341export class MessageRouter {
34- private readonly _workingPackageFolder : string ;
42+ public static readonly DIAGNOSTICS_LINE : string = '============================================================' ;
43+
44+ private readonly _workingPackageFolder : string | undefined ;
3545 private readonly _messageCallback : ( ( message : ExtractorMessage ) => void ) | undefined ;
3646
3747 // All messages
@@ -53,20 +63,30 @@ export class MessageRouter {
5363
5464 public errorCount : number = 0 ;
5565 public warningCount : number = 0 ;
56- public showVerboseMessages : boolean = false ;
5766
58- public constructor ( workingPackageFolder : string ,
59- messageCallback : ( ( message : ExtractorMessage ) => void ) | undefined ,
60- messagesConfig : IExtractorMessagesConfig ) {
67+ /**
68+ * See {@link IExtractorInvokeOptions.showVerboseMessages}
69+ */
70+ public readonly showVerboseMessages : boolean ;
71+
72+ /**
73+ * See {@link IExtractorInvokeOptions.showDiagnostics}
74+ */
75+ public readonly showDiagnostics : boolean ;
6176
62- this . _workingPackageFolder = workingPackageFolder ;
63- this . _messageCallback = messageCallback ;
77+ public constructor ( options : IMessageRouterOptions ) {
78+ this . _workingPackageFolder = options . workingPackageFolder ;
79+ this . _messageCallback = options . messageCallback ;
6480
6581 this . _messages = [ ] ;
6682 this . _associatedMessagesForAstDeclaration = new Map < AstDeclaration , ExtractorMessage [ ] > ( ) ;
6783 this . _sourceMapper = new SourceMapper ( ) ;
6884
69- this . _applyMessagesConfig ( messagesConfig ) ;
85+ // showDiagnostics implies showVerboseMessages
86+ this . showVerboseMessages = options . showVerboseMessages || options . showDiagnostics ;
87+ this . showDiagnostics = options . showDiagnostics ;
88+
89+ this . _applyMessagesConfig ( options . messagesConfig ) ;
7090 }
7191
7292 /**
@@ -222,6 +242,57 @@ export class MessageRouter {
222242 }
223243 }
224244
245+ /**
246+ * Recursively collects the primitive members (numbers, strings, arrays, etc) into an object that
247+ * is JSON serializable. This is used by the "--diagnostics" feature to dump the state of configuration objects.
248+ *
249+ * @returns a JSON serializable object (possibly including `null` values)
250+ * or `undefined` if the input cannot be represented as JSON
251+ */
252+ // tslint:disable-next-line:no-any
253+ public static buildJsonDumpObject ( input : any ) : any | undefined {
254+ if ( input === null || input === undefined ) {
255+ // tslint:disable-next-line:no-null-keyword
256+ return null ; // JSON uses null instead of undefined
257+ }
258+
259+ switch ( typeof input ) {
260+ case 'boolean' :
261+ case 'number' :
262+ case 'string' :
263+ return input ;
264+ case 'object' :
265+ if ( Array . isArray ( input ) ) {
266+ // tslint:disable-next-line:no-any
267+ const outputArray : any [ ] = [ ] ;
268+ for ( const element of input ) {
269+ // tslint:disable-next-line:no-any
270+ const serializedElement : any = MessageRouter . buildJsonDumpObject ( element ) ;
271+ if ( serializedElement !== undefined ) {
272+ outputArray . push ( serializedElement ) ;
273+ }
274+ }
275+ return outputArray ;
276+ }
277+
278+ const outputObject : object = { } ;
279+ for ( const key of Object . getOwnPropertyNames ( input ) ) {
280+ // tslint:disable-next-line:no-any
281+ const value : any = input [ key ] ;
282+
283+ // tslint:disable-next-line:no-any
284+ const serializedValue : any = MessageRouter . buildJsonDumpObject ( value ) ;
285+
286+ if ( serializedValue !== undefined ) {
287+ outputObject [ key ] = serializedValue ;
288+ }
289+ }
290+ return outputObject ;
291+ }
292+
293+ return undefined ;
294+ }
295+
225296 /**
226297 * Record this message in _associatedMessagesForAstDeclaration
227298 */
@@ -384,6 +455,20 @@ export class MessageRouter {
384455 } ) ) ;
385456 }
386457
458+ public logDiagnosticHeader ( title : string ) : void {
459+ this . logDiagnostic ( MessageRouter . DIAGNOSTICS_LINE ) ;
460+ this . logDiagnostic ( `DIAGNOSTIC: ` + title ) ;
461+ this . logDiagnostic ( MessageRouter . DIAGNOSTICS_LINE ) ;
462+ }
463+
464+ public logDiagnosticFooter ( ) : void {
465+ this . logDiagnostic ( MessageRouter . DIAGNOSTICS_LINE + '\n' ) ;
466+ }
467+
468+ public logDiagnostic ( message : string ) : void {
469+ this . logVerbose ( ConsoleMessageId . Diagnostics , message ) ;
470+ }
471+
387472 /**
388473 * Give the calling application a chance to handle the `ExtractorMessage`, and if not, display it on the console.
389474 */
0 commit comments