forked from palantir/tslint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtslint-cli.ts
334 lines (293 loc) · 11.6 KB
/
tslint-cli.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/**
* @license
* Copyright 2013 Palantir Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as fs from "fs";
import * as glob from "glob";
import * as optimist from "optimist";
import * as path from "path";
import * as ts from "typescript";
import {
CONFIG_FILENAME,
DEFAULT_CONFIG,
findConfiguration,
} from "./configuration";
import * as Linter from "./linter";
import { consoleTestResultHandler, runTest } from "./test";
import { updateNotifierCheck } from "./updateNotifier";
let processed = optimist
.usage("Usage: $0 [options] file ...")
.check((argv: any) => {
// at least one of file, help, version, project or unqualified argument must be present
if (!(argv.h || argv.i || argv.test || argv.v || argv.project || argv._.length > 0)) {
throw "Missing files";
}
if (argv.f) {
throw "-f option is no longer available. Supply files directly to the tslint command instead.";
}
})
.options({
c: {
alias: "config",
describe: "configuration file",
},
e: {
alias: "exclude",
describe: "exclude globs from path expansion",
type: "string",
},
fix: {
describe: "Fixes linting errors for select rules. This may overwrite linted files",
},
force: {
describe: "return status code 0 even if there are lint errors",
type: "boolean",
},
h: {
alias: "help",
describe: "display detailed help",
},
i: {
alias: "init",
describe: "generate a tslint.json config file in the current working directory",
},
o: {
alias: "out",
describe: "output file",
},
project: {
describe: "tsconfig.json file",
},
r: {
alias: "rules-dir",
describe: "rules directory",
},
s: {
alias: "formatters-dir",
describe: "formatters directory",
},
t: {
alias: "format",
default: "prose",
describe: "output format (prose, json, stylish, verbose, pmd, msbuild, checkstyle, vso, fileslist)",
},
test: {
describe: "test that tslint produces the correct output for the specified directory",
},
"type-check": {
describe: "enable type checking when linting a project",
},
v: {
alias: "version",
describe: "current version",
},
});
const argv = processed.argv;
let outputStream: any;
if (argv.o != null) {
outputStream = fs.createWriteStream(argv.o, {
flags: "w+",
mode: 420,
});
} else {
outputStream = process.stdout;
}
if (argv.v != null) {
outputStream.write(Linter.VERSION + "\n");
process.exit(0);
}
if (argv.i != null) {
if (fs.existsSync(CONFIG_FILENAME)) {
console.error(`Cannot generate ${CONFIG_FILENAME}: file already exists`);
process.exit(1);
}
const tslintJSON = JSON.stringify(DEFAULT_CONFIG, undefined, " ");
fs.writeFileSync(CONFIG_FILENAME, tslintJSON);
process.exit(0);
}
if (argv.test != null) {
const results = runTest(argv.test, argv.r);
const didAllTestsPass = consoleTestResultHandler(results);
process.exit(didAllTestsPass ? 0 : 1);
}
if ("help" in argv) {
outputStream.write(processed.help());
const outputString = `
tslint accepts the following commandline options:
-c, --config:
The location of the configuration file that tslint will use to
determine which rules are activated and what options to provide
to the rules. If no option is specified, the config file named
tslint.json is used, so long as it exists in the path.
The format of the file is { rules: { /* rules list */ } },
where /* rules list */ is a key: value comma-seperated list of
rulename: rule-options pairs. Rule-options can be either a
boolean true/false value denoting whether the rule is used or not,
or a list [boolean, ...] where the boolean provides the same role
as in the non-list case, and the rest of the list are options passed
to the rule that will determine what it checks for (such as number
of characters for the max-line-length rule, or what functions to ban
for the ban rule).
-e, --exclude:
A filename or glob which indicates files to exclude from linting.
This option can be supplied multiple times if you need multiple
globs to indicate which files to exclude.
--fix:
Fixes linting errors for select rules. This may overwrite linted files.
--force:
Return status code 0 even if there are any lint errors.
Useful while running as npm script.
-i, --init:
Generates a tslint.json config file in the current working directory.
-o, --out:
A filename to output the results to. By default, tslint outputs to
stdout, which is usually the console where you're running it from.
-r, --rules-dir:
An additional rules directory, for user-created rules.
tslint will always check its default rules directory, in
node_modules/tslint/lib/rules, before checking the user-provided
rules directory, so rules in the user-provided rules directory
with the same name as the base rules will not be loaded.
-s, --formatters-dir:
An additional formatters directory, for user-created formatters.
Formatters are files that will format the tslint output, before
writing it to stdout or the file passed in --out. The default
directory, node_modules/tslint/build/formatters, will always be
checked first, so user-created formatters with the same names
as the base formatters will not be loaded.
-t, --format:
The formatter to use to format the results of the linter before
outputting it to stdout or the file passed in --out. The core
formatters are prose (human readable), json (machine readable)
and verbose. prose is the default if this option is not used.
Other built-in options include pmd, msbuild, checkstyle, and vso.
Additonal formatters can be added and used if the --formatters-dir
option is set.
--test:
Runs tslint on the specified directory and checks if tslint's output matches
the expected output in .lint files. Automatically loads the tslint.json file in the
specified directory as the configuration file for the tests. See the
full tslint documentation for more details on how this can be used to test custom rules.
--project:
The location of a tsconfig.json file that will be used to determine which
files will be linted.
--type-check
Enables the type checker when running linting rules. --project must be
specified in order to enable type checking.
-v, --version:
The current version of tslint.
-h, --help:
Prints this help message.\n`;
outputStream.write(outputString);
process.exit(0);
}
// when provided, it should point to an existing location
if (argv.c && !fs.existsSync(argv.c)) {
console.error("Invalid option for configuration: " + argv.c);
process.exit(1);
}
const possibleConfigAbsolutePath = argv.c != null ? path.resolve(argv.c) : null;
const processFiles = (files: string[], program?: ts.Program) => {
const linter = new Linter({
fix: argv.fix,
formatter: argv.t,
formattersDirectory: argv.s || "",
rulesDirectory: argv.r || "",
}, program);
for (const file of files) {
if (!fs.existsSync(file)) {
console.error(`Unable to open file: ${file}`);
process.exit(1);
}
const buffer = new Buffer(256);
buffer.fill(0);
const fd = fs.openSync(file, "r");
try {
fs.readSync(fd, buffer, 0, 256, null);
if (buffer.readInt8(0) === 0x47 && buffer.readInt8(188) === 0x47) {
// MPEG transport streams use the '.ts' file extension. They use 0x47 as the frame
// separator, repeating every 188 bytes. It is unlikely to find that pattern in
// TypeScript source, so tslint ignores files with the specific pattern.
console.warn(`${file}: ignoring MPEG transport stream`);
return;
}
} finally {
fs.closeSync(fd);
}
const contents = fs.readFileSync(file, "utf8");
const configLoad = findConfiguration(possibleConfigAbsolutePath, file);
if (configLoad.results) {
linter.lint(file, contents, configLoad.results);
} else {
console.error(`Failed to load ${configLoad.path}: ${configLoad.error.message}`);
process.exit(1);
}
}
const lintResult = linter.getResult();
outputStream.write(lintResult.output, () => {
if (lintResult.failureCount > 0) {
process.exit(argv.force ? 0 : 2);
}
});
if (lintResult.format === "prose") {
// Check to see if there are any updates available
updateNotifierCheck();
}
};
// if both files and tsconfig are present, use files
let files = argv._;
let program: ts.Program;
if (argv.project != null) {
if (!fs.existsSync(argv.project)) {
console.error("Invalid option for project: " + argv.project);
process.exit(1);
}
program = Linter.createProgram(argv.project, path.dirname(argv.project));
if (files.length === 0) {
files = Linter.getFileNames(program);
}
if (argv["type-check"]) {
// if type checking, run the type checker
const diagnostics = ts.getPreEmitDiagnostics(program);
if (diagnostics.length > 0) {
const messages = diagnostics.map((diag) => {
// emit any error messages
let message = ts.DiagnosticCategory[diag.category];
if (diag.file) {
const {line, character} = diag.file.getLineAndCharacterOfPosition(diag.start);
message += ` at ${diag.file.fileName}:${line + 1}:${character + 1}:`;
}
message += " " + ts.flattenDiagnosticMessageText(diag.messageText, "\n");
return message;
});
throw new Error(messages.join("\n"));
}
} else {
// if not type checking, we don't need to pass in a program object
program = undefined;
}
}
const trimSingleQuotes = (str: string) => str.replace(/^'|'$/g, "");
let ignorePatterns: string[] = [];
if (argv.e) {
const excludeArguments: string[] = Array.isArray(argv.e) ? argv.e : [argv.e];
ignorePatterns = excludeArguments.map(trimSingleQuotes);
}
files = files
// remove single quotes which break matching on Windows when glob is passed in single quotes
.map(trimSingleQuotes)
.map((file: string) => glob.sync(file, { ignore: ignorePatterns, nodir: true }))
.reduce((a: string[], b: string[]) => a.concat(b));
processFiles(files, program);