Skip to content

Commit

Permalink
Fix: create .eslintrc.cjs for module type (#14304)
Browse files Browse the repository at this point in the history
* Fix: create `.eslintrc.cjs` for `module` type

* Chore: add tests

* Chore: more test

* Chore: refactor tests

* Chore: refactor tests

* Chore: refactor test

* Chore: refactor tests
  • Loading branch information
snitin315 committed May 6, 2021
1 parent 6791dec commit ee3a3ea
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/init/config-file.js
Expand Up @@ -117,6 +117,7 @@ function writeJSConfigFile(config, filePath) {
function write(config, filePath) {
switch (path.extname(filePath)) {
case ".js":
case ".cjs":
writeJSConfigFile(config, filePath);
break;

Expand Down
12 changes: 12 additions & 0 deletions lib/init/config-initializer.js
Expand Up @@ -12,6 +12,7 @@

const util = require("util"),
path = require("path"),
fs = require("fs"),
enquirer = require("enquirer"),
ProgressBar = require("progress"),
semver = require("semver"),
Expand Down Expand Up @@ -48,6 +49,16 @@ function writeFile(config, format) {
extname = ".yml";
} else if (format === "JSON") {
extname = ".json";
} else if (format === "JavaScript") {
const pkgJSONPath = npmUtils.findPackageJson();

if (pkgJSONPath) {
const pkgJSONContents = JSON.parse(fs.readFileSync(pkgJSONPath, "utf8"));

if (pkgJSONContents.type === "module") {
extname = ".cjs";
}
}
}

const installedESLint = config.installedESLint;
Expand Down Expand Up @@ -684,6 +695,7 @@ const init = {
hasESLintVersionConflict,
installModules,
processAnswers,
writeFile,
/* istanbul ignore next */initializeConfig() {
return promptUser();
}
Expand Down
1 change: 1 addition & 0 deletions lib/init/npm-utils.js
Expand Up @@ -172,6 +172,7 @@ function checkPackageJson(startDir) {
module.exports = {
installSyncSaveDev,
fetchPeerDependencies,
findPackageJson,
checkDeps,
checkDevDeps,
checkPackageJson
Expand Down
119 changes: 119 additions & 0 deletions tests/lib/init/config-initializer.js
Expand Up @@ -27,6 +27,8 @@ const proxyquire = require("proxyquire").noPreserveCache();
//------------------------------------------------------------------------------

let answers = {};
let pkgJSONContents = {};
let pkgJSONPath = "";

describe("configInitializer", () => {

Expand Down Expand Up @@ -455,4 +457,121 @@ describe("configInitializer", () => {
});
});
});

describe("writeFile()", () => {

beforeEach(() => {
answers = {
purpose: "style",
source: "prompt",
extendDefault: true,
indent: 2,
quotes: "single",
linebreak: "unix",
semi: true,
moduleType: "esm",
es6Globals: true,
env: ["browser"],
format: "JSON"
};

pkgJSONContents = {
name: "config-initializer",
version: "1.0.0"
};

process.chdir(fixtureDir);

pkgJSONPath = path.resolve(fixtureDir, "package.json");
});

afterEach(() => {
process.chdir(originalDir);
});

it("should create .eslintrc.json", () => {
const config = init.processAnswers(answers);
const filePath = path.resolve(fixtureDir, ".eslintrc.json");

fs.writeFileSync(pkgJSONPath, JSON.stringify(pkgJSONContents));

init.writeFile(config, answers.format);

assert.isTrue(fs.existsSync(filePath));

fs.unlinkSync(filePath);
fs.unlinkSync(pkgJSONPath);
});

it("should create .eslintrc.js", () => {
answers.format = "JavaScript";

const config = init.processAnswers(answers);
const filePath = path.resolve(fixtureDir, ".eslintrc.js");

fs.writeFileSync(pkgJSONPath, JSON.stringify(pkgJSONContents));

init.writeFile(config, answers.format);

assert.isTrue(fs.existsSync(filePath));

fs.unlinkSync(filePath);
fs.unlinkSync(pkgJSONPath);
});

it("should create .eslintrc.yml", () => {
answers.format = "YAML";

const config = init.processAnswers(answers);
const filePath = path.resolve(fixtureDir, ".eslintrc.yml");

fs.writeFileSync(pkgJSONPath, JSON.stringify(pkgJSONContents));

init.writeFile(config, answers.format);

assert.isTrue(fs.existsSync(filePath));

fs.unlinkSync(filePath);
fs.unlinkSync(pkgJSONPath);
});

// For https://github.com/eslint/eslint/issues/14137
it("should create .eslintrc.cjs", () => {
answers.format = "JavaScript";

// create package.json with "type": "module"
pkgJSONContents.type = "module";

fs.writeFileSync(pkgJSONPath, JSON.stringify(pkgJSONContents));

const config = init.processAnswers(answers);
const filePath = path.resolve(fixtureDir, ".eslintrc.cjs");

init.writeFile(config, answers.format);

assert.isTrue(fs.existsSync(filePath));

fs.unlinkSync(filePath);
fs.unlinkSync(pkgJSONPath);
});

it("should create .eslintrc.json even with type: 'module'", () => {
answers.format = "JSON";

// create package.json with "type": "module"
pkgJSONContents.type = "module";

fs.writeFileSync(pkgJSONPath, JSON.stringify(pkgJSONContents));

const config = init.processAnswers(answers);
const filePath = path.resolve(fixtureDir, ".eslintrc.json");

init.writeFile(config, answers.format);

assert.isTrue(fs.existsSync(filePath));

fs.unlinkSync(filePath);
fs.unlinkSync(pkgJSONPath);
});
});
});

0 comments on commit ee3a3ea

Please sign in to comment.