1 | "use strict";
|
---|
2 | /**
|
---|
3 | * @license
|
---|
4 | * Copyright Google LLC All Rights Reserved.
|
---|
5 | *
|
---|
6 | * Use of this source code is governed by an MIT-style license that can be
|
---|
7 | * found in the LICENSE file at https://angular.io/license
|
---|
8 | */
|
---|
9 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
---|
10 | if (k2 === undefined) k2 = k;
|
---|
11 | Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
---|
12 | }) : (function(o, m, k, k2) {
|
---|
13 | if (k2 === undefined) k2 = k;
|
---|
14 | o[k2] = m[k];
|
---|
15 | }));
|
---|
16 | var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
---|
17 | Object.defineProperty(o, "default", { enumerable: true, value: v });
|
---|
18 | }) : function(o, v) {
|
---|
19 | o["default"] = v;
|
---|
20 | });
|
---|
21 | var __importStar = (this && this.__importStar) || function (mod) {
|
---|
22 | if (mod && mod.__esModule) return mod;
|
---|
23 | var result = {};
|
---|
24 | if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
---|
25 | __setModuleDefault(result, mod);
|
---|
26 | return result;
|
---|
27 | };
|
---|
28 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
29 | const architect_1 = require("@angular-devkit/architect");
|
---|
30 | const fs_1 = require("fs");
|
---|
31 | const glob = __importStar(require("glob"));
|
---|
32 | const minimatch_1 = require("minimatch");
|
---|
33 | const path = __importStar(require("path"));
|
---|
34 | const strip_bom_1 = require("../utils/strip-bom");
|
---|
35 | async function _run(options, context) {
|
---|
36 | context.logger.warn(`TSLint's support is discontinued and we're deprecating its support in Angular CLI.\n` +
|
---|
37 | 'To opt-in using the community driven ESLint builder, see: https://github.com/angular-eslint/angular-eslint#migrating-an-angular-cli-project-from-codelyzer-and-tslint.');
|
---|
38 | const systemRoot = context.workspaceRoot;
|
---|
39 | process.chdir(context.currentDirectory);
|
---|
40 | const projectName = (context.target && context.target.project) || '<???>';
|
---|
41 | // Print formatter output only for non human-readable formats.
|
---|
42 | const printInfo = ['prose', 'verbose', 'stylish'].includes(options.format || '') && !options.silent;
|
---|
43 | context.reportStatus(`Linting ${JSON.stringify(projectName)}...`);
|
---|
44 | if (printInfo) {
|
---|
45 | context.logger.info(`Linting ${JSON.stringify(projectName)}...`);
|
---|
46 | }
|
---|
47 | if (!options.tsConfig && options.typeCheck) {
|
---|
48 | throw new Error('A "project" must be specified to enable type checking.');
|
---|
49 | }
|
---|
50 | let tslint;
|
---|
51 | try {
|
---|
52 | tslint = await Promise.resolve().then(() => __importStar(require('tslint')));
|
---|
53 | }
|
---|
54 | catch {
|
---|
55 | throw new Error('Unable to find TSLint. Ensure TSLint is installed.');
|
---|
56 | }
|
---|
57 | const tslintConfigPath = options.tslintConfig
|
---|
58 | ? path.resolve(systemRoot, options.tslintConfig)
|
---|
59 | : null;
|
---|
60 | const Linter = tslint.Linter;
|
---|
61 | let result = undefined;
|
---|
62 | if (options.tsConfig) {
|
---|
63 | const tsConfigs = Array.isArray(options.tsConfig) ? options.tsConfig : [options.tsConfig];
|
---|
64 | context.reportProgress(0, tsConfigs.length);
|
---|
65 | const allPrograms = tsConfigs.map((tsConfig) => {
|
---|
66 | return Linter.createProgram(path.resolve(systemRoot, tsConfig));
|
---|
67 | });
|
---|
68 | let i = 0;
|
---|
69 | for (const program of allPrograms) {
|
---|
70 | const partial = await _lint(tslint, systemRoot, tslintConfigPath, options, program, allPrograms);
|
---|
71 | if (result === undefined) {
|
---|
72 | result = partial;
|
---|
73 | }
|
---|
74 | else {
|
---|
75 | result.failures = result.failures
|
---|
76 | .filter((curr) => {
|
---|
77 | return !partial.failures.some((prev) => curr.equals(prev));
|
---|
78 | })
|
---|
79 | .concat(partial.failures);
|
---|
80 | // we are not doing much with 'errorCount' and 'warningCount'
|
---|
81 | // apart from checking if they are greater than 0 thus no need to dedupe these.
|
---|
82 | result.errorCount += partial.errorCount;
|
---|
83 | result.warningCount += partial.warningCount;
|
---|
84 | result.fileNames = [...new Set([...result.fileNames, ...partial.fileNames])];
|
---|
85 | if (partial.fixes) {
|
---|
86 | result.fixes = result.fixes ? result.fixes.concat(partial.fixes) : partial.fixes;
|
---|
87 | }
|
---|
88 | }
|
---|
89 | context.reportProgress(++i, allPrograms.length);
|
---|
90 | }
|
---|
91 | }
|
---|
92 | else {
|
---|
93 | result = await _lint(tslint, systemRoot, tslintConfigPath, options);
|
---|
94 | }
|
---|
95 | if (result == undefined) {
|
---|
96 | throw new Error('Invalid lint configuration. Nothing to lint.');
|
---|
97 | }
|
---|
98 | if (!options.silent) {
|
---|
99 | const Formatter = tslint.findFormatter(options.format || '');
|
---|
100 | if (!Formatter) {
|
---|
101 | throw new Error(`Invalid lint format "${options.format}".`);
|
---|
102 | }
|
---|
103 | const formatter = new Formatter();
|
---|
104 | const output = formatter.format(result.failures, result.fixes, result.fileNames);
|
---|
105 | if (output.trim()) {
|
---|
106 | context.logger.info(output);
|
---|
107 | }
|
---|
108 | }
|
---|
109 | if (result.warningCount > 0 && printInfo) {
|
---|
110 | context.logger.warn('Lint warnings found in the listed files.');
|
---|
111 | }
|
---|
112 | if (result.errorCount > 0 && printInfo) {
|
---|
113 | context.logger.error('Lint errors found in the listed files.');
|
---|
114 | }
|
---|
115 | if (result.warningCount === 0 && result.errorCount === 0 && printInfo) {
|
---|
116 | context.logger.info('All files pass linting.');
|
---|
117 | }
|
---|
118 | return {
|
---|
119 | success: options.force || result.errorCount === 0,
|
---|
120 | };
|
---|
121 | }
|
---|
122 | /** @deprecated since version 11 as part of the TSLint deprecation. */
|
---|
123 | exports.default = architect_1.createBuilder(_run);
|
---|
124 | async function _lint(projectTslint, systemRoot, tslintConfigPath, options, program, allPrograms) {
|
---|
125 | const Linter = projectTslint.Linter;
|
---|
126 | const Configuration = projectTslint.Configuration;
|
---|
127 | const files = getFilesToLint(systemRoot, options, Linter, program);
|
---|
128 | const lintOptions = {
|
---|
129 | fix: !!options.fix,
|
---|
130 | formatter: options.format,
|
---|
131 | };
|
---|
132 | const linter = new Linter(lintOptions, program);
|
---|
133 | let lastDirectory = undefined;
|
---|
134 | let configLoad;
|
---|
135 | const lintedFiles = [];
|
---|
136 | for (const file of files) {
|
---|
137 | if (program && allPrograms) {
|
---|
138 | // If it cannot be found in ANY program, then this is an error.
|
---|
139 | if (allPrograms.every((p) => p.getSourceFile(file) === undefined)) {
|
---|
140 | throw new Error(`File ${JSON.stringify(file)} is not part of a TypeScript project '${options.tsConfig}'.`);
|
---|
141 | }
|
---|
142 | else if (program.getSourceFile(file) === undefined) {
|
---|
143 | // The file exists in some other programs. We will lint it later (or earlier) in the loop.
|
---|
144 | continue;
|
---|
145 | }
|
---|
146 | }
|
---|
147 | const contents = getFileContents(file);
|
---|
148 | // Only check for a new tslint config if the path changes.
|
---|
149 | const currentDirectory = path.dirname(file);
|
---|
150 | if (currentDirectory !== lastDirectory) {
|
---|
151 | configLoad = Configuration.findConfiguration(tslintConfigPath, file);
|
---|
152 | lastDirectory = currentDirectory;
|
---|
153 | }
|
---|
154 | if (configLoad) {
|
---|
155 | // Give some breathing space to other promises that might be waiting.
|
---|
156 | await Promise.resolve();
|
---|
157 | linter.lint(file, contents, configLoad.results);
|
---|
158 | lintedFiles.push(file);
|
---|
159 | }
|
---|
160 | }
|
---|
161 | return {
|
---|
162 | ...linter.getResult(),
|
---|
163 | fileNames: lintedFiles,
|
---|
164 | };
|
---|
165 | }
|
---|
166 | function getFilesToLint(root, options, linter, program) {
|
---|
167 | const ignore = options.exclude;
|
---|
168 | const files = options.files || [];
|
---|
169 | if (files.length > 0) {
|
---|
170 | return files
|
---|
171 | .map((file) => glob.sync(file, { cwd: root, ignore, nodir: true }))
|
---|
172 | .reduce((prev, curr) => prev.concat(curr), [])
|
---|
173 | .map((file) => path.join(root, file));
|
---|
174 | }
|
---|
175 | if (!program) {
|
---|
176 | return [];
|
---|
177 | }
|
---|
178 | let programFiles = linter.getFileNames(program);
|
---|
179 | if (ignore && ignore.length > 0) {
|
---|
180 | // normalize to support ./ paths
|
---|
181 | const ignoreMatchers = ignore.map((pattern) => new minimatch_1.Minimatch(path.normalize(pattern), { dot: true }));
|
---|
182 | programFiles = programFiles.filter((file) => !ignoreMatchers.some((matcher) => matcher.match(path.relative(root, file))));
|
---|
183 | }
|
---|
184 | return programFiles;
|
---|
185 | }
|
---|
186 | function getFileContents(file) {
|
---|
187 | // NOTE: The tslint CLI checks for and excludes MPEG transport streams; this does not.
|
---|
188 | try {
|
---|
189 | return strip_bom_1.stripBom(fs_1.readFileSync(file, 'utf-8'));
|
---|
190 | }
|
---|
191 | catch {
|
---|
192 | throw new Error(`Could not read file '${file}'.`);
|
---|
193 | }
|
---|
194 | }
|
---|