Skip to content

Commit 3ea8f06

Browse files
clydinBrocco
authored andcommitted
fix(@angular/cli): use explicit error when linting non-program file
1 parent 9bdf498 commit 3ea8f06

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

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

+18-6
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ const Task = require('../ember-cli/lib/models/task');
1818
export interface CliLintConfig {
1919
files?: (string | string[]);
2020
project?: string;
21-
projectOnly?: boolean;
2221
tslintConfig?: string;
2322
exclude?: (string | string[]);
2423
}
@@ -77,8 +76,7 @@ export default Task.extend({
7776
let lastDirectory;
7877
let configLoad;
7978
for (const file of files) {
80-
// The linter retrieves the SourceFile TS node directly if a program is used
81-
const fileContents = program ? undefined : getFileContents(file);
79+
const contents = getFileContents(file, config, program);
8280

8381
// Only check for a new tslint config if path changes
8482
const currentDirectory = path.dirname(file);
@@ -87,7 +85,7 @@ export default Task.extend({
8785
lastDirectory = currentDirectory;
8886
}
8987

90-
linter.lint(file, fileContents, configLoad.results);
88+
linter.lint(file, contents, configLoad.results);
9189
}
9290

9391
return linter.getResult();
@@ -173,11 +171,25 @@ function getFilesToLint(
173171
return programFiles;
174172
}
175173

176-
function getFileContents(file: string): string {
174+
function getFileContents(
175+
file: string,
176+
config: CliLintConfig,
177+
program?: ts.Program,
178+
): string | undefined {
179+
// The linter retrieves the SourceFile TS node directly if a program is used
180+
if (program) {
181+
if (program.getSourceFile(file) == undefined) {
182+
const message = `File '${file}' is not part of the TypeScript project '${config.project}'.`;
183+
throw new SilentError(chalk.red(message));
184+
}
185+
186+
return undefined;
187+
}
188+
177189
// NOTE: The tslint CLI checks for and excludes MPEG transport streams; this does not.
178190
try {
179191
return stripBom(fs.readFileSync(file, 'utf-8'));
180192
} catch (e) {
181-
throw new SilentError(`Could not read file "${file}".`);
193+
throw new SilentError(`Could not read file '${file}'.`);
182194
}
183195
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { ng } from '../../utils/process';
2+
import { expectToFail } from '../../utils/utils';
3+
4+
export default function () {
5+
return Promise.resolve()
6+
.then(() => ng('set', 'lint.0.files', '"src/app/**/*.ts"'))
7+
.then(() => expectToFail(() => ng('lint')));
8+
}

0 commit comments

Comments
 (0)