Skip to content

Commit

Permalink
Update: Make --init run js config files through linter (fixes eslint#…
Browse files Browse the repository at this point in the history
  • Loading branch information
BRKurek committed Jan 30, 2019
1 parent c1fd6f5 commit 889ed4a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
19 changes: 17 additions & 2 deletions lib/config/config-file.js
Expand Up @@ -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");
}

/**
Expand Down
22 changes: 22 additions & 0 deletions tests/lib/config/config-file.js
Expand Up @@ -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"));
Expand Down

0 comments on commit 889ed4a

Please sign in to comment.