Skip to content

Commit 8035f54

Browse files
Charles Lydingfilipesilva
Charles Lyding
authored andcommitted
feat(@angular/cli): allow lint project setting to be optional
1 parent a5d8bc1 commit 8035f54

File tree

2 files changed

+70
-7
lines changed

2 files changed

+70
-7
lines changed

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

+44-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
const Task = require('../ember-cli/lib/models/task');
21
import * as chalk from 'chalk';
2+
import * as fs from 'fs';
33
import * as glob from 'glob';
4+
import * as path from 'path';
45
import * as ts from 'typescript';
56
import { requireProjectModule } from '../utilities/require-project-module';
67
import { CliConfig } from '../models/config';
78
import { LintCommandOptions } from '../commands/lint';
89

10+
const SilentError = require('silent-error');
11+
const Task = require('../ember-cli/lib/models/task');
12+
913
interface CliLintConfig {
1014
files?: (string | string[]);
1115
project?: string;
@@ -30,7 +34,12 @@ export default Task.extend({
3034

3135
const result = lintConfigs
3236
.map((config) => {
33-
const program: ts.Program = Linter.createProgram(config.project);
37+
let program: ts.Program;
38+
if (config.project) {
39+
program = Linter.createProgram(config.project);
40+
} else if (commandOptions.typeCheck) {
41+
ui.writeLine(chalk.yellow('A "project" must be specified to enable type checking.'));
42+
}
3443
const files = getFilesToLint(program, config, Linter);
3544
const lintOptions = {
3645
fix: commandOptions.fix,
@@ -39,13 +48,21 @@ export default Task.extend({
3948
const lintProgram = commandOptions.typeCheck ? program : undefined;
4049
const linter = new Linter(lintOptions, lintProgram);
4150

51+
let lastDirectory: string;
52+
let configLoad: any;
4253
files.forEach((file) => {
43-
const sourceFile = program.getSourceFile(file);
44-
if (!sourceFile) {
54+
const fileContents = getFileContents(file, program);
55+
if (!fileContents) {
4556
return;
4657
}
47-
const fileContents = sourceFile.getFullText();
48-
const configLoad = Configuration.findConfiguration(config.tslintConfig, file);
58+
59+
// Only check for a new tslint config if path changes
60+
const currentDirectory = path.dirname(file);
61+
if (currentDirectory !== lastDirectory) {
62+
configLoad = Configuration.findConfiguration(config.tslintConfig, file);
63+
lastDirectory = currentDirectory;
64+
}
65+
4966
linter.lint(file, fileContents, configLoad.results);
5067
});
5168

@@ -94,7 +111,7 @@ function getFilesToLint(program: ts.Program, lintConfig: CliLintConfig, Linter:
94111

95112
if (lintConfig.files !== null) {
96113
files = Array.isArray(lintConfig.files) ? lintConfig.files : [lintConfig.files];
97-
} else {
114+
} else if (program) {
98115
files = Linter.getFileNames(program);
99116
}
100117

@@ -114,3 +131,23 @@ function getFilesToLint(program: ts.Program, lintConfig: CliLintConfig, Linter:
114131

115132
return files;
116133
}
134+
135+
function getFileContents(file: string, program?: ts.Program): string {
136+
let contents: string;
137+
138+
if (program) {
139+
const sourceFile = program.getSourceFile(file);
140+
if (sourceFile) {
141+
contents = sourceFile.getFullText();
142+
}
143+
} else {
144+
// NOTE: The tslint CLI checks for and excludes MPEG transport streams; this does not.
145+
try {
146+
contents = fs.readFileSync(file, 'utf8');
147+
} catch (e) {
148+
throw new SilentError(`Could not read file "${file}".`);
149+
}
150+
}
151+
152+
return contents;
153+
}
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { ng } from '../../utils/process';
2+
import { writeFile } from '../../utils/fs';
3+
import { expectToFail } from '../../utils/utils';
4+
import { oneLine } from 'common-tags';
5+
6+
export default function () {
7+
return Promise.resolve()
8+
.then(() => ng('set', 'lint.0.project', ''))
9+
.then(() => ng('lint', '--type-check'))
10+
.then(({ stdout }) => {
11+
if (!stdout.match(/A "project" must be specified to enable type checking./)) {
12+
throw new Error(oneLine`
13+
Expected to match "A "project" must be specified to enable type checking."
14+
in ${stdout}.
15+
`);
16+
}
17+
18+
return stdout;
19+
})
20+
.then(() => ng('set', 'lint.0.files', '"**/baz.ts"'))
21+
.then(() => writeFile('src/app/foo.ts', 'const foo = "";\n'))
22+
.then(() => writeFile('src/app/baz.ts', 'const baz = \'\';\n'))
23+
.then(() => ng('lint'))
24+
.then(() => ng('set', 'lint.0.files', '"**/foo.ts"'))
25+
.then(() => expectToFail(() => ng('lint')));
26+
}

0 commit comments

Comments
 (0)