Skip to content

Commit 9c55670

Browse files
committed
feat(tslint): add support to read extended tslint.json
Closes: #123
1 parent 89f6990 commit 9c55670

File tree

1 file changed

+62
-83
lines changed

1 file changed

+62
-83
lines changed

lib/provider/tslintjson.ts

+62-83
Original file line numberDiff line numberDiff line change
@@ -1,118 +1,97 @@
11
import * as ts from "typescript";
2-
2+
import { IOptions } from "tslint";
33
import * as path from "path";
4-
import * as fs from "fs";
54

65
import { Options } from "../";
7-
import { getConfigFileName, parseJSON } from "../utils";
8-
9-
interface TslintSettings {
10-
rules: {
11-
indent: {
12-
0: boolean;
13-
1: string;
14-
};
15-
"no-consecutive-blank-lines": boolean,
16-
whitespace: {
17-
0: boolean;
18-
1: string;
19-
2: string;
20-
3: string;
21-
4: string;
22-
5: string;
23-
[key: string]: any;
24-
};
25-
};
26-
}
6+
import { getConfigFileName } from "../utils";
7+
278

289
export interface AdditionalFormatSettings {
2910
$noConsecutiveBlankLines: boolean;
3011
}
3112

32-
export function makeFormatCodeOptions(fileName: string, opts: Options, formatSettings: ts.FormatCodeSettings): ts.FormatCodeSettings {
13+
export async function makeFormatCodeOptions(fileName: string, opts: Options, formatSettings: ts.FormatCodeSettings): Promise<ts.FormatCodeSettings> {
3314

34-
let baseDir = opts.baseDir ? path.resolve(opts.baseDir) : path.dirname(path.resolve(fileName));
35-
let configFileName: string | null;
36-
if (opts.tslintFile && path.isAbsolute(opts.tslintFile)) {
37-
configFileName = opts.tslintFile;
38-
} else {
39-
configFileName = getConfigFileName(baseDir, opts.tslintFile || "tslint.json");
40-
}
41-
if (!configFileName) {
42-
return formatSettings;
43-
}
44-
45-
if (opts.verbose) {
46-
console.log(`read ${configFileName} for ${fileName}`);
47-
}
15+
const rules = await getRules(fileName, opts);
4816

49-
let config: TslintSettings = parseJSON(fs.readFileSync(configFileName, "utf-8"));
50-
if (!config.rules) {
17+
if (!rules) {
5118
return formatSettings;
5219
}
53-
if (config.rules.indent && config.rules.indent[0]) {
54-
if (config.rules.indent[1] === "spaces") {
55-
formatSettings.convertTabsToSpaces = true;
56-
} else if (config.rules.indent[1] === "tabs") {
57-
formatSettings.convertTabsToSpaces = false;
20+
21+
const indent = rules.get("indent");
22+
const whitespace = rules.get("whitespace");
23+
24+
if (indent && indent.ruleArguments) {
25+
switch (indent.ruleArguments[0]) {
26+
case "spaces":
27+
formatSettings.convertTabsToSpaces = true;
28+
break;
29+
case "tabs":
30+
formatSettings.convertTabsToSpaces = false;
31+
break;
32+
default:
33+
break;
5834
}
5935
}
60-
if (config.rules.whitespace && config.rules.whitespace[0]) {
61-
for (let p in config.rules.whitespace) {
62-
let value = config.rules.whitespace[p];
63-
if (value === "check-branch") {
64-
formatSettings.insertSpaceAfterKeywordsInControlFlowStatements = true;
65-
} else if (value === "check-decl") {
66-
// none?
67-
} else if (value === "check-operator") {
68-
formatSettings.insertSpaceBeforeAndAfterBinaryOperators = true;
69-
} else if (value === "check-separator") {
70-
formatSettings.insertSpaceAfterCommaDelimiter = true;
71-
formatSettings.insertSpaceAfterSemicolonInForStatements = true;
72-
} else if (value === "check-type") {
73-
// none?
74-
} else if (value === "check-typecast") {
75-
formatSettings.insertSpaceAfterTypeAssertion = true;
36+
if (whitespace && whitespace.ruleArguments) {
37+
for (let p in whitespace.ruleArguments) {
38+
switch (whitespace.ruleArguments[p]) {
39+
case "check-branch":
40+
formatSettings.insertSpaceAfterKeywordsInControlFlowStatements = true;
41+
break;
42+
case "check-operator":
43+
formatSettings.insertSpaceBeforeAndAfterBinaryOperators = true;
44+
break;
45+
case "check-separator":
46+
formatSettings.insertSpaceAfterCommaDelimiter = true;
47+
formatSettings.insertSpaceAfterSemicolonInForStatements = true;
48+
break;
49+
case "check-typecast":
50+
formatSettings.insertSpaceAfterTypeAssertion = true;
51+
break;
52+
default:
53+
break;
7654
}
7755
}
7856
}
7957

8058
return formatSettings;
8159
}
8260

83-
export function postProcess(fileName: string, formattedCode: string, opts: Options, _formatSettings: ts.FormatCodeSettings): string {
61+
export async function postProcess(fileName: string, formattedCode: string, opts: Options, _formatSettings: ts.FormatCodeSettings): Promise<string> {
8462

85-
let baseDir = opts.baseDir ? path.resolve(opts.baseDir) : path.dirname(path.resolve(fileName));
86-
let configFileName: string | null;
87-
if (opts.tslintFile && path.isAbsolute(opts.tslintFile)) {
88-
configFileName = opts.tslintFile;
89-
} else {
90-
configFileName = getConfigFileName(baseDir, opts.tslintFile || "tslint.json");
91-
}
92-
if (!configFileName) {
93-
return formattedCode;
94-
}
63+
const rules = await getRules(fileName, opts);
9564

96-
let config: TslintSettings = parseJSON(fs.readFileSync(configFileName, "utf-8"));
97-
if (!config.rules) {
65+
if (!rules) {
9866
return formattedCode;
9967
}
10068

101-
let additionalOptions = createDefaultAdditionalFormatCodeSettings();
102-
if (config.rules["no-consecutive-blank-lines"] === true) {
103-
additionalOptions.$noConsecutiveBlankLines = true;
104-
}
105-
106-
if (additionalOptions.$noConsecutiveBlankLines) {
69+
if (rules.has("no-consecutive-blank-lines")) {
10770
formattedCode = formattedCode.replace(/\n+^$/mg, "\n");
10871
}
10972

11073
return formattedCode;
11174
}
11275

113-
function createDefaultAdditionalFormatCodeSettings(): AdditionalFormatSettings {
76+
async function getRules(fileName: string, opts: Options): Promise<Map<string, Partial<IOptions>> | undefined> {
77+
const baseDir = opts.baseDir ? path.resolve(opts.baseDir) : path.dirname(path.resolve(fileName));
78+
let configFileName: string | null;
79+
80+
if (opts.tslintFile && path.isAbsolute(opts.tslintFile)) {
81+
configFileName = opts.tslintFile;
82+
} else {
83+
configFileName = getConfigFileName(baseDir, opts.tslintFile || "tslint.json");
84+
}
85+
86+
if (!configFileName) {
87+
return undefined;
88+
}
89+
90+
if (opts.verbose) {
91+
console.log(`read ${configFileName} for ${fileName}`);
92+
}
11493

115-
return {
116-
$noConsecutiveBlankLines: false,
117-
};
94+
const { Configuration } = await import("tslint");
95+
const { rules } = Configuration.loadConfigurationFromPath(configFileName);
96+
return rules;
11897
}

0 commit comments

Comments
 (0)