Skip to content

Commit 31673d7

Browse files
committed
Implement console output
1 parent dbbe444 commit 31673d7

3 files changed

Lines changed: 62 additions & 11 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,9 @@ export class Extractor {
479479
);
480480
}
481481

482+
// Show out all the messages that we collected during analysis
483+
collector.messageRouter.reportMessagesToLogger(this._monitoredLogger, collector.workingPackage.packageFolder);
484+
482485
if (this._localBuild) {
483486
// For a local build, fail if there were errors (but ignore warnings)
484487
return this._monitoredLogger.errorCount === 0;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,13 @@ export class ExtractorMessage {
8080

8181
/**
8282
* The line number where the issue occurred in the input source file. This is not used if `sourceFilePath`
83-
* is undefined.
83+
* is undefined. The first line number is 1.
8484
*/
8585
public readonly sourceFileLine: number | undefined;
8686

8787
/**
8888
* The column number where the issue occurred in the input source file. This is not used if `sourceFilePath`
89-
* is undefined.
89+
* is undefined. The first column number is 1.
9090
*/
9191
public readonly sourceFileColumn: number | undefined;
9292

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

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import * as ts from 'typescript';
55
import * as tsdoc from '@microsoft/tsdoc';
6+
import { Sort } from '@microsoft/node-core-library';
67

78
import { TypeScriptMessageFormatter } from '../analyzer/TypeScriptMessageFormatter';
89
import { AstDeclaration } from '../analyzer/AstDeclaration';
@@ -19,7 +20,7 @@ import {
1920
IExtractorMessageReportingRuleConfig
2021
} from '../api/IExtractorConfig';
2122
import { AedocDefinitions } from '../aedoc/AedocDefinitions';
22-
import { Sort } from '@microsoft/node-core-library';
23+
import { ILogger } from '../api/ILogger';
2324

2425
interface IReportingRule {
2526
logLevel: ExtractorMessageLogLevel;
@@ -146,8 +147,8 @@ export class MessageRouter {
146147
diagnostic.start || 0);
147148

148149
options.sourceFilePath = sourceFile.fileName;
149-
options.sourceFileLine = lineAndCharacter.line;
150-
options.sourceFileColumn = lineAndCharacter.character;
150+
options.sourceFileLine = lineAndCharacter.line + 1;
151+
options.sourceFileColumn = lineAndCharacter.character + 1;
151152
}
152153

153154
this._messages.push(new ExtractorMessage(options));
@@ -189,8 +190,8 @@ export class MessageRouter {
189190
messageId: message.messageId,
190191
text: message.unformattedText,
191192
sourceFilePath: sourceFile.fileName,
192-
sourceFileLine: lineAndCharacter.line,
193-
sourceFileColumn: lineAndCharacter.character
193+
sourceFileLine: lineAndCharacter.line + 1,
194+
sourceFileColumn: lineAndCharacter.character + 1
194195
});
195196

196197
if (astDeclaration) {
@@ -231,8 +232,8 @@ export class MessageRouter {
231232
messageId,
232233
text: messageText,
233234
sourceFilePath: sourceFile.fileName,
234-
sourceFileLine: lineAndCharacter.line,
235-
sourceFileColumn: lineAndCharacter.character
235+
sourceFileLine: lineAndCharacter.line + 1,
236+
sourceFileColumn: lineAndCharacter.character + 1
236237
};
237238

238239
const extractorMessage: ExtractorMessage = new ExtractorMessage(options);
@@ -298,6 +299,40 @@ export class MessageRouter {
298299
return messagesForApiReviewFile;
299300
}
300301

302+
/**
303+
* This returns the list of remaining messages that were not already processed by
304+
* `fetchAssociatedMessagesForReviewFile()` or `fetchUnassociatedMessagesForReviewFile()`.
305+
* These messages will be shown on the console.
306+
*/
307+
public reportMessagesToLogger(logger: ILogger, workingPackageFolderPath: string): ExtractorMessage[] {
308+
const messagesForLogger: ExtractorMessage[] = [];
309+
310+
for (const message of this.messages) {
311+
312+
// Make sure we didn't already report this message
313+
if (!this._messagesAddedToApiReviewFile.has(message)) {
314+
messagesForLogger.push(message);
315+
}
316+
}
317+
318+
this._sortMessagesForOutput(messagesForLogger);
319+
320+
for (const message of messagesForLogger) {
321+
// Is this message type configured to go to the console?
322+
const reportingRule: IReportingRule = this._getRuleForMessage(message);
323+
switch (reportingRule.logLevel) {
324+
case ExtractorMessageLogLevel.Error:
325+
logger.logError('Error: ' + message.formatMessageAndLocation(workingPackageFolderPath));
326+
break;
327+
case ExtractorMessageLogLevel.Warning:
328+
logger.logWarning('Warning: ' + message.formatMessageAndLocation(workingPackageFolderPath));
329+
break;
330+
}
331+
}
332+
333+
return messagesForLogger;
334+
}
335+
301336
/**
302337
* For a given message, determine the IReportingRule based on the rule tables.
303338
*/
@@ -320,7 +355,20 @@ export class MessageRouter {
320355
* Sorts an array of messages according to a reasonable ordering
321356
*/
322357
private _sortMessagesForOutput(messages: ExtractorMessage[]): void {
323-
// Sort by filename, then by line number
324-
Sort.sortBy(messages, x => (x.sourceFilePath || '') + '#' + (x.sourceFileLine || ''));
358+
messages.sort((a, b) => {
359+
let diff: number;
360+
// First sort by file name
361+
diff = Sort.compareByValue(a.sourceFilePath, b.sourceFilePath);
362+
if (diff !== 0) {
363+
return diff;
364+
}
365+
// Then sort by line number
366+
diff = Sort.compareByValue(a.sourceFileLine, b.sourceFileLine);
367+
if (diff !== 0) {
368+
return diff;
369+
}
370+
// Then sort by messageId
371+
return Sort.compareByValue(a.messageId, b.messageId);
372+
});
325373
}
326374
}

0 commit comments

Comments
 (0)