Skip to content

Commit

Permalink
feat(tsfmt): fix many issue by @myitcv #24
Browse files Browse the repository at this point in the history
Merge branch 'myitcv-newline_and_indent_fixes'
  • Loading branch information
vvakame committed Sep 22, 2015
2 parents 5581713 + 5b46e5d commit d0f2719
Show file tree
Hide file tree
Showing 12 changed files with 130 additions and 10 deletions.
4 changes: 2 additions & 2 deletions lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,14 @@ function showResultHandler(resultMap: lib.ResultMap): Promise<any> {
Object.keys(resultMap)
.map(fileName => resultMap[fileName])
.filter(result => result.error)
.forEach(result => console.error(result.message));
.forEach(result => process.stderr.write(result.message));
process.exit(1);
} else {
Object.keys(resultMap)
.map(fileName => resultMap[fileName])
.forEach(result => {
if (result.message) {
console.log(result.message);
process.stdout.write(result.message);
}
});
}
Expand Down
18 changes: 14 additions & 4 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 editorconfig from "./provider/editorconfig";
import tslintjson from "./provider/tslintjson";
import tslintjson, {postProcess as tslintPostProcess} from "./provider/tslintjson";

export interface Options {
dryRun?: boolean;
Expand All @@ -21,6 +21,10 @@ export interface Options {
tsfmt: boolean;
}

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

export interface ResultMap {
[fileName: string]: Result;
}
Expand All @@ -43,7 +47,7 @@ export function processFiles(files: string[], opts: Options): Promise<ResultMap>
let result: Result = {
fileName: fileName,
options: null,
message: `${fileName} is not exists. process abort.`,
message: `${fileName} does not exist. process abort.\n`,
error: true,
src: "",
dest: ""
Expand Down Expand Up @@ -85,6 +89,7 @@ export function processString(fileName: string, content: string, opts: Options):

let formatOptions = createDefaultFormatCodeOptions();
let optGenPromises: (ts.FormatCodeOptions | Promise<ts.FormatCodeOptions>)[] = [];
let postProcesses: PostProcess[] = [];
if (opts.tsfmt) {
optGenPromises.push(base(fileName, opts, formatOptions));
}
Expand All @@ -93,6 +98,7 @@ export function processString(fileName: string, content: string, opts: Options):
}
if (opts.tslint) {
optGenPromises.push(tslintjson(fileName, opts, formatOptions));
postProcesses.push(tslintPostProcess);
}

return Promise
Expand All @@ -104,18 +110,22 @@ export function processString(fileName: string, content: string, opts: Options):
formattedCode += "\n";
}

postProcesses.forEach(postProcess => {
formattedCode = postProcess(fileName, formattedCode, opts, formatOptions) || formattedCode;
});

