From 32edfff7aae15654d2cfa4a10da9b8f81e021bce Mon Sep 17 00:00:00 2001 From: Gordon Diggs Date: Tue, 28 Feb 2017 11:48:42 -0500 Subject: [PATCH] Fix error when there are no config files Fixes the following error: TypeError: Reduce of empty array with no initial value at Array.reduce (native) at Function.upgradeInstructions (/usr/src/app/lib/config_upgrader.js:272:20) at Object. (/usr/src/app/bin/eslint.js:239:37) at Module._compile (module.js:556:32) at Object.Module._extensions..js (module.js:565:10) at Module.load (module.js:473:32) at tryModuleLoad (module.js:432:12) at Function.Module._load (module.js:424:3) at Module.runMain (module.js:590:10) at run (bootstrap_node.js:394:7) I couldn't find a good initial value to give to `reduce` to obtain the correct functionality. There's no reason to reduce anyway if there are no reports. --- lib/config_upgrader.js | 6 +++++- test/config_upgrater_test.js | 13 +++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/config_upgrader.js b/lib/config_upgrader.js index 9a4f39004..78cb8d9a6 100644 --- a/lib/config_upgrader.js +++ b/lib/config_upgrader.js @@ -269,7 +269,11 @@ class ConfigUpgrader { return report; }); - return reports.reduce(function(a, b) { return a.concat([""]).concat(b); }); + if (reports.length > 0) { + return reports.reduce(function(a, b) { return a.concat([""]).concat(b); }); + } else { + return reports; + } } } diff --git a/test/config_upgrater_test.js b/test/config_upgrater_test.js index e6cba7123..1f1c356c0 100644 --- a/test/config_upgrater_test.js +++ b/test/config_upgrater_test.js @@ -55,6 +55,19 @@ describe("ConfigUpgrader", function() { }); }); }); + + it("doesn't care if there aren't any configs", function(done) { + temp.mkdir("code ", function(err, directory) { + if (err) { throw err; } + + process.chdir(directory); + + let report = ConfigUpgrader + .upgradeInstructions([directory + '/file.js'], directory); + expect(report).to.deep.eq([]); + done(); + }); + }); });