Skip to content

Commit 29c8c8a

Browse files
clydinfilipesilva
authored andcommitted
refactor(@angular/cli): simplify lint file discovery
1 parent 0f0d7ed commit 29c8c8a

File tree

3 files changed

+24
-16
lines changed

3 files changed

+24
-16
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
"lodash": "^4.11.1",
7171
"magic-string": "^0.22.3",
7272
"memory-fs": "^0.4.1",
73+
"minimatch": "^3.0.4",
7374
"node-modules-path": "^1.0.0",
7475
"nopt": "^4.0.1",
7576
"opn": "~5.1.0",

packages/@angular/cli/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"license-webpack-plugin": "^1.0.0",
5555
"lodash": "^4.11.1",
5656
"memory-fs": "^0.4.1",
57+
"minimatch": "^3.0.4",
5758
"node-modules-path": "^1.0.0",
5859
"nopt": "^4.0.1",
5960
"opn": "~5.1.0",

packages/@angular/cli/tasks/lint.ts

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import chalk from 'chalk';
44
import * as fs from 'fs';
55
import * as glob from 'glob';
6+
import { Minimatch } from 'minimatch';
67
import * as path from 'path';
78
import { satisfies } from 'semver';
89
import * as ts from 'typescript';
@@ -17,6 +18,7 @@ const Task = require('../ember-cli/lib/models/task');
1718
export interface CliLintConfig {
1819
files?: (string | string[]);
1920
project?: string;
21+
projectOnly?: boolean;
2022
tslintConfig?: string;
2123
exclude?: (string | string[]);
2224
}
@@ -137,34 +139,38 @@ export default Task.extend({
137139
}
138140
});
139141

142+
function normalizeArrayOption<T>(option: T | Array<T>): Array<T> {
143+
return Array.isArray(option) ? option : [option];
144+
}
145+
140146
function getFilesToLint(
141147
program: ts.Program,
142148
lintConfig: CliLintConfig,
143149
linter: typeof tslint.Linter,
144150
): string[] {
145-
let files: string[] = [];
151+
const providedFiles = lintConfig.files && normalizeArrayOption(lintConfig.files);
152+
const ignore = lintConfig.exclude && normalizeArrayOption(lintConfig.exclude);
146153

147-
if (lintConfig.files) {
148-
files = Array.isArray(lintConfig.files) ? lintConfig.files : [lintConfig.files];
149-
} else if (program) {
150-
files = linter.getFileNames(program);
154+
if (providedFiles) {
155+
return providedFiles
156+
.map(file => glob.sync(file, { ignore, nodir: true }))
157+
.reduce((prev, curr) => prev.concat(curr), []);
151158
}
152159

153-
let globOptions = {};
160+
if (!program) {
161+
return [];
162+
}
154163

155-
if (lintConfig.exclude) {
156-
const excludePatterns = Array.isArray(lintConfig.exclude)
157-
? lintConfig.exclude
158-
: [lintConfig.exclude];
164+
let programFiles = linter.getFileNames(program);
159165

160-
globOptions = { ignore: excludePatterns, nodir: true };
161-
}
166+
if (ignore && ignore.length > 0) {
167+
const ignoreMatchers = ignore.map(pattern => new Minimatch(pattern));
162168

163-
files = files
164-
.map((file: string) => glob.sync(file, globOptions))
165-
.reduce((a: string[], b: string[]) => a.concat(b), []);
169+
programFiles = programFiles
170+
.filter(file => !ignoreMatchers.some(matcher => matcher.match(file)));
171+
}
166172

167-
return files;
173+
return programFiles;
168174
}
169175

170176
function getFileContents(file: string): string {

0 commit comments

Comments
 (0)