Skip to content

Commit a62c576

Browse files
committed
PR feedback
1 parent b9466ac commit a62c576

File tree

9 files changed

+30
-44
lines changed

9 files changed

+30
-44
lines changed

apps/api-extractor/src/analyzer/PackageMetadataManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export class PackageMetadataManager {
132132
/**
133133
* Writes the TSDoc metadata file to the specified output file.
134134
*/
135-
public static writeTsdocMetadataFile(tsdocMetadataPath: string, newlineKind: NewlineKind = NewlineKind.CrLf): void {
135+
public static writeTsdocMetadataFile(tsdocMetadataPath: string, newlineKind: NewlineKind): void {
136136
const fileObject: JsonObject = {
137137
tsdocVersion: '0.12',
138138
toolPackages: [

apps/api-extractor/src/analyzer/test/PackageMetadataManager.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
import * as path from 'path';
33
import { PackageMetadataManager } from '../PackageMetadataManager';
4-
import { FileSystem, PackageJsonLookup, INodePackageJson } from '@microsoft/node-core-library';
4+
import { FileSystem, PackageJsonLookup, INodePackageJson, NewlineKind } from '@microsoft/node-core-library';
55

66
const packageJsonLookup: PackageJsonLookup = new PackageJsonLookup();
77

@@ -40,7 +40,7 @@ describe('PackageMetadataManager', () => {
4040
});
4141

4242
it('writes the tsdoc metadata file at the provided path', () => {
43-
PackageMetadataManager.writeTsdocMetadataFile('/foo/bar');
43+
PackageMetadataManager.writeTsdocMetadataFile('/foo/bar', NewlineKind.CrLf);
4444
expect(firstArgument(mockWriteFile)).toBe('/foo/bar');
4545
});
4646
});

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,8 @@ export class Extractor {
332332

333333
if (extractorConfig.tsdocMetadataEnabled) {
334334
// Write the tsdoc-metadata.json file for this project
335-
PackageMetadataManager.writeTsdocMetadataFile(extractorConfig.tsdocMetadataFilePath);
335+
PackageMetadataManager.writeTsdocMetadataFile(
336+
extractorConfig.tsdocMetadataFilePath, extractorConfig.newlineKind);
336337
}
337338

338339
// Show all the messages that we collected during analysis

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import { StringWriter } from './StringWriter';
1818
import { DtsEmitHelpers } from './DtsEmitHelpers';
1919

2020
export class ApiReportGenerator {
21+
private static _TrimSpacesRegExp: RegExp = / +$/gm;
22+
2123
/**
2224
* Compares the contents of two API files that were created using ApiFileGenerator,
2325
* and returns true if they are equivalent. Note that these files are not normally edited
@@ -155,7 +157,7 @@ export class ApiReportGenerator {
155157
stringWriter.writeLine('\n```');
156158

157159
// Remove any trailing spaces
158-
return stringWriter.toString().replace(/ +$/gm, '');
160+
return stringWriter.toString().replace(ApiReportGenerator._TrimSpacesRegExp, '');
159161
}
160162

161163
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export class DtsRollupGenerator {
5151
* @param dtsFilename - The *.d.ts output filename
5252
*/
5353
public static writeTypingsFile(
54-
collector: Collector, dtsFilename: string, dtsKind: DtsRollupKind, newlineKind: NewlineKind = NewlineKind.CrLf
54+
collector: Collector, dtsFilename: string, dtsKind: DtsRollupKind, newlineKind: NewlineKind
5555
): void {
5656
const stringWriter: StringWriter = new StringWriter();
5757

common/reviews/api/node-core-library.api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,9 +537,9 @@ export enum TerminalProviderSeverity {
537537

538538
// @public
539539
export class Text {
540+
static convertTo(input: string, newlineKind: NewlineKind): string;
540541
static convertToCrLf(input: string): string;
541542
static convertToLf(input: string): string;
542-
static convertToOsDefault(input: string): string;
543543
static ensureTrailingNewline(s: string, newlineKind?: NewlineKind): string;
544544
static padEnd(s: string, minimumLength: number, paddingCharacter?: string): string;
545545
static padStart(s: string, minimumLength: number, paddingCharacter?: string): string;

libraries/node-core-library/src/FileSystem.ts

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { PosixModeBits } from './PosixModeBits';
1818
export interface IFileSystemReadFolderOptions {
1919
/**
2020
* If true, returns the absolute paths of the files in the folder.
21-
* Defaults to `false`.
21+
* @defaultValue false
2222
*/
2323
absolutePaths?: boolean;
2424
}
@@ -36,13 +36,13 @@ export interface IFileSystemWriteFileOptions {
3636

3737
/**
3838
* If specified, will normalize line endings to the specified style of newline.
39-
* @defaultValue undefined
39+
* @defaultValue `undefined` which means no conversion will be performed
4040
*/
4141
convertLineEndings?: NewlineKind;
4242

4343
/**
4444
* If specified, will change the encoding of the file that will be written.
45-
* Defaults to `"utf8"`.
45+
* @defaultValue "utf8"
4646
*/
4747
encoding?: Encoding;
4848
}
@@ -60,7 +60,7 @@ export interface IFileSystemReadFileOptions {
6060

6161
/**
6262
* If specified, will normalize line endings to the specified style of newline.
63-
* @defaultValue undefined
63+
* @defaultValue `undefined` which means no conversion will be performed
6464
*/
6565
convertLineEndings?: NewlineKind;
6666
}
@@ -370,7 +370,9 @@ export class FileSystem {
370370
FileSystem.ensureFolder(folderPath);
371371
}
372372

373-
contents = FileSystem._convertLineEndings(contents.toString(), options.convertLineEndings);
373+
if (options.convertLineEndings) {
374+
contents = Text.convertTo(contents.toString(), options.convertLineEndings);
375+
}
374376

375377
fsx.writeFileSync(filePath, contents, { encoding: options.encoding });
376378
}
@@ -397,7 +399,9 @@ export class FileSystem {
397399
FileSystem.ensureFolder(folderPath);
398400
}
399401

400-
contents = FileSystem._convertLineEndings(contents.toString(), options.convertLineEndings);
402+
if (options.convertLineEndings) {
403+
contents = Text.convertTo(contents.toString(), options.convertLineEndings);
404+
}
401405

402406
fsx.appendFileSync(filePath, contents, { encoding: options.encoding });
403407
}
@@ -415,8 +419,11 @@ export class FileSystem {
415419
...options
416420
};
417421

418-
const contents: string = FileSystem.readFileToBuffer(filePath).toString(options.encoding);
419-
return FileSystem._convertLineEndings(contents, options.convertLineEndings);
422+
let contents: string = FileSystem.readFileToBuffer(filePath).toString(options.encoding);
423+
if (options.convertLineEndings) {
424+
contents = Text.convertTo(contents, options.convertLineEndings);
425+
}
426+
return contents;
420427
}
421428

422429
/**
@@ -514,22 +521,4 @@ export class FileSystem {
514521
public static getRealPath(linkPath: string): string {
515522
return fsx.realpathSync(linkPath);
516523
}
517-
518-
/**
519-
* A helper function that converts line endings on a string.
520-
* @param text - The text to be normalized.
521-
* @param lineEndings - The style of line endings to use.
522-
*/
523-
private static _convertLineEndings(text: string, lineEndings: NewlineKind | undefined): string {
524-
switch (lineEndings) {
525-
case NewlineKind.CrLf:
526-
return Text.convertToCrLf(text);
527-
case NewlineKind.Lf:
528-
return Text.convertToLf(text);
529-
case NewlineKind.OsDefault:
530-
return Text.convertToOsDefault(text);
531-
default:
532-
return text;
533-
}
534-
}
535524
}

libraries/node-core-library/src/JsonFile.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,7 @@ export class JsonFile {
157157
stringified = Text.ensureTrailingNewline(stringified);
158158

159159
if (options && options.newlineConversion) {
160-
switch (options.newlineConversion) {
161-
case NewlineKind.CrLf:
162-
return Text.convertToCrLf(stringified);
163-
case NewlineKind.Lf:
164-
return Text.convertToLf(stringified);
165-
case NewlineKind.OsDefault:
166-
return Text.convertToOsDefault(stringified);
167-
}
160+
stringified = Text.convertTo(stringified, options.newlineConversion);
168161
}
169162

170163
return stringified;

libraries/node-core-library/src/Text.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,11 @@ export class Text {
7676
}
7777

7878
/**
79-
* Converts all newlines in the provided string to use the default newline type for this operating system.
79+
* Converts all newlines in the provided string to use the specified newline type.
8080
*/
81-
public static convertToOsDefault(input: string): string {
82-
return input.replace(Text._newLineRegEx, os.EOL);
81+
public static convertTo(input: string, newlineKind: NewlineKind): string {
82+
const newline: string = newlineKind === NewlineKind.OsDefault ? os.EOL : newlineKind as string;
83+
return input.replace(Text._newLineRegEx, newline);
8384
}
8485

8586
/**

0 commit comments

Comments
 (0)