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: addresses #9947 #10653

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
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");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure using Linter here will work correctly when the generated config contains something like extends (i.e. when the user says they want to follow a popular styleguide). The second argument of Linter#verifyAndFix is supposed to be a "fully-resolved" config (i.e. it just has rules, without depending on any external plugins or extends clauses).

It might be better to use the CLIEngine#executeOnText API instead. For example, maybe something like this would work:

const results = new CLIEngine({ useEslintrc: false, ignore: false, baseConfig: config, fix: true })
    .executeOnText(content, ".eslintrc.js")

// ... extract output from results


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");
});
});

});