Skip to content

Commit

Permalink
Fix: Newline before eof when creating config via --init (eslint#12952)
Browse files Browse the repository at this point in the history
* Fix: Newline before eof when creating config via --init

* Add test

* Make a dedicated test (with a bit of duplication)
  • Loading branch information
papandreou authored and anikethsaha committed Mar 23, 2020
1 parent d21b2dc commit 09dee06
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/init/config-file.js
Expand Up @@ -45,7 +45,7 @@ function sortByKey(a, b) {
function writeJSONConfigFile(config, filePath) {
debug(`Writing JSON config file: ${filePath}`);

const content = stringify(config, { cmp: sortByKey, space: 4 });
const content = `${stringify(config, { cmp: sortByKey, space: 4 })}\n`;

fs.writeFileSync(filePath, content, "utf8");
}
Expand Down Expand Up @@ -80,7 +80,7 @@ function writeJSConfigFile(config, filePath) {
debug(`Writing JS config file: ${filePath}`);

let contentToWrite;
const stringifiedContent = `module.exports = ${stringify(config, { cmp: sortByKey, space: 4 })};`;
const stringifiedContent = `module.exports = ${stringify(config, { cmp: sortByKey, space: 4 })};\n`;

try {
const { CLIEngine } = require("../cli-engine");
Expand Down
15 changes: 15 additions & 0 deletions tests/lib/init/config-file.js
Expand Up @@ -82,6 +82,21 @@ describe("ConfigFile", () => {
StubbedConfigFile.write(config, filename);
});

it("should include a newline character at EOF", () => {
const fakeFS = leche.fake(fs);

sinon.mock(fakeFS).expects("writeFileSync").withExactArgs(
filename,
sinon.match(value => value.endsWith("\n")),
"utf8"
);

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

StubbedConfigFile.write(config, filename);
});
});

it("should make sure js config files match linting rules", () => {
Expand Down

0 comments on commit 09dee06

Please sign in to comment.