Skip to content

Commit d0f2719

Browse files
committed
feat(tsfmt): fix many issue by @myitcv #24
Merge branch 'myitcv-newline_and_indent_fixes'
2 parents 5581713 + 5b46e5d commit d0f2719

File tree

12 files changed

+130
-10
lines changed

12 files changed

+130
-10
lines changed

lib/cli.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,14 @@ function showResultHandler(resultMap: lib.ResultMap): Promise<any> {
124124
Object.keys(resultMap)
125125
.map(fileName => resultMap[fileName])
126126
.filter(result => result.error)
127-
.forEach(result => console.error(result.message));
127+
.forEach(result => process.stderr.write(result.message));
128128
process.exit(1);
129129
} else {
130130
Object.keys(resultMap)
131131
.map(fileName => resultMap[fileName])
132132
.forEach(result => {
133133
if (result.message) {
134-
console.log(result.message);
134+
process.stdout.write(result.message);
135135
}
136136
});
137137
}

lib/index.ts

+14-4
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 editorconfig from "./provider/editorconfig";
11-
import tslintjson from "./provider/tslintjson";
11+
import tslintjson, {postProcess as tslintPostProcess} from "./provider/tslintjson";
1212

1313
export interface Options {
1414
dryRun?: boolean;
@@ -21,6 +21,10 @@ export interface Options {
2121
tsfmt: boolean;
2222
}
2323

24+
export interface PostProcess {
25+
(fileName: string, formattedCode: string, opts: Options, formatOptions: ts.FormatCodeOptions): string;
26+
}
27+
2428
export interface ResultMap {
2529
[fileName: string]: Result;
2630
}
@@ -43,7 +47,7 @@ export function processFiles(files: string[], opts: Options): Promise<ResultMap>
4347
let result: Result = {
4448
fileName: fileName,
4549
options: null,
46-
message: `${fileName} is not exists. process abort.`,
50+
message: `${fileName} does not exist. process abort.\n`,
4751
error: true,
4852
src: "",
4953
dest: ""
@@ -85,6 +89,7 @@ export function processString(fileName: string, content: string, opts: Options):
8589

8690
let formatOptions = createDefaultFormatCodeOptions();
8791
let optGenPromises: (ts.FormatCodeOptions | Promise<ts.FormatCodeOptions>)[] = [];
92+
let postProcesses: PostProcess[] = [];
8893
if (opts.tsfmt) {
8994
optGenPromises.push(base(fileName, opts, formatOptions));
9095
}
@@ -93,6 +98,7 @@ export function processString(fileName: string, content: string, opts: Options):
9398
}
9499
if (opts.tslint) {
95100
optGenPromises.push(tslintjson(fileName, opts, formatOptions));
101+
postProcesses.push(tslintPostProcess);
96102
}
97103

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

113+
postProcesses.forEach(postProcess => {
114+
formattedCode = postProcess(fileName, formattedCode, opts, formatOptions) || formattedCode;
115+
});
116+
107117
// TODO replace newline code. NewLineCharacter params affect to only "new" newline. maybe.
108118
let message: string;
109119
let error = false;
110120
if (opts && opts.verify) {
111121
if (content !== formattedCode) {
112-
message = `${fileName} is not formatted`;
122+
message = `${fileName} is not formatted\n`;
113123
error = true;
114124
}
115125
} else if (opts && opts.replace) {
116126
if (content !== formattedCode) {
117127
fs.writeFileSync(fileName, formattedCode);
118-
message = `replaced ${fileName}`;
128+
message = `replaced ${fileName}\n`;
119129
}
120130
} else if (opts && !opts.dryRun) {
121131
message = formattedCode;

lib/provider/tslintjson.ts

+42-3
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ interface TslintSettings {
1212
rules: {
1313
indent: {
1414
0: boolean;
15-
1: number;
15+
1: string;
1616
};
17+
"no-consecutive-blank-lines": boolean,
1718
whitespace: {
1819
0: boolean;
1920
1: string;
@@ -26,6 +27,10 @@ interface TslintSettings {
2627
};
2728
}
2829

30+
export interface AdditionalFormatOptions {
31+
noConsecutiveBlankLines: boolean;
32+
}
33+
2934
export default function makeFormatCodeOptions(fileName: string, opts: Options, formatOptions: ts.FormatCodeOptions): ts.FormatCodeOptions {
3035
"use strict";
3136

@@ -42,8 +47,8 @@ export default function makeFormatCodeOptions(fileName: string, opts: Options, f
4247
if (!config.rules) {
4348
return formatOptions;
4449
}
45-
if (config.rules.indent && config.rules.indent[0]) {
46-
formatOptions.IndentSize = config.rules.indent[1];
50+
if (config.rules.indent && config.rules.indent[0] && config.rules.indent[1] === "spaces") {
51+
formatOptions.ConvertTabsToSpaces = true;
4752
}
4853
if (config.rules.whitespace && config.rules.whitespace[0]) {
4954
for (let p in config.rules.whitespace) {
@@ -65,3 +70,37 @@ export default function makeFormatCodeOptions(fileName: string, opts: Options, f
6570

6671
return formatOptions;
6772
}
73+
74+
export function postProcess(fileName: string, formattedCode: string, opts: Options, formatOptions: ts.FormatCodeOptions): string {
75+
"use strict";
76+
77+
let baseDir = opts.baseDir ? path.resolve(opts.baseDir) : path.dirname(path.resolve(fileName));
78+
let configFileName = getConfigFileName(baseDir, "tslint.json");
79+
if (!configFileName) {
80+
return formattedCode;
81+
}
82+
83+
let config: TslintSettings = JSON.parse(<any>fs.readFileSync(configFileName, "utf-8"));
84+
if (!config.rules) {
85+
return formattedCode;
86+
}
87+
88+
let additionalOptions = createDefaultAdditionalFormatCodeOptions();
89+
if (config.rules["no-consecutive-blank-lines"] === true) {
90+
additionalOptions.noConsecutiveBlankLines = true;
91+
}
92+
93+
if (additionalOptions.noConsecutiveBlankLines) {
94+
formattedCode = formattedCode.replace(/\n+^$/mg, "\n");
95+
}
96+
97+
return formattedCode;
98+
}
99+
100+
function createDefaultAdditionalFormatCodeOptions(): AdditionalFormatOptions {
101+
"use strict";
102+
103+
return {
104+
noConsecutiveBlankLines: false
105+
};
106+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"IndentSize": 4,
3+
"TabSize": 4,
4+
"NewLineCharacter": "\r\n",
5+
"ConvertTabsToSpaces": true,
6+
"InsertSpaceAfterCommaDelimiter": true,
7+
"InsertSpaceAfterSemicolonInForStatements": true,
8+
"InsertSpaceBeforeAndAfterBinaryOperators": true,
9+
"InsertSpaceAfterKeywordsInControlFlowStatements": true,
10+
"InsertSpaceAfterFunctionKeywordForAnonymousFunctions": false,
11+
"InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false,
12+
"InsertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": false,
13+
"PlaceOpenBraceOnNewLineForFunctions": false,
14+
"PlaceOpenBraceOnNewLineForControlBlocks": false
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Sample {
2+
3+
test() {
4+
return "";
5+
}
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"IndentSize": 4,
3+
"TabSize": 4,
4+
"NewLineCharacter": "\r\n",
5+
"ConvertTabsToSpaces": true,
6+
"InsertSpaceAfterCommaDelimiter": true,
7+
"InsertSpaceAfterSemicolonInForStatements": true,
8+
"InsertSpaceBeforeAndAfterBinaryOperators": true,
9+
"InsertSpaceAfterKeywordsInControlFlowStatements": true,
10+
"InsertSpaceAfterFunctionKeywordForAnonymousFunctions": false,
11+
"InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false,
12+
"InsertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": false,
13+
"PlaceOpenBraceOnNewLineForFunctions": false,
14+
"PlaceOpenBraceOnNewLineForControlBlocks": false
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Sample {
2+
test() {
3+
return "";
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Sample {
2+
3+
4+
5+
6+
test() {
7+
return "";
8+
}
9+
}
10+
11+
12+
13+
14+
15+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"rules": {
3+
"no-consecutive-blank-lines": true
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Sample {
2+
test() {
3+
return "";
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"rules": {
3+
"no-trailing-whitespace": true
4+
}
5+
}

test/indexSpec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ describe("tsfmt test", () => {
173173
})
174174
.then(resultMap => {
175175
assert(resultMap[fileName].error);
176-
assert(resultMap[fileName].message === "./test/fixture/tsfmt/a/main.ts is not formatted");
176+
assert(resultMap[fileName].message === "./test/fixture/tsfmt/a/main.ts is not formatted\n");
177177
});
178178
});
179179
});

0 commit comments

Comments
 (0)