.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / runner.js
1 "use strict";
2 /**
3  * @license
4  * Copyright 2013 Palantir Technologies, Inc.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 Object.defineProperty(exports, "__esModule", { value: true });
19 var tslib_1 = require("tslib");
20 // tslint:disable strict-boolean-expressions (TODO: Fix up options)
21 var fs = require("fs");
22 var path = require("path");
23 var ts = require("typescript");
24 var configuration_1 = require("./configuration");
25 var error_1 = require("./error");
26 var reading_1 = require("./files/reading");
27 var resolution_1 = require("./files/resolution");
28 var linter_1 = require("./linter");
29 var utils_1 = require("./utils");
30 function run(options, logger) {
31     return tslib_1.__awaiter(this, void 0, void 0, function () {
32         var error_2;
33         return tslib_1.__generator(this, function (_a) {
34             switch (_a.label) {
35                 case 0:
36                     _a.trys.push([0, 2, , 3]);
37                     return [4 /*yield*/, runWorker(options, logger)];
38                 case 1: return [2 /*return*/, _a.sent()];
39                 case 2:
40                     error_2 = _a.sent();
41                     if (error_2 instanceof error_1.FatalError) {
42                         logger.error(error_2.message + "\n");
43                         return [2 /*return*/, 1 /* FatalError */];
44                     }
45                     throw error_2;
46                 case 3: return [2 /*return*/];
47             }
48         });
49     });
50 }
51 exports.run = run;
52 function runWorker(options, logger) {
53     return tslib_1.__awaiter(this, void 0, void 0, function () {
54         var test_1, results, _a, output, errorCount;
55         return tslib_1.__generator(this, function (_b) {
56             switch (_b.label) {
57                 case 0:
58                     if (options.init) {
59                         if (fs.existsSync(configuration_1.JSON_CONFIG_FILENAME)) {
60                             throw new error_1.FatalError("Cannot generate " + configuration_1.JSON_CONFIG_FILENAME + ": file already exists");
61                         }
62                         fs.writeFileSync(configuration_1.JSON_CONFIG_FILENAME, JSON.stringify(configuration_1.DEFAULT_CONFIG, undefined, "    "));
63                         return [2 /*return*/, 0 /* Ok */];
64                     }
65                     if (options.printConfig) {
66                         return [2 /*return*/, printConfiguration(options, logger)];
67                     }
68                     if (!options.test) return [3 /*break*/, 2];
69                     return [4 /*yield*/, Promise.resolve().then(function () { return require("./test"); })];
70                 case 1:
71                     test_1 = _b.sent();
72                     results = test_1.runTests((options.files || []).map(utils_1.trimSingleQuotes), options.rulesDirectory);
73                     return [2 /*return*/, test_1.consoleTestResultsHandler(results, logger) ? 0 /* Ok */ : 1 /* FatalError */];
74                 case 2:
75                     if (options.config && !fs.existsSync(options.config)) {
76                         throw new error_1.FatalError("Invalid option for configuration: " + options.config);
77                     }
78                     return [4 /*yield*/, runLinter(options, logger)];
79                 case 3:
80                     _a = _b.sent(), output = _a.output, errorCount = _a.errorCount;
81                     if (output && output.trim()) {
82                         logger.log(output + "\n");
83                     }
84                     return [2 /*return*/, options.force || errorCount === 0 ? 0 /* Ok */ : 2 /* LintError */];
85             }
86         });
87     });
88 }
89 function printConfiguration(options, logger) {
90     return tslib_1.__awaiter(this, void 0, void 0, function () {
91         var files, configurationPath, configuration;
92         return tslib_1.__generator(this, function (_a) {
93             files = options.files;
94             if (files.length !== 1) {
95                 throw new error_1.FatalError("--print-config must be run with exactly one file");
96             }
97             configurationPath = options.config === undefined ? configuration_1.findConfigurationPath(null, files[0]) : options.config;
98             if (configurationPath === undefined) {
99                 throw new error_1.FatalError("Could not find configuration path. Try passing a --config to your tslint.json.");
100             }
101             configuration = configuration_1.findConfiguration(configurationPath, files[0]).results;
102             if (configuration === undefined) {
103                 throw new error_1.FatalError("Could not find configuration for '" + files[1]);
104             }
105             logger.log(configuration_1.stringifyConfiguration(configuration) + "\n");
106             return [2 /*return*/, 0 /* Ok */];
107         });
108     });
109 }
110 function runLinter(options, logger) {
111     return tslib_1.__awaiter(this, void 0, void 0, function () {
112         var _a, files, program, diagnostics, message;
113         return tslib_1.__generator(this, function (_b) {
114             _a = resolution_1.resolveFilesAndProgram(options, logger), files = _a.files, program = _a.program;
115             // if type checking, run the type checker
116             if (program && options.typeCheck) {
117                 diagnostics = ts.getPreEmitDiagnostics(program);
118                 if (diagnostics.length !== 0) {
119                     message = diagnostics
120                         .map(function (d) { return showDiagnostic(d, program, options.outputAbsolutePaths); })
121                         .join("\n");
122                     if (options.force) {
123                         logger.error(message + "\n");
124                     }
125                     else {
126                         throw new error_1.FatalError(message);
127                     }
128                 }
129             }
130             return [2 /*return*/, doLinting(options, files, program, logger)];
131         });
132     });
133 }
134 function doLinting(options, files, program, logger) {
135     return tslib_1.__awaiter(this, void 0, void 0, function () {
136         var configFile, formatter, linter, lastFolder, _i, files_1, file, folder, contents, sourceFile;
137         return tslib_1.__generator(this, function (_a) {
138             switch (_a.label) {
139                 case 0:
140                     configFile = options.config !== undefined ? configuration_1.findConfiguration(options.config).results : undefined;
141                     formatter = options.format;
142                     if (formatter === undefined) {
143                         formatter =
144                             configFile && configFile.linterOptions && configFile.linterOptions.format
145                                 ? configFile.linterOptions.format
146                                 : "prose";
147                     }
148                     linter = new linter_1.Linter({
149                         fix: !!options.fix,
150                         formatter: formatter,
151                         formattersDirectory: options.formattersDirectory,
152                         quiet: !!options.quiet,
153                         rulesDirectory: options.rulesDirectory,
154                     }, program);
155                     _i = 0, files_1 = files;
156                     _a.label = 1;
157                 case 1:
158                     if (!(_i < files_1.length)) return [3 /*break*/, 6];
159                     file = files_1[_i];
160                     if (options.config === undefined) {
161                         folder = path.dirname(file);
162                         if (lastFolder !== folder) {
163                             configFile = configuration_1.findConfiguration(null, folder).results;
164                             lastFolder = folder;
165                         }
166                     }
167                     if (configuration_1.isFileExcluded(file, configFile)) {
168                         return [3 /*break*/, 5];
169                     }
170                     contents = void 0;
171                     if (!(program !== undefined)) return [3 /*break*/, 2];
172                     sourceFile = program.getSourceFile(file);
173                     if (sourceFile !== undefined) {
174                         contents = sourceFile.text;
175                     }
176                     return [3 /*break*/, 4];
177                 case 2: return [4 /*yield*/, reading_1.tryReadFile(file, logger)];
178                 case 3:
179                     contents = _a.sent();
180                     _a.label = 4;
181                 case 4:
182                     if (contents !== undefined) {
183                         linter.lint(file, contents, configFile);
184                     }
185                     _a.label = 5;
186                 case 5:
187                     _i++;
188                     return [3 /*break*/, 1];
189                 case 6: return [2 /*return*/, linter.getResult()];
190             }
191         });
192     });
193 }
194 function showDiagnostic(_a, program, outputAbsolutePaths) {
195     var file = _a.file, start = _a.start, category = _a.category, messageText = _a.messageText;
196     var message = ts.DiagnosticCategory[category];
197     if (file !== undefined && start !== undefined) {
198         var _b = file.getLineAndCharacterOfPosition(start), line = _b.line, character = _b.character;
199         var currentDirectory = program.getCurrentDirectory();
200         var filePath = outputAbsolutePaths
201             ? path.resolve(currentDirectory, file.fileName)
202             : path.relative(currentDirectory, file.fileName);
203         message += " at " + filePath + ":" + (line + 1) + ":" + (character + 1) + ":";
204     }
205     return message + " " + ts.flattenDiagnosticMessageText(messageText, "\n");
206 }