From 889ed4adf780fd5dc310e74279de71a9f0487425 Mon Sep 17 00:00:00 2001 From: Brian Kurek Date: Wed, 30 Jan 2019 15:37:10 -0500 Subject: [PATCH] Update: Make --init run js config files through linter (fixes #9947) --- lib/config/config-file.js | 19 +++++++++++++++++-- tests/lib/config/config-file.js | 22 ++++++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/lib/config/config-file.js b/lib/config/config-file.js index dde79cb40c8..af62ef7bae1 100644 --- a/lib/config/config-file.js +++ b/lib/config/config-file.js @@ -286,9 +286,24 @@ function writeYAMLConfigFile(config, filePath) { function writeJSConfigFile(config, filePath) { debug(`Writing JS config file: ${filePath}`); - const content = `module.exports = ${stringify(config, { cmp: sortByKey, space: 4 })};`; + let contentToWrite; + const stringifiedContent = `module.exports = ${stringify(config, { cmp: sortByKey, space: 4 })};`; - fs.writeFileSync(filePath, content, "utf8"); + try { + const CLIEngine = require("../cli-engine"); + const linter = new CLIEngine({ + baseConfig: config, + fix: true, + useEslintrc: false + }); + const report = linter.executeOnText(stringifiedContent); + + contentToWrite = report.results[0].output || stringifiedContent; + } catch (e) { + contentToWrite = stringifiedContent; + } + + fs.writeFileSync(filePath, contentToWrite, "utf8"); } /** diff --git a/tests/lib/config/config-file.js b/tests/lib/config/config-file.js index 944ed84260f..b367be824a4 100644 --- a/tests/lib/config/config-file.js +++ b/tests/lib/config/config-file.js @@ -1262,6 +1262,28 @@ describe("ConfigFile", () => { }); + it("should make sure js config files match linting rules", () => { + const fakeFS = leche.fake(fs); + + const singleQuoteConfig = { + rules: { + quotes: [2, "single"] + } + }; + + sandbox.mock(fakeFS).expects("writeFileSync").withExactArgs( + "test-config.js", + sinon.match(value => !value.includes("\"")), + "utf8" + ); + + const StubbedConfigFile = proxyquire("../../../lib/config/config-file", { + fs: fakeFS + }); + + StubbedConfigFile.write(singleQuoteConfig, "test-config.js"); + }); + it("should throw error if file extension is not valid", () => { assert.throws(() => { ConfigFile.write({}, getFixturePath("yaml/.eslintrc.class"));