// TODO replace newline code. NewLineCharacter params affect to only "new" newline. maybe.
let message: string;
let error = false;
if (opts && opts.verify) {
if (content !== formattedCode) {
message = `${fileName} is not formatted`;
message = `${fileName} is not formatted\n`;
error = true;
}
} else if (opts && opts.replace) {
if (content !== formattedCode) {
fs.writeFileSync(fileName, formattedCode);
message = `replaced ${fileName}`;
message = `replaced ${fileName}\n`;
}
} else if (opts && !opts.dryRun) {
message = formattedCode;
Expand Down
45 changes: 42 additions & 3 deletions lib/provider/tslintjson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ interface TslintSettings {
rules: {
indent: {
0: boolean;
1: number;
1: string;
};
"no-consecutive-blank-lines": boolean,
whitespace: {
0: boolean;
1: string;
Expand All @@ -26,6 +27,10 @@ interface TslintSettings {
};
}

export interface AdditionalFormatOptions {
noConsecutiveBlankLines: boolean;
}

export default function makeFormatCodeOptions(fileName: string, opts: Options, formatOptions: ts.FormatCodeOptions): ts.FormatCodeOptions {
"use strict";

Expand All @@ -42,8 +47,8 @@ export default function makeFormatCodeOptions(fileName: string, opts: Options, f
if (!config.rules) {
return formatOptions;
}
if (config.rules.indent && config.rules.indent[0]) {
formatOptions.IndentSize = config.rules.indent[1];
if (config.rules.indent && config.rules.indent[0] && config.rules.indent[1] === "spaces") {
formatOptions.ConvertTabsToSpaces = true;
}
if (config.rules.whitespace && config.rules.whitespace[0]) {
for (let p in config.rules.whitespace) {
Expand All @@ -65,3 +70,37 @@ export default function makeFormatCodeOptions(fileName: string, opts: Options, f

return formatOptions;
}

export function postProcess(fileName: string, formattedCode: string, opts: Options, formatOptions: ts.FormatCodeOptions): string {
"use strict";

let baseDir = opts.baseDir ? path.resolve(opts.baseDir) : path.dirname(path.resolve(fileName));
let configFileName = getConfigFileName(baseDir, "tslint.json");
if (!configFileName) {
return formattedCode;
}

let config: TslintSettings = JSON.parse(<any>fs.readFileSync(configFileName, "utf-8"));
if (!config.rules) {
return formattedCode;
}

let additionalOptions = createDefaultAdditionalFormatCodeOptions();
if (config.rules["no-consecutive-blank-lines"] === true) {
additionalOptions.noConsecutiveBlankLines = true;
}

if (additionalOptions.noConsecutiveBlankLines) {
formattedCode = formattedCode.replace(/\n+^$/mg, "\n");
}

return formattedCode;
}

function createDefaultAdditionalFormatCodeOptions(): AdditionalFormatOptions {
"use strict";

return {
noConsecutiveBlankLines: false
};
}
15 changes: 15 additions & 0 deletions test/expected/tslint/no-consecutive-blank-lines/main.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"IndentSize": 4,
"TabSize": 4,
"NewLineCharacter": "\r\n",
"ConvertTabsToSpaces": true,
"InsertSpaceAfterCommaDelimiter": true,
"InsertSpaceAfterSemicolonInForStatements": true,
"InsertSpaceBeforeAndAfterBinaryOperators": true,
"InsertSpaceAfterKeywordsInControlFlowStatements": true,
"InsertSpaceAfterFunctionKeywordForAnonymousFunctions": false,
"InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false,
"InsertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": false,
"PlaceOpenBraceOnNewLineForFunctions": false,
"PlaceOpenBraceOnNewLineForControlBlocks": false
}
6 changes: 6 additions & 0 deletions test/expected/tslint/no-consecutive-blank-lines/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Sample {

test() {
return "";
}
}
15 changes: 15 additions & 0 deletions test/expected/tslint/no-trailing-whitespace/main.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"IndentSize": 4,
"TabSize": 4,
"NewLineCharacter": "\r\n",
"ConvertTabsToSpaces": true,
"InsertSpaceAfterCommaDelimiter": true,
"InsertSpaceAfterSemicolonInForStatements": true,
"InsertSpaceBeforeAndAfterBinaryOperators": true,
"InsertSpaceAfterKeywordsInControlFlowStatements": true,
"InsertSpaceAfterFunctionKeywordForAnonymousFunctions": false,
"InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false,
"InsertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": false,
"PlaceOpenBraceOnNewLineForFunctions": false,
"PlaceOpenBraceOnNewLineForControlBlocks": false
}
5 changes: 5 additions & 0 deletions test/expected/tslint/no-trailing-whitespace/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Sample {
test() {
return "";
}
}
15 changes: 15 additions & 0 deletions test/fixture/tslint/no-consecutive-blank-lines/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Sample {




test() {
return "";
}
}






5 changes: 5 additions & 0 deletions test/fixture/tslint/no-consecutive-blank-lines/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"no-consecutive-blank-lines": true
}
}
5 changes: 5 additions & 0 deletions test/fixture/tslint/no-trailing-whitespace/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Sample {
test() {
return "";
}
}
5 changes: 5 additions & 0 deletions test/fixture/tslint/no-trailing-whitespace/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"no-trailing-whitespace": true
}
}
2 changes: 1 addition & 1 deletion test/indexSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ describe("tsfmt test", () => {
})
.then(resultMap => {
assert(resultMap[fileName].error);
assert(resultMap[fileName].message === "./test/fixture/tsfmt/a/main.ts is not formatted");
assert(resultMap[fileName].message === "./test/fixture/tsfmt/a/main.ts is not formatted\n");
});
});
});
Expand Down

0 comments on commit d0f2719

Please sign in to comment.