Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Newline fix and various tslint rule changes #24

Merged
merged 2 commits into from
Sep 22, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,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
22 changes: 18 additions & 4 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ export interface Result {
dest: string;
}

export interface AdditionalFormatOptions {
noConsecutiveBlankLines: boolean;
noTrailingWhitespace: boolean;
}

export function processFiles(files: string[], opts: Options): Promise<ResultMap> {
"use strict";

Expand All @@ -42,7 +47,7 @@ export function processFiles(files: string[], opts: Options): Promise<ResultMap>
var 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 @@ -83,6 +88,7 @@ export function processString(fileName: string, content: string, opts: Options):
"use strict";

var options = utils.createDefaultFormatCodeOptions();
var additionalOptions = utils.createDefaultAdditionalFormatCodeOptions();
var optGenPromises: (ts.FormatCodeOptions | Promise<ts.FormatCodeOptions>)[] = [];
if (opts.tsfmt) {
optGenPromises.push(base.makeFormatCodeOptions(fileName, options));
Expand All @@ -91,7 +97,7 @@ export function processString(fileName: string, content: string, opts: Options):
optGenPromises.push(editorconfig.makeFormatCodeOptions(fileName, options));
}
if (opts.tslint) {
optGenPromises.push(tslintjson.makeFormatCodeOptions(fileName, options));
optGenPromises.push(tslintjson.makeFormatCodeOptions(fileName, options, additionalOptions));
}

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

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

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

// TODO replace newline code. NewLineCharacter params affect to only "new" newline. maybe.
var message: string;
var 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
22 changes: 12 additions & 10 deletions lib/provider/tslintjson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import ts = require("typescript");

import path = require("path");
import fs = require("fs");
import lib = require("../index");

function getConfigFileName(baseFileName: string, configFileName: string): string {
"use strict";
Expand All @@ -23,10 +24,8 @@ function getConfigFileName(baseFileName: string, configFileName: string): string

interface TslintSettings {
rules: {
indent: {
0: boolean;
1: number;
};
"no-consecutive-blank-lines": boolean,
"no-trailing-whitespace": boolean,
whitespace: {
0: boolean;
1: string;
Expand All @@ -39,23 +38,18 @@ interface TslintSettings {
};
}

export function makeFormatCodeOptions(fileName: string, options: ts.FormatCodeOptions): ts.FormatCodeOptions {
export function makeFormatCodeOptions(fileName: string, options: ts.FormatCodeOptions, additionalOptions: lib.AdditionalFormatOptions): ts.FormatCodeOptions {
"use strict";

var configFileName = getConfigFileName(path.resolve(fileName), "tslint.json");
if (!configFileName) {
return options;
}
// console.log("tslint makeFormatCodeOptions");
// console.log("read " + configFileName);

var config: TslintSettings = JSON.parse(<any>fs.readFileSync(configFileName, "utf-8"));
if (!config.rules) {
return options;
}
if (config.rules.indent && config.rules.indent[0]) {
options.IndentSize = config.rules.indent[1];
}
if (config.rules.whitespace && config.rules.whitespace[0]) {
for (var p in config.rules.whitespace) {
var value = config.rules.whitespace[p];
Expand All @@ -74,5 +68,13 @@ export function makeFormatCodeOptions(fileName: string, options: ts.FormatCodeOp
}
}

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

if (config.rules["no-trailing-whitespace"] === true) {
additionalOptions.noTrailingWhitespace = true;
}

return options;
}
10 changes: 10 additions & 0 deletions lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";

import ts = require("typescript");
import lib = require("./index");

export function createDefaultFormatCodeOptions(): ts.FormatCodeOptions {
"use strict";
Expand All @@ -21,3 +22,12 @@ export function createDefaultFormatCodeOptions(): ts.FormatCodeOptions {
PlaceOpenBraceOnNewLineForControlBlocks: false
};
}

export function createDefaultAdditionalFormatCodeOptions(): lib.AdditionalFormatOptions {
"use strict";

return {
noConsecutiveBlankLines: false,
noTrailingWhitespace: false
};
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@
"./typings/power-assert/power-assert.d.ts",
"./node_modules/typescript/lib/lib.es6.d.ts"
]
}
}