Skip to content

Commit 5c343f0

Browse files
authored
Merge pull request microsoft#1167 from Microsoft/octogonz/ae-stringwriter
[api-extractor] Refactor code using StringWriter
2 parents 03ce8e9 + 971fa54 commit 5c343f0

4 files changed

Lines changed: 63 additions & 47 deletions

File tree

apps/api-extractor/src/generators/DtsRollupGenerator.ts

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
/* tslint:disable:no-bitwise */
55

66
import * as ts from 'typescript';
7-
import { FileSystem, NewlineKind, InternalError, StringBuilder } from '@microsoft/node-core-library';
7+
import { FileSystem, NewlineKind, InternalError } from '@microsoft/node-core-library';
88
import { ReleaseTag } from '@microsoft/api-extractor-model';
99

1010
import { Collector } from '../collector/Collector';
@@ -16,6 +16,7 @@ import { AstDeclaration } from '../analyzer/AstDeclaration';
1616
import { DeclarationMetadata } from '../collector/DeclarationMetadata';
1717
import { AstSymbol } from '../analyzer/AstSymbol';
1818
import { SymbolMetadata } from '../collector/SymbolMetadata';
19+
import { StringWriter } from './StringWriter';
1920

2021
/**
2122
* Used with DtsRollupGenerator.writeTypingsFile()
@@ -42,26 +43,6 @@ export enum DtsRollupKind {
4243
PublicRelease
4344
}
4445

45-
// A small helper used by DtsRollupGenerator
46-
class StringWriter extends StringBuilder {
47-
public readonly stringBuilder: StringBuilder = new StringBuilder();
48-
49-
public write(s: string): void {
50-
this.stringBuilder.append(s);
51-
}
52-
53-
public writeLine(s: string = ''): void {
54-
if (s.length > 0) {
55-
this.stringBuilder.append(s);
56-
}
57-
this.stringBuilder.append('\n');
58-
}
59-
60-
public toString(): string {
61-
return this.stringBuilder.toString();
62-
}
63-
}
64-
6546
export class DtsRollupGenerator {
6647
/**
6748
* Generates the typings file and writes it to disk.

apps/api-extractor/src/generators/ReviewFileGenerator.ts

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ import { TypeScriptHelpers } from '../analyzer/TypeScriptHelpers';
1010
import { Span } from '../analyzer/Span';
1111
import { CollectorEntity } from '../collector/CollectorEntity';
1212
import { AstDeclaration } from '../analyzer/AstDeclaration';
13-
import { StringBuilder } from '@microsoft/tsdoc';
1413
import { DeclarationMetadata } from '../collector/DeclarationMetadata';
1514
import { SymbolMetadata } from '../collector/SymbolMetadata';
1615
import { AstImport } from '../analyzer/AstImport';
1716
import { AstSymbol } from '../analyzer/AstSymbol';
1817
import { ExtractorMessage } from '../api/ExtractorMessage';
18+
import { StringWriter } from './StringWriter';
1919

2020
export class ReviewFileGenerator {
2121
/**
@@ -33,30 +33,30 @@ export class ReviewFileGenerator {
3333
}
3434

3535
public static generateReviewFileContent(collector: Collector): string {
36-
const output: StringBuilder = new StringBuilder();
36+
const stringWriter: StringWriter = new StringWriter();
3737

38-
output.append([
38+
stringWriter.writeLine([
3939
`## API Review File for "${collector.workingPackage.name}"`,
4040
``,
4141
`> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/).`,
4242
``
4343
].join('\n'));
4444

4545
// Write the opening delimiter for the Markdown code fence
46-
output.append('\n```ts\n\n');
46+
stringWriter.writeLine('```ts\n');
4747

4848
for (const entity of collector.entities) {
4949
if (entity.exported) {
5050
if (entity.astEntity instanceof AstSymbol) {
5151
// Emit all the declarations for this entry
5252
for (const astDeclaration of entity.astEntity.astDeclarations || []) {
5353

54-
output.append(ReviewFileGenerator._getAedocSynopsis(collector, astDeclaration));
54+
stringWriter.write(ReviewFileGenerator._getAedocSynopsis(collector, astDeclaration));
5555

5656
const span: Span = new Span(astDeclaration.declaration);
5757
ReviewFileGenerator._modifySpan(collector, span, entity, astDeclaration, false);
58-
span.writeModifiedText(output);
59-
output.append('\n\n');
58+
span.writeModifiedText(stringWriter.stringBuilder);
59+
stringWriter.writeLine('\n');
6060
}
6161
} else {
6262
// This definition is reexported from another package, so write it as an "export" line
@@ -65,47 +65,47 @@ export class ReviewFileGenerator {
6565
const astImport: AstImport = entity.astEntity;
6666

6767
if (astImport.exportName === '*') {
68-
output.append(`export * as ${entity.nameForEmit}`);
68+
stringWriter.write(`export * as ${entity.nameForEmit}`);
6969
} else if (entity.nameForEmit !== astImport.exportName) {
70-
output.append(`export { ${astImport.exportName} as ${entity.nameForEmit} }`);
70+
stringWriter.write(`export { ${astImport.exportName} as ${entity.nameForEmit} }`);
7171
} else {
72-
output.append(`export { ${astImport.exportName} }`);
72+
stringWriter.write(`export { ${astImport.exportName} }`);
7373
}
74-
output.append(` from '${astImport.modulePath}';\n`);
74+
stringWriter.writeLine(` from '${astImport.modulePath}';`);
7575
}
7676
}
7777
}
7878

7979
if (collector.starExportedExternalModulePaths.length > 0) {
80-
output.append('\n');
80+
stringWriter.writeLine();
8181
for (const starExportedExternalModulePath of collector.starExportedExternalModulePaths) {
82-
output.append(`export * from "${starExportedExternalModulePath}";\n`);
82+
stringWriter.writeLine(`export * from "${starExportedExternalModulePath}";`);
8383
}
8484
}
8585

8686
// Write the unassociated warnings at the bottom of the file
8787
const unassociatedMessages: ExtractorMessage[] = collector.messageRouter
8888
.fetchUnassociatedMessagesForReviewFile();
8989
if (unassociatedMessages.length > 0) {
90-
output.append('\n');
91-
ReviewFileGenerator._writeLineAsComments(output, 'Warnings were encountered during analysis:');
92-
ReviewFileGenerator._writeLineAsComments(output, '');
90+
stringWriter.writeLine();
91+
ReviewFileGenerator._writeLineAsComments(stringWriter, 'Warnings were encountered during analysis:');
92+
ReviewFileGenerator._writeLineAsComments(stringWriter, '');
9393
for (const unassociatedMessage of unassociatedMessages) {
94-
ReviewFileGenerator._writeLineAsComments(output, unassociatedMessage.formatMessageWithLocation(
94+
ReviewFileGenerator._writeLineAsComments(stringWriter, unassociatedMessage.formatMessageWithLocation(
9595
collector.workingPackage.packageFolder
9696
));
9797
}
9898
}
9999

100100
if (collector.workingPackage.tsdocComment === undefined) {
101-
output.append('\n');
102-
ReviewFileGenerator._writeLineAsComments(output, '(No @packageDocumentation comment for this package)');
101+
stringWriter.writeLine();
102+
ReviewFileGenerator._writeLineAsComments(stringWriter, '(No @packageDocumentation comment for this package)');
103103
}
104104

105105
// Write the closing delimiter for the Markdown code fence
106-
output.append('\n```\n');
106+
stringWriter.writeLine('\n```');
107107

108-
return output.toString();
108+
return stringWriter.toString();
109109
}
110110

111111
/**
@@ -230,7 +230,7 @@ export class ReviewFileGenerator {
230230
* by the analysis.
231231
*/
232232
private static _getAedocSynopsis(collector: Collector, astDeclaration: AstDeclaration): string {
233-
const output: StringBuilder = new StringBuilder();
233+
const output: StringWriter = new StringWriter();
234234

235235
const messagesToReport: ExtractorMessage[] = collector.messageRouter
236236
.fetchAssociatedMessagesForReviewFile(astDeclaration);
@@ -287,12 +287,12 @@ export class ReviewFileGenerator {
287287
return output.toString();
288288
}
289289

290-
private static _writeLineAsComments(output: StringBuilder, line: string): void {
290+
private static _writeLineAsComments(stringWriter: StringWriter, line: string): void {
291291
const lines: string[] = Text.convertToLf(line).split('\n');
292292
for (const realLine of lines) {
293-
output.append('// ');
294-
output.append(realLine);
295-
output.append('\n');
293+
stringWriter.write('// ');
294+
stringWriter.write(realLine);
295+
stringWriter.writeLine();
296296
}
297297
}
298298

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
2+
// See LICENSE in the project root for license information.
3+
4+
import { StringBuilder } from '@microsoft/tsdoc';
5+
6+
// A small helper used by the generators
7+
export class StringWriter {
8+
public readonly stringBuilder: StringBuilder = new StringBuilder();
9+
10+
public write(s: string): void {
11+
this.stringBuilder.append(s);
12+
}
13+
14+
public writeLine(s: string = ''): void {
15+
if (s.length > 0) {
16+
this.stringBuilder.append(s);
17+
}
18+
this.stringBuilder.append('\n');
19+
}
20+
21+
public toString(): string {
22+
return this.stringBuilder.toString();
23+
}
24+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"comment": "",
5+
"packageName": "@microsoft/api-extractor",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@microsoft/api-extractor",
10+
"email": "4673363+octogonz@users.noreply.github.com"
11+
}

0 commit comments

Comments
 (0)