aboutsummaryrefslogtreecommitdiff
path: root/node_modules/tslint/lib/runner.js
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2018-09-20 02:56:13 +0200
committerFlorian Dold <florian.dold@gmail.com>2018-09-20 02:56:13 +0200
commitbbff7403fbf46f9ad92240ac213df8d30ef31b64 (patch)
treec58400ec5124da1c7d56b01aea83309f80a56c3b /node_modules/tslint/lib/runner.js
parent003fb34971cf63466184351b4db5f7c67df4f444 (diff)
update packages
Diffstat (limited to 'node_modules/tslint/lib/runner.js')
-rw-r--r--node_modules/tslint/lib/runner.js138
1 files changed, 86 insertions, 52 deletions
diff --git a/node_modules/tslint/lib/runner.js b/node_modules/tslint/lib/runner.js
index b21019592..3ce085cb8 100644
--- a/node_modules/tslint/lib/runner.js
+++ b/node_modules/tslint/lib/runner.js
@@ -25,7 +25,7 @@ var path = require("path");
var ts = require("typescript");
var configuration_1 = require("./configuration");
var error_1 = require("./error");
-var Linter = require("./linter");
+var linter_1 = require("./linter");
var utils_1 = require("./utils");
function run(options, logger) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
@@ -39,7 +39,7 @@ function run(options, logger) {
case 2:
error_2 = _a.sent();
if (error_2 instanceof error_1.FatalError) {
- logger.error(error_2.message);
+ logger.error(error_2.message + "\n");
return [2 /*return*/, 1 /* FatalError */];
}
throw error_2;
@@ -56,10 +56,10 @@ function runWorker(options, logger) {
switch (_b.label) {
case 0:
if (options.init) {
- if (fs.existsSync(configuration_1.CONFIG_FILENAME)) {
- throw new error_1.FatalError("Cannot generate " + configuration_1.CONFIG_FILENAME + ": file already exists");
+ if (fs.existsSync(configuration_1.JSON_CONFIG_FILENAME)) {
+ throw new error_1.FatalError("Cannot generate " + configuration_1.JSON_CONFIG_FILENAME + ": file already exists");
}
- fs.writeFileSync(configuration_1.CONFIG_FILENAME, JSON.stringify(configuration_1.DEFAULT_CONFIG, undefined, " "));
+ fs.writeFileSync(configuration_1.JSON_CONFIG_FILENAME, JSON.stringify(configuration_1.DEFAULT_CONFIG, undefined, " "));
return [2 /*return*/, 0 /* Ok */];
}
if (!options.test) return [3 /*break*/, 2];
@@ -67,7 +67,7 @@ function runWorker(options, logger) {
case 1:
test_1 = _b.sent();
results = test_1.runTests((options.files || []).map(trimSingleQuotes), options.rulesDirectory);
- return [2 /*return*/, test_1.consoleTestResultsHandler(results) ? 0 /* Ok */ : 1 /* FatalError */];
+ return [2 /*return*/, test_1.consoleTestResultsHandler(results, logger) ? 0 /* Ok */ : 1 /* FatalError */];
case 2:
if (options.config && !fs.existsSync(options.config)) {
throw new error_1.FatalError("Invalid option for configuration: " + options.config);
@@ -76,7 +76,7 @@ function runWorker(options, logger) {
case 3:
_a = _b.sent(), output = _a.output, errorCount = _a.errorCount;
if (output && output.trim()) {
- logger.log(output);
+ logger.log(output + "\n");
}
return [2 /*return*/, options.force || errorCount === 0 ? 0 /* Ok */ : 2 /* LintError */];
}
@@ -87,14 +87,14 @@ function runLinter(options, logger) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var _a, files, program, diagnostics, message;
return tslib_1.__generator(this, function (_b) {
- _a = resolveFilesAndProgram(options), files = _a.files, program = _a.program;
+ _a = resolveFilesAndProgram(options, logger), files = _a.files, program = _a.program;
// if type checking, run the type checker
if (program && options.typeCheck) {
diagnostics = ts.getPreEmitDiagnostics(program);
if (diagnostics.length !== 0) {
message = diagnostics.map(function (d) { return showDiagnostic(d, program, options.outputAbsolutePaths); }).join("\n");
if (options.force) {
- logger.error(message);
+ logger.error(message + "\n");
}
else {
throw new error_1.FatalError(message);
@@ -105,83 +105,114 @@ function runLinter(options, logger) {
});
});
}
-function resolveFilesAndProgram(_a) {
+function resolveFilesAndProgram(_a, logger) {
var files = _a.files, project = _a.project, exclude = _a.exclude, outputAbsolutePaths = _a.outputAbsolutePaths;
// remove single quotes which break matching on Windows when glob is passed in single quotes
- var ignore = utils_1.arrayify(exclude).map(trimSingleQuotes);
+ exclude = exclude.map(trimSingleQuotes);
if (project === undefined) {
- return { files: resolveGlobs(files, ignore, outputAbsolutePaths) };
+ return { files: resolveGlobs(files, exclude, outputAbsolutePaths, logger) };
}
var projectPath = findTsconfig(project);
if (projectPath === undefined) {
throw new error_1.FatalError("Invalid option for project: " + project);
}
- var program = Linter.createProgram(projectPath);
+ exclude = exclude.map(function (pattern) { return path.resolve(pattern); });
+ var program = linter_1.Linter.createProgram(projectPath);
var filesFound;
- if (files === undefined || files.length === 0) {
- filesFound = Linter.getFileNames(program);
- if (ignore.length !== 0) {
- var mm_1 = ignore.map(function (pattern) { return new minimatch_1.Minimatch(path.resolve(pattern)); });
- filesFound = filesFound.filter(function (file) { return !mm_1.some(function (matcher) { return matcher.match(file); }); });
- }
+ if (files.length === 0) {
+ filesFound = filterFiles(linter_1.Linter.getFileNames(program), exclude, false);
}
else {
- filesFound = resolveGlobs(files, ignore, outputAbsolutePaths);
+ files = files.map(function (f) { return path.resolve(f); });
+ filesFound = filterFiles(program.getSourceFiles().map(function (f) { return f.fileName; }), files, true);
+ filesFound = filterFiles(filesFound, exclude, false);
+ // find non-glob files that have no matching file in the project and are not excluded by any exclude pattern
+ for (var _i = 0, _b = filterFiles(files, exclude, false); _i < _b.length; _i++) {
+ var file = _b[_i];
+ if (!glob.hasMagic(file) && !filesFound.some(minimatch_1.filter(file))) {
+ if (fs.existsSync(file)) {
+ throw new error_1.FatalError("'" + file + "' is not included in project.");
+ }
+ logger.error("'" + file + "' does not exist. This will be an error in TSLint 6.\n"); // TODO make this an error in v6.0.0
+ }
+ }
}
return { files: filesFound, program: program };
}
-function resolveGlobs(files, ignore, outputAbsolutePaths) {
- return utils_1.flatMap(utils_1.arrayify(files), function (file) {
- return glob.sync(trimSingleQuotes(file), { ignore: ignore, nodir: true });
- })
- .map(function (file) { return outputAbsolutePaths ? path.resolve(file) : path.relative(process.cwd(), file); });
+function filterFiles(files, patterns, include) {
+ if (patterns.length === 0) {
+ return include ? [] : files;
+ }
+ var matcher = patterns.map(function (pattern) { return new minimatch_1.Minimatch(pattern, { dot: !include }); }); // `glob` always enables `dot` for ignore patterns
+ return files.filter(function (file) { return include === matcher.some(function (pattern) { return pattern.match(file); }); });
+}
+function resolveGlobs(files, ignore, outputAbsolutePaths, logger) {
+ var results = utils_1.flatMap(files, function (file) { return glob.sync(trimSingleQuotes(file), { ignore: ignore, nodir: true }); });
+ // warn if `files` contains non-existent files, that are not patters and not excluded by any of the exclude patterns
+ for (var _i = 0, _a = filterFiles(files, ignore, false); _i < _a.length; _i++) {
+ var file = _a[_i];
+ if (!glob.hasMagic(file) && !results.some(minimatch_1.filter(file))) {
+ logger.error("'" + file + "' does not exist. This will be an error in TSLint 6.\n"); // TODO make this an error in v6.0.0
+ }
+ }
+ var cwd = process.cwd();
+ return results.map(function (file) { return outputAbsolutePaths ? path.resolve(cwd, file) : path.relative(cwd, file); });
}
function doLinting(options, files, program, logger) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
- var possibleConfigAbsolutePath, linter, lastFolder, configFile, isFileExcluded, _i, files_1, file, contents, folder;
+ function isFileExcluded(filepath) {
+ if (configFile === undefined || configFile.linterOptions == undefined || configFile.linterOptions.exclude == undefined) {
+ return false;
+ }
+ var fullPath = path.resolve(filepath);
+ return configFile.linterOptions.exclude.some(function (pattern) { return new minimatch_1.Minimatch(pattern).match(fullPath); });
+ }
+ var linter, lastFolder, configFile, _i, files_1, file, folder, contents, sourceFile;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
- possibleConfigAbsolutePath = options.config !== undefined ? path.resolve(options.config) : null;
- linter = new Linter({
+ linter = new linter_1.Linter({
fix: !!options.fix,
formatter: options.format,
formattersDirectory: options.formattersDirectory,
rulesDirectory: options.rulesDirectory,
}, program);
- isFileExcluded = function (filepath) {
- if (configFile === undefined || configFile.linterOptions == undefined || configFile.linterOptions.exclude == undefined) {
- return false;
- }
- var fullPath = path.resolve(filepath);
- return configFile.linterOptions.exclude.some(function (pattern) { return new minimatch_1.Minimatch(pattern).match(fullPath); });
- };
+ configFile = options.config !== undefined ? configuration_1.findConfiguration(options.config).results : undefined;
_i = 0, files_1 = files;
_a.label = 1;
case 1:
- if (!(_i < files_1.length)) return [3 /*break*/, 4];
+ if (!(_i < files_1.length)) return [3 /*break*/, 6];
file = files_1[_i];
- if (!fs.existsSync(file)) {
- throw new error_1.FatalError("Unable to open file: " + file);
- }
- return [4 /*yield*/, tryReadFile(file, logger)];
- case 2:
- contents = _a.sent();
- if (contents !== undefined) {
+ if (options.config === undefined) {
folder = path.dirname(file);
if (lastFolder !== folder) {
- configFile = configuration_1.findConfiguration(possibleConfigAbsolutePath, folder).results;
+ configFile = configuration_1.findConfiguration(null, folder).results;
lastFolder = folder;
}
- if (!isFileExcluded(file)) {
- linter.lint(file, contents, configFile);
- }
}
- _a.label = 3;
+ if (isFileExcluded(file)) {
+ return [3 /*break*/, 5];
+ }
+ contents = void 0;
+ if (!(program !== undefined)) return [3 /*break*/, 2];
+ sourceFile = program.getSourceFile(file);
+ if (sourceFile !== undefined) {
+ contents = sourceFile.text;
+ }
+ return [3 /*break*/, 4];
+ case 2: return [4 /*yield*/, tryReadFile(file, logger)];
case 3:
+ contents = _a.sent();
+ _a.label = 4;
+ case 4:
+ if (contents !== undefined) {
+ linter.lint(file, contents, configFile);
+ }
+ _a.label = 5;
+ case 5:
_i++;
return [3 /*break*/, 1];
- case 4: return [2 /*return*/, linter.getResult()];
+ case 6: return [2 /*return*/, linter.getResult()];
}
});
});
@@ -191,15 +222,18 @@ function tryReadFile(filename, logger) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var buffer, fd;
return tslib_1.__generator(this, function (_a) {
- buffer = new Buffer(256);
+ if (!fs.existsSync(filename)) {
+ throw new error_1.FatalError("Unable to open file: " + filename);
+ }
+ buffer = Buffer.allocUnsafe(256);
fd = fs.openSync(filename, "r");
try {
fs.readSync(fd, buffer, 0, 256, 0);
- if (buffer.readInt8(0, true) === 0x47 && buffer.readInt8(188, true) === 0x47) {
+ 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.
- logger.error(filename + ": ignoring MPEG transport stream");
+ logger.error(filename + ": ignoring MPEG transport stream\n");
return [2 /*return*/, undefined];
}
}