Skip to content

Commit dbbe444

Browse files
committed
Implement the addToApiReviewFile reporting
1 parent 9e12c4b commit dbbe444

6 files changed

Lines changed: 285 additions & 53 deletions

File tree

‎apps/api-extractor/src/analyzer/Span.ts‎

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -287,21 +287,40 @@ export class Span {
287287

288288
/**
289289
* Starting from the first character of this span, walk backwards until we find the start of the line,
290-
* and return that whitespace.
290+
* and return whitespace after that position.
291+
*
292+
* @remarks
293+
* For example, suppose the character buffer contains this text:
294+
* ```
295+
* 1 2
296+
* 012345 6 7890123456789012345
297+
* "line 1\r\n line 2 Example"
298+
* ```
299+
*
300+
* And suppose the span starts at index 17, i.e. the the "E" in example. The `getIndent()` method would return
301+
* two spaces corresponding to the range from index 8 through and including index 9.
291302
*/
292303
public getIndent(): string {
293304
const buffer: string = this.node.getSourceFile().text;
294-
let lineStartIndex: number = this.startIndex;
295305

296-
while (lineStartIndex > 0) {
297-
const c: number = buffer.charCodeAt(lineStartIndex - 1);
298-
if (c !== 32 /* space */ && c !== 9 /* tab */) {
306+
let lineStartIndex: number = 0;
307+
let firstNonSpaceIndex: number = this.startIndex;
308+
309+
let i: number = this.startIndex - 1;
310+
while (i >= 0) {
311+
const c: number = buffer.charCodeAt(i);
312+
if (c === 13 /* \r */ || c === 10 /* \n */) {
313+
lineStartIndex = i + 1;
299314
break;
300315
}
301-
--lineStartIndex;
316+
if (c !== 32 /* space */ && c !== 9 /* tab */) {
317+
// We encountered a non-spacing character, so move the contentStartIndex backwards
318+
firstNonSpaceIndex = i;
319+
}
320+
--i;
302321
}
303322

304-
return buffer.substring(lineStartIndex, this.startIndex);
323+
return buffer.substring(lineStartIndex, firstNonSpaceIndex);
305324
}
306325

307326
/**

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
// See LICENSE in the project root for license information.
33

44
import * as tsdoc from '@microsoft/tsdoc';
5+
import * as path from 'path';
56
import { ExtractorMessageId } from './ExtractorMessageId';
7+
import { Path, Text } from '@microsoft/node-core-library';
68

79
/**
810
* Specifies a category of messages for use with {@link ExtractorMessage}.
@@ -97,4 +99,50 @@ export class ExtractorMessage {
9799
this.sourceFileLine = options.sourceFileLine;
98100
this.sourceFileColumn = options.sourceFileColumn;
99101
}
102+
103+
/**
104+
* Returns the message formatted with its identifier and file position.
105+
* @remarks
106+
* Example:
107+
* ```
108+
* src/folder/File.ts:123:4 - (ae-extra-release-tag) The doc comment should not contain more than one release tag.
109+
* ```
110+
*/
111+
public formatMessageAndLocation(workingPackageFolderPath: string): string {
112+
let result: string = '';
113+
114+
if (this.sourceFilePath) {
115+
// Make the path relative to the workingPackageFolderPath
116+
let scrubbedPath: string = this.sourceFilePath;
117+
118+
// If it's under the working folder, make it a relative path
119+
if (Path.isUnderOrEqual(this.sourceFilePath, workingPackageFolderPath)) {
120+
scrubbedPath = path.relative(workingPackageFolderPath, this.sourceFilePath);
121+
}
122+
123+
// Convert it to a Unix-style path
124+
scrubbedPath = Text.replaceAll(scrubbedPath, '\\', '/');
125+
result += scrubbedPath;
126+
127+
if (this.sourceFileLine) {
128+
result += `:${this.sourceFileLine}`;
129+
130+
if (this.sourceFileColumn) {
131+
result += `:${this.sourceFileColumn}`;
132+
}
133+
}
134+
135+
if (result.length > 0) {
136+
result += ' - ';
137+
}
138+
}
139+
140+
result += this.formatMessageWithoutLocation();
141+
142+
return result;
143+
}
144+
145+
public formatMessageWithoutLocation(): string {
146+
return `(${this.messageId}) ${this.text}`;
147+
}
100148
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ export class Collector {
636636

637637
const parserContext: tsdoc.ParserContext = this._tsdocParser.parseRange(tsdocTextRange);
638638

639-
this.messageRouter.addTsdocMessages(parserContext, declaration.getSourceFile());
639+
this.messageRouter.addTsdocMessages(parserContext, declaration.getSourceFile(), astDeclaration);
640640

641641
return parserContext;
642642
}

0 commit comments

Comments
 (0)