Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Update: Make --init run js config files through linter (fixes #9947) (#…
  • Loading branch information
BRKurek authored and not-an-aardvark committed Feb 6, 2019
1 parent 92fc2f4 commit d0e823a
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
27 changes: 25 additions & 2 deletions lib/config/config-file.js
Expand Up @@ -280,15 +280,38 @@ function writeYAMLConfigFile(config, filePath) {
* Writes a configuration file in JavaScript format.
* @param {Object} config The configuration object to write.
* @param {string} filePath The filename to write to.
* @throws {Error} If an error occurs linting the config file contents.
* @returns {void}
* @private
*/
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) {
debug("Error linting JavaScript config file, writing unlinted version");
const errorMessage = e.message;

contentToWrite = stringifiedContent;
e.message = "An error occurred while generating your JavaScript config file. ";
e.message += "A config file was still generated, but the config file itself may not follow your linting rules.";
e.message += `\nError: ${errorMessage}`;
throw e;
} finally {
fs.writeFileSync(filePath, contentToWrite, "utf8");
}
}

/**
Expand Down
46 changes: 46 additions & 0 deletions tests/lib/config/config-file.js
Expand Up @@ -20,6 +20,7 @@ const Module = require("module"),
espree = require("espree"),
ConfigFile = require("../../../lib/config/config-file"),
Linter = require("../../../lib/linter"),
CLIEngine = require("../../../lib/cli-engine"),
Config = require("../../../lib/config");

const userHome = os.homedir();
Expand Down Expand Up @@ -1262,6 +1263,51 @@ 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 still write a js config file even if linting fails", () => {
const fakeFS = leche.fake(fs);
const fakeCLIEngine = sandbox.mock().withExactArgs(sinon.match({
baseConfig: config,
fix: true,
useEslintrc: false
}));

fakeCLIEngine.prototype = leche.fake(CLIEngine.prototype);
sandbox.stub(fakeCLIEngine.prototype, "executeOnText").throws();

sandbox.mock(fakeFS).expects("writeFileSync").once();

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

assert.throws(() => {
StubbedConfigFile.write(config, "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 d0e823a

Please sign in to comment.