Skip to content

Commit 2941857

Browse files
committed
feat(tsfmt): refactor to es6 style
1 parent b058cdd commit 2941857

File tree

7 files changed

+78
-80
lines changed

7 files changed

+78
-80
lines changed

lib/cli.ts

+22-22
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
require("es6-promise").polyfill();
22

3-
import fs = require("fs");
4-
import commandpost = require("commandpost");
5-
import path = require("path");
3+
import * as fs from "fs";
4+
import * as commandpost from "commandpost";
5+
import * as path from "path";
66

7-
import lib = require("./index");
8-
import utils = require("./utils");
7+
import * as lib from "./";
8+
import {getConfigFileName} from "./utils";
99

10-
var packageJson = JSON.parse(fs.readFileSync(__dirname + "/../package.json").toString());
10+
let packageJson = JSON.parse(fs.readFileSync(__dirname + "/../package.json").toString());
1111

1212
interface RootOptions {
1313
replace: boolean;
@@ -23,7 +23,7 @@ interface RootArguments {
2323
files: string[];
2424
}
2525

26-
var root = commandpost
26+
let root = commandpost
2727
.create<RootOptions, RootArguments>("tsfmt [files...]")
2828
.version(packageJson.version, "-v, --version")
2929
.option("-r, --replace", "replace .ts file")
@@ -34,17 +34,17 @@ var root = commandpost
3434
.option("--no-tsfmt", "don't read a tsfmt.json")
3535
.option("--verbose", "makes output more verbose")
3636
.action((opts, args) => {
37-
var replace = !!opts.replace;
38-
var verify = !!opts.verify;
39-
var stdin = !!opts.stdin;
40-
var tslint = !!opts.tslint;
41-
var editorconfig = !!opts.editorconfig;
42-
var tsfmt = !!opts.tsfmt;
43-
44-
var files = args.files;
45-
var useTsconfig = false;
37+
let replace = !!opts.replace;
38+
let verify = !!opts.verify;
39+
let stdin = !!opts.stdin;
40+
let tslint = !!opts.tslint;
41+
let editorconfig = !!opts.editorconfig;
42+
let tsfmt = !!opts.tsfmt;
43+
44+
let files = args.files;
45+
let useTsconfig = false;
4646
if (files.length === 0) {
47-
var configFileName = utils.getConfigFileName(process.cwd(), "tsconfig.json");
47+
let configFileName = getConfigFileName(process.cwd(), "tsconfig.json");
4848
if (configFileName) {
4949
files = readFilesFromTsconfig(configFileName);
5050
useTsconfig = true;
@@ -80,7 +80,7 @@ var root = commandpost
8080
tsfmt: tsfmt
8181
})
8282
.then(result => {
83-
var resultMap: lib.ResultMap = {};
83+
let resultMap: lib.ResultMap = {};
8484
resultMap[result.fileName] = result;
8585
return resultMap;
8686
})
@@ -107,7 +107,7 @@ commandpost
107107
function showResultHandler(resultMap: lib.ResultMap): Promise<any> {
108108
"use strict";
109109

110-
var hasError = Object.keys(resultMap).filter(fileName => resultMap[fileName].error).length !== 0;
110+
let hasError = Object.keys(resultMap).filter(fileName => resultMap[fileName].error).length !== 0;
111111
if (hasError) {
112112
Object.keys(resultMap)
113113
.map(fileName => resultMap[fileName])
@@ -143,10 +143,10 @@ function errorHandler(err: any): Promise<any> {
143143
function readFilesFromTsconfig(configPath: string) {
144144
"use strict";
145145

146-
var tsconfigDir = path.dirname(configPath);
147-
var tsconfig = JSON.parse(fs.readFileSync(configPath, "utf-8"));
146+
let tsconfigDir = path.dirname(configPath);
147+
let tsconfig = JSON.parse(fs.readFileSync(configPath, "utf-8"));
148148
if (tsconfig.files) {
149-
var files: string[] = tsconfig.files;
149+
let files: string[] = tsconfig.files;
150150
return files.map(filePath => path.resolve(tsconfigDir, filePath));
151151
} else {
152152
throw new Error(`No "files" section present in tsconfig.json`);

lib/formatter.ts

+13-15
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,46 @@
11
"use strict";
22

3-
import ts = require("typescript");
4-
import utils = require("./utils");
3+
import * as ts from "typescript";
4+
import {createDefaultFormatCodeOptions} from "./utils";
55

66
// from https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#pretty-printer-using-the-ls-formatter
77

88
// Note: this uses ts.formatting which is part of the typescript 1.4 package but is not currently
99
// exposed in the public typescript.d.ts. The typings should be exposed in the next release.
10-
function format(fileName: string, text: string, options = utils.createDefaultFormatCodeOptions()) {
10+
export default function format(fileName: string, text: string, options = createDefaultFormatCodeOptions()) {
1111
"use strict";
1212

1313
// Parse the source text
14-
var sourceFile = ts.createSourceFile(fileName, text, ts.ScriptTarget.Latest, (<any>/* backward compat for typescript-1.4.1 */"0"));
14+
let sourceFile = ts.createSourceFile(fileName, text, ts.ScriptTarget.Latest, (<any>/* backward compat for typescript-1.4.1 */"0"));
1515
fixupParentReferences(sourceFile);
1616

1717
// Get the formatting edits on the input sources
18-
var edits = (<any>ts).formatting.formatDocument(sourceFile, getRuleProvider(options), options);
18+
let edits = (<any>ts).formatting.formatDocument(sourceFile, getRuleProvider(options), options);
1919

2020
// Apply the edits on the input code
2121
return applyEdits(text, edits);
2222

2323
function getRuleProvider(options: ts.FormatCodeOptions) {
2424
// Share this between multiple formatters using the same options.
2525
// This represents the bulk of the space the formatter uses.
26-
var ruleProvider = new (<any>ts).formatting.RulesProvider();
26+
let ruleProvider = new (<any>ts).formatting.RulesProvider();
2727
ruleProvider.ensureUpToDate(options);
2828
return ruleProvider;
2929
}
3030

3131
function applyEdits(text: string, edits: ts.TextChange[]): string {
3232
// Apply edits in reverse on the existing text
33-
var result = text;
34-
for (var i = edits.length - 1; i >= 0; i--) {
35-
var change = edits[i];
36-
var head: string;
33+
let result = text;
34+
for (let i = edits.length - 1; i >= 0; i--) {
35+
let change = edits[i];
36+
let head: string;
3737
if (typeof change.span.start === "number") {
3838
head = result.slice(0, change.span.start);
3939
} else {
4040
// backward compat for typescript-1.4.1
4141
head = result.slice(0, (<any>change.span.start)());
4242
}
43-
var tail: string;
43+
let tail: string;
4444
if (typeof change.span.start === "number") {
4545
tail = result.slice(change.span.start + change.span.length);
4646
} else {
@@ -56,18 +56,16 @@ function format(fileName: string, text: string, options = utils.createDefaultFor
5656
function fixupParentReferences(sourceFile: ts.SourceFile) {
5757
"use strict";
5858

59-
var parent: ts.Node = sourceFile;
59+
let parent: ts.Node = sourceFile;
6060

6161
function walk(n: ts.Node): void {
6262
n.parent = parent;
6363

64-
var saveParent = parent;
64+
let saveParent = parent;
6565
parent = n;
6666
ts.forEachChild(n, walk);
6767
parent = saveParent;
6868
}
6969

7070
ts.forEachChild(sourceFile, walk);
7171
}
72-
73-
export = format;

lib/index.ts

+21-21
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
"use strict";
22

3-
import ts = require("typescript");
4-
import formatter = require("./formatter");
3+
import * as ts from "typescript";
4+
import formatter from "./formatter";
55
import utils = require("./utils");
66

7-
import fs = require("fs");
7+
import * as fs from "fs";
88

9-
import base = require("./provider/base");
10-
import editorconfig = require("./provider/editorconfig");
11-
import tslintjson = require("./provider/tslintjson");
9+
import base from "./provider/base";
10+
import editorconfig from "./provider/editorconfig";
11+
import tslintjson from "./provider/tslintjson";
1212

1313
export interface Options {
1414
dryRun?: boolean;
@@ -36,10 +36,10 @@ export interface Result {
3636
export function processFiles(files: string[], opts: Options): Promise<ResultMap> {
3737
"use strict";
3838

39-
var resultMap: ResultMap = {};
40-
var promises = files.map(fileName => {
39+
let resultMap: ResultMap = {};
40+
let promises = files.map(fileName => {
4141
if (!fs.existsSync(fileName)) {
42-
var result: Result = {
42+
let result: Result = {
4343
fileName: fileName,
4444
options: null,
4545
message: `${fileName} is not exists. process abort.`,
@@ -50,7 +50,7 @@ export function processFiles(files: string[], opts: Options): Promise<ResultMap>
5050
return Promise.resolve(result);
5151
}
5252

53-
var content = fs.readFileSync(fileName).toString();
53+
let content = fs.readFileSync(fileName).toString();
5454
return processString(fileName, content, opts);
5555
});
5656
return Promise.all(promises).then(resultList=> {
@@ -66,8 +66,8 @@ export function processStream(fileName: string, input: NodeJS.ReadableStream, op
6666

6767
input.setEncoding("utf8");
6868

69-
var promise = new Promise<string>((resolve, reject) => {
70-
var fragment = "";
69+
let promise = new Promise<string>((resolve, reject) => {
70+
let fragment = "";
7171
input.on("data", (chunk: string) => {
7272
fragment += chunk;
7373
});
@@ -82,30 +82,30 @@ export function processStream(fileName: string, input: NodeJS.ReadableStream, op
8282
export function processString(fileName: string, content: string, opts: Options): Promise<Result> {
8383
"use strict";
8484

85-
var options = utils.createDefaultFormatCodeOptions();
86-
var optGenPromises: (ts.FormatCodeOptions | Promise<ts.FormatCodeOptions>)[] = [];
85+
let options = utils.createDefaultFormatCodeOptions();
86+
let optGenPromises: (ts.FormatCodeOptions | Promise<ts.FormatCodeOptions>)[] = [];
8787
if (opts.tsfmt) {
88-
optGenPromises.push(base.makeFormatCodeOptions(fileName, options));
88+
optGenPromises.push(base(fileName, options));
8989
}
9090
if (opts.editorconfig) {
91-
optGenPromises.push(editorconfig.makeFormatCodeOptions(fileName, options));
91+
optGenPromises.push(editorconfig(fileName, options));
9292
}
9393
if (opts.tslint) {
94-
optGenPromises.push(tslintjson.makeFormatCodeOptions(fileName, options));
94+
optGenPromises.push(tslintjson(fileName, options));
9595
}
9696

9797
return Promise
9898
.all(optGenPromises)
9999
.then(() => {
100-
var formattedCode = formatter(fileName, content, options);
100+
let formattedCode = formatter(fileName, content, options);
101101
if ((<any>formattedCode).trimRight) {
102102
formattedCode = (<any>formattedCode).trimRight();
103103
formattedCode += "\n";
104104
}
105105

106106
// TODO replace newline code. NewLineCharacter params affect to only "new" newline. maybe.
107-
var message: string;
108-
var error = false;
107+
let message: string;
108+
let error = false;
109109
if (opts && opts.verify) {
110110
if (content !== formattedCode) {
111111
message = `${fileName} is not formatted`;
@@ -120,7 +120,7 @@ export function processString(fileName: string, content: string, opts: Options):
120120
message = formattedCode;
121121
}
122122

123-
var result: Result = {
123+
let result: Result = {
124124
fileName: fileName,
125125
options: options,
126126
message: message,

lib/provider/base.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
"use strict";
22

3-
import ts = require("typescript");
3+
import * as ts from "typescript";
44

5-
import path = require("path");
6-
import fs = require("fs");
5+
import * as path from "path";
6+
import * as fs from "fs";
77

8-
import utils = require("../utils");
8+
import {getConfigFileName} from "../utils";
99

1010
interface TsfmtSettings {
1111
// from FormatCodeOptions
@@ -33,10 +33,10 @@ interface TsfmtSettings {
3333
convertTabsToSpaces?: boolean;
3434
}
3535

36-
export function makeFormatCodeOptions(fileName: string, options: ts.FormatCodeOptions): ts.FormatCodeOptions {
36+
export default function makeFormatCodeOptions(fileName: string, options: ts.FormatCodeOptions): ts.FormatCodeOptions {
3737
"use strict";
3838

39-
var configFileName = utils.getConfigFileName(path.dirname(path.resolve(fileName)), "tsfmt.json");
39+
var configFileName = getConfigFileName(path.dirname(path.resolve(fileName)), "tsfmt.json");
4040
if (!configFileName) {
4141
return options;
4242
}

lib/provider/editorconfig.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
"use strict";
22

3-
import ts = require("typescript");
3+
import * as ts from "typescript";
44

5-
import editorconfig = require("editorconfig");
5+
import * as editorconfig from "editorconfig";
66

7-
export function makeFormatCodeOptions(fileName: string, options: ts.FormatCodeOptions): Promise<ts.FormatCodeOptions> {
7+
export default function makeFormatCodeOptions(fileName: string, options: ts.FormatCodeOptions): Promise<ts.FormatCodeOptions> {
88
"use strict";
99

1010
return editorconfig

lib/provider/tslintjson.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
"use strict";
22

3-
import ts = require("typescript");
3+
import * as ts from "typescript";
44

5-
import path = require("path");
6-
import fs = require("fs");
5+
import * as path from "path";
6+
import * as fs from "fs";
77

8-
import utils = require("../utils");
8+
import {getConfigFileName} from "../utils";
99

1010
interface TslintSettings {
1111
rules: {
@@ -25,26 +25,26 @@ interface TslintSettings {
2525
};
2626
}
2727

28-
export function makeFormatCodeOptions(fileName: string, options: ts.FormatCodeOptions): ts.FormatCodeOptions {
28+
export default function makeFormatCodeOptions(fileName: string, options: ts.FormatCodeOptions): ts.FormatCodeOptions {
2929
"use strict";
3030

31-
var configFileName = utils.getConfigFileName(path.dirname(path.resolve(fileName)), "tslint.json");
31+
let configFileName = getConfigFileName(path.dirname(path.resolve(fileName)), "tslint.json");
3232
if (!configFileName) {
3333
return options;
3434
}
3535
// console.log("tslint makeFormatCodeOptions");
3636
// console.log("read " + configFileName);
3737

38-
var config: TslintSettings = JSON.parse(<any>fs.readFileSync(configFileName, "utf-8"));
38+
let config: TslintSettings = JSON.parse(<any>fs.readFileSync(configFileName, "utf-8"));
3939
if (!config.rules) {
4040
return options;
4141
}
4242
if (config.rules.indent && config.rules.indent[0]) {
4343
options.IndentSize = config.rules.indent[1];
4444
}
4545
if (config.rules.whitespace && config.rules.whitespace[0]) {
46-
for (var p in config.rules.whitespace) {
47-
var value = config.rules.whitespace[p];
46+
for (let p in config.rules.whitespace) {
47+
let value = config.rules.whitespace[p];
4848
if (value === "check-branch") {
4949
options.InsertSpaceAfterKeywordsInControlFlowStatements = true;
5050
} else if (value === "check-decl") {

lib/utils.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"use strict";
22

3-
import ts = require("typescript");
3+
import * as ts from "typescript";
44

5-
import fs = require("fs");
6-
import path = require("path");
5+
import * as fs from "fs";
6+
import * as path from "path";
77

88
export function createDefaultFormatCodeOptions(): ts.FormatCodeOptions {
99
"use strict";
@@ -28,7 +28,7 @@ export function createDefaultFormatCodeOptions(): ts.FormatCodeOptions {
2828
export function getConfigFileName(baseDir: string, configFileName: string): string {
2929
"use strict";
3030

31-
var configFilePath = path.resolve(baseDir, configFileName);
31+
let configFilePath = path.resolve(baseDir, configFileName);
3232
if (fs.existsSync(configFilePath)) {
3333
return configFilePath;
3434
}

0 commit comments

Comments
 (0)