From 5b6cad882110776830531e6519d357d6331c04e4 Mon Sep 17 00:00:00 2001 From: Charles Lyding Date: Sat, 4 Mar 2017 09:54:06 -0500 Subject: [PATCH] fix(@angular/cli): ensure lint generates well-formed machine output 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 #5259 Close #5224 --- packages/@angular/cli/tasks/lint.ts | 39 ++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/packages/@angular/cli/tasks/lint.ts b/packages/@angular/cli/tasks/lint.ts index ef63b2dc3d4e..12cf4c4c9725 100644 --- a/packages/@angular/cli/tasks/lint.ts +++ b/packages/@angular/cli/tasks/lint.ts @@ -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); @@ -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); }