Skip to content

Commit

Permalink
fix(@angular/cli): ensure lint generates well-formed machine output
Browse files Browse the repository at this point in the history
For machine readable output formats, ng lint was concatenating multiple documents into a single file. This resulted in invalid JSON, XML, etc. The lint results are now aggregated and one full output is generated.

Fix angular#5259
Close angular#5224
  • Loading branch information
Charles Lyding authored and Zhicheng Wang committed Mar 16, 2017
1 parent 947af38 commit 5b6cad8
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions packages/@angular/cli/tasks/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,8 @@ export default Task.extend({
const Linter = tslint.Linter;
const Configuration = tslint.Configuration;

let errors = 0;
let results = '';

lintConfigs
.forEach((config) => {
const result = lintConfigs
.map((config) => {
const program: ts.Program = Linter.createProgram(config.project);
const files = getFilesToLint(program, config, Linter);

Expand All @@ -56,19 +53,37 @@ export default Task.extend({
linter.lint(file, fileContents, configLoad.results);
});

const result = linter.getResult();
errors += result.failureCount;
results = results.concat(result.output.trim().concat('\n'));
return linter.getResult();
})
.reduce((total, current) => {
const failures = current.failures
.filter((cf: any) => !total.failures.some((ef: any) => ef.equals(cf)));
total.failures = total.failures.concat(...failures);

if (current.fixes) {
total.fixes = (total.fixes || []).concat(...current.fixes);
}
return total;
}, {
failures: [],
fixes: undefined
});

const Formatter = tslint.findFormatter(commandOptions.format);
const formatter = new Formatter();

const output = formatter.format(result.failures, result.fixes);
if (output) {
ui.writeLine(output);
}

// print formatter output directly for non human-readable formats
if (['prose', 'verbose', 'stylish'].indexOf(commandOptions.format) == -1) {
ui.writeLine(results.trim());
return (errors == 0 || commandOptions.force) ? Promise.resolve(0) : Promise.resolve(2);
return (result.failures.length == 0 || commandOptions.force)
? Promise.resolve(0) : Promise.resolve(2);
}

if (errors > 0) {
ui.writeLine(results.trim());
if (result.failures.length > 0) {
ui.writeLine(chalk.red('Lint errors found in the listed files.'));
return commandOptions.force ? Promise.resolve(0) : Promise.resolve(2);
}
Expand Down

0 comments on commit 5b6cad8

Please sign in to comment.