Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: Make --init run js config files through linter (fixes #9947) #11337

Merged
merged 4 commits into from Feb 6, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 {
platinumazure marked this conversation as resolved.
Show resolved Hide resolved
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