Skip to content

Commit 2df1f7a

Browse files
jriekenvvakame
authored andcommitted
feat(tsfmt): move final newline character logic to editorconfig part
1 parent 2b9ed27 commit 2df1f7a

File tree

4 files changed

+46
-12
lines changed

4 files changed

+46
-12
lines changed

lib/index.ts

+25-9
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import * as fs from "fs";
88

99
import base from "./provider/base";
1010
import tsconfigjson from "./provider/tsconfigjson";
11-
import editorconfig from "./provider/editorconfig";
11+
import editorconfig, { postProcess as editorconfigPostProcess } from "./provider/editorconfig";
1212
import tslintjson, { postProcess as tslintPostProcess } from "./provider/tslintjson";
1313

1414
export interface Options {
@@ -24,7 +24,26 @@ export interface Options {
2424
}
2525

2626
export interface PostProcess {
27-
(fileName: string, formattedCode: string, opts: Options, formatOptions: ts.FormatCodeOptions): string;
27+
(fileName: string, formattedCode: string, opts: Options, formatOptions: ts.FormatCodeOptions): Promise<string> | string;
28+
}
29+
30+
export class PostProcess {
31+
32+
static sequence(all: PostProcess[]): PostProcess {
33+
34+
let index = 0;
35+
36+
function next(fileName: string, formattedCode: string, opts: Options, formatOptions: ts.FormatCodeOptions): Promise<string> | string {
37+
if (index < all.length) {
38+
return Promise.resolve(all[index++](fileName, formattedCode, opts, formatOptions)).then(newFormattedCode => {
39+
return next(fileName, newFormattedCode || formattedCode, opts, formatOptions);
40+
});
41+
}
42+
return formattedCode;
43+
};
44+
45+
return next;
46+
}
2847
}
2948

3049
export interface ResultMap {
@@ -97,6 +116,7 @@ export function processString(fileName: string, content: string, opts: Options):
97116
}
98117
if (opts.editorconfig) {
99118
optGenPromises.push(editorconfig(fileName, opts, formatOptions));
119+
postProcesses.push(editorconfigPostProcess);
100120
}
101121
if (opts.tslint) {
102122
optGenPromises.push(tslintjson(fileName, opts, formatOptions));
@@ -107,15 +127,11 @@ export function processString(fileName: string, content: string, opts: Options):
107127
.all(optGenPromises)
108128
.then(() => {
109129
let formattedCode = formatter(fileName, content, formatOptions);
110-
if ((<any>formattedCode).trimRight) {
111-
formattedCode = (<any>formattedCode).trimRight();
112-
formattedCode += formatOptions.NewLineCharacter;
113-
}
114130

115-
postProcesses.forEach(postProcess => {
116-
formattedCode = postProcess(fileName, formattedCode, opts, formatOptions) || formattedCode;
117-
});
131+
// apply post process logic
132+
return PostProcess.sequence(postProcesses)(fileName, formattedCode, opts, formatOptions);
118133

134+
}).then(formattedCode => {
119135
// replace newline code. maybe NewLineCharacter params affect to only "new" newline by language service.
120136
formattedCode = formattedCode.replace(/\r?\n/g, formatOptions.NewLineCharacter);
121137

lib/provider/editorconfig.ts

+18
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,21 @@ export default function makeFormatCodeOptions(fileName: string, opts: Options, f
4343
return formatOptions;
4444
});
4545
}
46+
47+
48+
export function postProcess(fileName: string, formattedCode: string, opts: Options, formatOptions: ts.FormatCodeOptions): Promise<string> {
49+
50+
if (opts.verbose && opts.baseDir && !emitBaseDirWarning) {
51+
console.log("editorconfig is not supported baseDir options");
52+
emitBaseDirWarning = true;
53+
}
54+
55+
return editorconfig
56+
.parse(fileName)
57+
.then(config => {
58+
if (config.insert_final_newline) {
59+
return formattedCode.replace(/\s+$/, formatOptions.NewLineCharacter);
60+
}
61+
return formattedCode;
62+
});
63+
}

test/expected/tsfmt/b/main.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -378,5 +378,5 @@ class Vector/* /*
378378
*/ return rayTracer.render( defaultScene(),ctx,256,256 );/*
379379
*/}/*
380380
*//*
381-
*/exec();/* /*
382-
*/ */
381+
*/exec();/*
382+
*/

test/indexSpec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ describe("tsfmt test", () => {
196196
.then(result => {
197197
assert(result !== null);
198198
assert(result.error === false);
199-
assert(result.dest === "class Sample { getString(): string { return \"hi!\"; } }\r\n");
199+
assert(result.dest === "class Sample { getString(): string { return \"hi!\"; } }");
200200
});
201201
});
202202
});

0 commit comments

Comments
 (0)