Skip to content

Commit

Permalink
Update: addresses eslint#9947
Browse files Browse the repository at this point in the history
Format newly generated .eslintrc.js file before it is saved.
  • Loading branch information
mbildner committed Jul 23, 2018
1 parent a9c678b commit d69beca
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/config/config-file.js
Expand Up @@ -18,7 +18,8 @@ const fs = require("fs"),
pathIsInside = require("path-is-inside"),
stripComments = require("strip-json-comments"),
stringify = require("json-stable-stringify-without-jsonify"),
requireUncached = require("require-uncached");
requireUncached = require("require-uncached"),
Linter = require("../linter.js");

const debug = require("debug")("eslint:config-file");

Expand Down Expand Up @@ -288,7 +289,10 @@ function writeJSConfigFile(config, filePath) {

const content = `module.exports = ${stringify(config, { cmp: sortByKey, space: 4 })};`;

fs.writeFileSync(filePath, content, "utf8");
const linter = new Linter();
const lintedContent = linter.verifyAndFix(content, config).output;

fs.writeFileSync(filePath, lintedContent, "utf8");
}

/**
Expand Down
23 changes: 23 additions & 0 deletions tests/lib/config/config-file.js
Expand Up @@ -1267,6 +1267,29 @@ describe("ConfigFile", () => {
ConfigFile.write({}, getFixturePath("yaml/.eslintrc.class"));
}, /write to unknown file type/);
});

it("should format file consistent with config if format is .js", () => {
const fakeFS = leche.fake(fs);

const singleQuoteNoSemiConfig = {
rules: {
quotes: [2, "single"],
semi: [2, "never"]
}
};

sandbox.mock(fakeFS).expects("writeFileSync").withExactArgs(
"dummyfile.js",
sinon.match(value => !(value.includes("\"") || value.includes(";"))),
"utf8"
);

const StubbedConfigFile = proxyquire("../../../lib/config/config-file", {
fs: fakeFS
});

StubbedConfigFile.write(singleQuoteNoSemiConfig, "dummyfile.js");
});
});

});

0 comments on commit d69beca

Please sign in to comment.