.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / files / resolution.js
1 "use strict";
2 /**
3  * @license
4  * Copyright 2019 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 fs = require("fs");
20 var glob = require("glob");
21 var minimatch_1 = require("minimatch");
22 var path = require("path");
23 var error_1 = require("../error");
24 var linter_1 = require("../linter");
25 var utils_1 = require("../utils");
26 function filterFiles(files, patterns, include) {
27     if (patterns.length === 0) {
28         return include ? [] : files;
29     }
30     var matcher = patterns.map(function (pattern) { return new minimatch_1.Minimatch(pattern, { dot: !include }); }); // `glob` always enables `dot` for ignore patterns
31     return files.filter(function (file) { return include === matcher.some(function (pattern) { return pattern.match(file); }); });
32 }
33 exports.filterFiles = filterFiles;
34 function findTsconfig(project) {
35     try {
36         var stats = fs.statSync(project); // throws if file does not exist
37         if (!stats.isDirectory()) {
38             return project;
39         }
40         var projectFile = path.join(project, "tsconfig.json");
41         fs.accessSync(projectFile); // throws if file does not exist
42         return projectFile;
43     }
44     catch (e) {
45         return undefined;
46     }
47 }
48 exports.findTsconfig = findTsconfig;
49 function resolveGlobs(files, ignore, outputAbsolutePaths, logger) {
50     var results = utils_1.flatMap(files, function (file) {
51         return glob.sync(utils_1.trimSingleQuotes(file), { ignore: ignore, nodir: true });
52     });
53     // warn if `files` contains non-existent files, that are not patters and not excluded by any of the exclude patterns
54     for (var _i = 0, _a = filterFiles(files, ignore, false); _i < _a.length; _i++) {
55         var file = _a[_i];
56         if (!glob.hasMagic(file) && !results.some(minimatch_1.filter(file))) {
57             logger.error("'" + file + "' does not exist. This will be an error in TSLint 6.\n"); // TODO make this an error in v6.0.0
58         }
59     }
60     var cwd = process.cwd();
61     return results.map(function (file) {
62         return outputAbsolutePaths ? path.resolve(cwd, file) : path.relative(cwd, file);
63     });
64 }
65 exports.resolveGlobs = resolveGlobs;
66 function resolveFilesAndProgram(_a, logger) {
67     var files = _a.files, project = _a.project, exclude = _a.exclude, outputAbsolutePaths = _a.outputAbsolutePaths;
68     // remove single quotes which break matching on Windows when glob is passed in single quotes
69     exclude = exclude.map(utils_1.trimSingleQuotes);
70     if (project === undefined) {
71         return { files: resolveGlobs(files, exclude, outputAbsolutePaths, logger) };
72     }
73     var projectPath = findTsconfig(project);
74     if (projectPath === undefined) {
75         throw new error_1.FatalError("Invalid option for project: " + project);
76     }
77     exclude = exclude.map(function (pattern) { return path.resolve(pattern); });
78     var program = linter_1.Linter.createProgram(projectPath);
79     var filesFound;
80     if (files.length === 0) {
81         filesFound = filterFiles(linter_1.Linter.getFileNames(program), exclude, false);
82     }
83     else {
84         files = files.map(function (f) { return path.resolve(f); });
85         filesFound = filterFiles(program.getSourceFiles().map(function (f) { return f.fileName; }), files, true);
86         filesFound = filterFiles(filesFound, exclude, false);
87         // find non-glob files that have no matching file in the project and are not excluded by any exclude pattern
88         for (var _i = 0, _b = filterFiles(files, exclude, false); _i < _b.length; _i++) {
89             var file = _b[_i];
90             if (!glob.hasMagic(file) && !filesFound.some(minimatch_1.filter(file))) {
91                 if (fs.existsSync(file)) {
92                     throw new error_1.FatalError("'" + file + "' is not included in project.");
93                 }
94                 logger.error("'" + file + "' does not exist. This will be an error in TSLint 6.\n"); // TODO make this an error in v6.0.0
95             }
96         }
97     }
98     return { files: filesFound, program: program };
99 }
100 exports.resolveFilesAndProgram = resolveFilesAndProgram;