|
1 | 1 | import * as ts from "typescript";
|
2 |
| - |
| 2 | +import { IOptions } from "tslint"; |
3 | 3 | import * as path from "path";
|
4 |
| -import * as fs from "fs"; |
5 | 4 |
|
6 | 5 | 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 | + |
27 | 8 |
|
28 | 9 | export interface AdditionalFormatSettings {
|
29 | 10 | $noConsecutiveBlankLines: boolean;
|
30 | 11 | }
|
31 | 12 |
|
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> { |
33 | 14 |
|
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); |
48 | 16 |
|
49 |
| - let config: TslintSettings = parseJSON(fs.readFileSync(configFileName, "utf-8")); |
50 |
| - if (!config.rules) { |
| 17 | + if (!rules) { |
51 | 18 | return formatSettings;
|
52 | 19 | }
|
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; |
58 | 34 | }
|
59 | 35 | }
|
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; |
76 | 54 | }
|
77 | 55 | }
|
78 | 56 | }
|
79 | 57 |
|
80 | 58 | return formatSettings;
|
81 | 59 | }
|
82 | 60 |
|
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> { |
84 | 62 |
|
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); |
95 | 64 |
|
96 |
| - let config: TslintSettings = parseJSON(fs.readFileSync(configFileName, "utf-8")); |
97 |
| - if (!config.rules) { |
| 65 | + if (!rules) { |
98 | 66 | return formattedCode;
|
99 | 67 | }
|
100 | 68 |
|
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")) { |
107 | 70 | formattedCode = formattedCode.replace(/\n+^$/mg, "\n");
|
108 | 71 | }
|
109 | 72 |
|
110 | 73 | return formattedCode;
|
111 | 74 | }
|
112 | 75 |
|
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 | + } |
114 | 93 |
|
115 |
| - return { |
116 |
| - $noConsecutiveBlankLines: false, |
117 |
| - }; |
| 94 | + const { Configuration } = await import("tslint"); |
| 95 | + const { rules } = Configuration.loadConfigurationFromPath(configFileName); |
| 96 | + return rules; |
118 | 97 | }
|
0 commit comments