Skip to content

Commit

Permalink
feat(tsfmt): move final newline character logic to editorconfig part
Browse files Browse the repository at this point in the history
  • Loading branch information
jrieken authored and vvakame committed Oct 9, 2016
1 parent 2b9ed27 commit 2df1f7a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 12 deletions.
34 changes: 25 additions & 9 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as fs from "fs";

import base from "./provider/base";
import tsconfigjson from "./provider/tsconfigjson";
import editorconfig from "./provider/editorconfig";
import editorconfig, { postProcess as editorconfigPostProcess } from "./provider/editorconfig";
import tslintjson, { postProcess as tslintPostProcess } from "./provider/tslintjson";

export interface Options {
Expand All @@ -24,7 +24,26 @@ export interface Options {
}

export interface PostProcess {
(fileName: string, formattedCode: string, opts: Options, formatOptions: ts.FormatCodeOptions): string;
(fileName: string, formattedCode: string, opts: Options, formatOptions: ts.FormatCodeOptions): Promise<string> | string;
}

export class PostProcess {

static sequence(all: PostProcess[]): PostProcess {

let index = 0;

function next(fileName: string, formattedCode: string, opts: Options, formatOptions: ts.FormatCodeOptions): Promise<string> | string {
if (index < all.length) {
return Promise.resolve(all[index++](fileName, formattedCode, opts, formatOptions)).then(newFormattedCode => {
return next(fileName, newFormattedCode || formattedCode, opts, formatOptions);
});
}
return formattedCode;
};

return next;
}
}

export interface ResultMap {
Expand Down Expand Up @@ -97,6 +116,7 @@ export function processString(fileName: string, content: string, opts: Options):
}
if (opts.editorconfig) {
optGenPromises.push(editorconfig(fileName, opts, formatOptions));
postProcesses.push(editorconfigPostProcess);
}
if (opts.tslint) {
optGenPromises.push(tslintjson(fileName, opts, formatOptions));
Expand All @@ -107,15 +127,11 @@ export function processString(fileName: string, content: string, opts: Options):
.all(optGenPromises)
.then(() => {
let formattedCode = formatter(fileName, content, formatOptions);
if ((<any>formattedCode).trimRight) {
formattedCode = (<any>formattedCode).trimRight();
formattedCode += formatOptions.NewLineCharacter;
}

postProcesses.forEach(postProcess => {
formattedCode = postProcess(fileName, formattedCode, opts, formatOptions) || formattedCode;
});
// apply post process logic
return PostProcess.sequence(postProcesses)(fileName, formattedCode, opts, formatOptions);

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

Expand Down
18 changes: 18 additions & 0 deletions lib/provider/editorconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,21 @@ export default function makeFormatCodeOptions(fileName: string, opts: Options, f
return formatOptions;
});
}


export function postProcess(fileName: string, formattedCode: string, opts: Options, formatOptions: ts.FormatCodeOptions): Promise<string> {

if (opts.verbose && opts.baseDir && !emitBaseDirWarning) {
console.log("editorconfig is not supported baseDir options");
emitBaseDirWarning = true;
}

return editorconfig
.parse(fileName)
.then(config => {
if (config.insert_final_newline) {
return formattedCode.replace(/\s+$/, formatOptions.NewLineCharacter);
}
return formattedCode;
});
}
4 changes: 2 additions & 2 deletions test/expected/tsfmt/b/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,5 +378,5 @@ class Vector/* /*
*/ return rayTracer.render( defaultScene(),ctx,256,256 );/*
*/}/*
*//*
*/exec();/* /*
*/ */
*/exec();/*
*/
2 changes: 1 addition & 1 deletion test/indexSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ describe("tsfmt test", () => {
.then(result => {
assert(result !== null);
assert(result.error === false);
assert(result.dest === "class Sample { getString(): string { return \"hi!\"; } }\r\n");
assert(result.dest === "class Sample { getString(): string { return \"hi!\"; } }");
});
});
});
Expand Down

0 comments on commit 2df1f7a

Please sign in to comment.