Skip to content

Commit ee3a3ea

Browse files
authored
Fix: create .eslintrc.cjs for module type (#14304)
* 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
1 parent 6791dec commit ee3a3ea

4 files changed

Lines changed: 133 additions & 0 deletions

File tree

lib/init/config-file.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ function writeJSConfigFile(config, filePath) {
117117
function write(config, filePath) {
118118
switch (path.extname(filePath)) {
119119
case ".js":
120+
case ".cjs":
120121
writeJSConfigFile(config, filePath);
121122
break;
122123

lib/init/config-initializer.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
const util = require("util"),
1414
path = require("path"),
15+
fs = require("fs"),
1516
enquirer = require("enquirer"),
1617
ProgressBar = require("progress"),
1718
semver = require("semver"),
@@ -48,6 +49,16 @@ function writeFile(config, format) {
4849
extname = ".yml";
4950
} else if (format === "JSON") {
5051
extname = ".json";
52+
} else if (format === "JavaScript") {
53+
const pkgJSONPath = npmUtils.findPackageJson();
54+
55+
if (pkgJSONPath) {
56+
const pkgJSONContents = JSON.parse(fs.readFileSync(pkgJSONPath, "utf8"));
57+
58+
if (pkgJSONContents.type === "module") {
59+
extname = ".cjs";
60+
}
61+
}
5162
}
5263

5364
const installedESLint = config.installedESLint;
@@ -684,6 +695,7 @@ const init = {
684695
hasESLintVersionConflict,
685696
installModules,
686697
processAnswers,
698+
writeFile,
687699
/* istanbul ignore next */initializeConfig() {
688700
return promptUser();
689701
}

lib/init/npm-utils.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ function checkPackageJson(startDir) {
172172
module.exports = {
173173
installSyncSaveDev,
174174
fetchPeerDependencies,
175+
findPackageJson,
175176
checkDeps,
176177
checkDevDeps,
177178
checkPackageJson

tests/lib/init/config-initializer.js

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ const proxyquire = require("proxyquire").noPreserveCache();
2727
//------------------------------------------------------------------------------
2828

2929
let answers = {};
30+
let pkgJSONContents = {};
31+
let pkgJSONPath = "";
3032

3133
describe("configInitializer", () => {
3234

@@ -455,4 +457,121 @@ describe("configInitializer", () => {
455457
});
456458
});
457459
});
460+
461+
describe("writeFile()", () => {
462+
463+
beforeEach(() => {
464+
answers = {
465+
purpose: "style",
466+
source: "prompt",
467+
extendDefault: true,
468+
indent: 2,
469+
quotes: "single",
470+
linebreak: "unix",
471+
semi: true,
472+
moduleType: "esm",
473+
es6Globals: true,
474+
env: ["browser"],
475+
format: "JSON"
476+
};
477+
478+
pkgJSONContents = {
479+
name: "config-initializer",
480+
version: "1.0.0"
481+
};
482+
483+
process.chdir(fixtureDir);
484+
485+
pkgJSONPath = path.resolve(fixtureDir, "package.json");
486+
});
487+
488+
afterEach(() => {
489+
process.chdir(originalDir);
490+
});
491+
492+
it("should create .eslintrc.json", () => {
493+
const config = init.processAnswers(answers);
494+
const filePath = path.resolve(fixtureDir, ".eslintrc.json");
495+
496+
fs.writeFileSync(pkgJSONPath, JSON.stringify(pkgJSONContents));
497+
498+
init.writeFile(config, answers.format);
499+
500+
assert.isTrue(fs.existsSync(filePath));
501+
502+
fs.unlinkSync(filePath);
503+
fs.unlinkSync(pkgJSONPath);
504+
});
505+
506+
it("should create .eslintrc.js", () => {
507+
answers.format = "JavaScript";
508+
509+
const config = init.processAnswers(answers);
510+
const filePath = path.resolve(fixtureDir, ".eslintrc.js");
511+
512+
fs.writeFileSync(pkgJSONPath, JSON.stringify(pkgJSONContents));
513+
514+
init.writeFile(config, answers.format);
515+
516+
assert.isTrue(fs.existsSync(filePath));
517+
518+
fs.unlinkSync(filePath);
519+
fs.unlinkSync(pkgJSONPath);
520+
});
521+
522+
it("should create .eslintrc.yml", () => {
523+
answers.format = "YAML";
524+
525+
const config = init.processAnswers(answers);
526+
const filePath = path.resolve(fixtureDir, ".eslintrc.yml");
527+
528+
fs.writeFileSync(pkgJSONPath, JSON.stringify(pkgJSONContents));
529+
530+
init.writeFile(config, answers.format);
531+
532+
assert.isTrue(fs.existsSync(filePath));
533+
534+
fs.unlinkSync(filePath);
535+
fs.unlinkSync(pkgJSONPath);
536+
});
537+
538+
// For https://github.com/eslint/eslint/issues/14137
539+
it("should create .eslintrc.cjs", () => {
540+
answers.format = "JavaScript";
541+
542+
// create package.json with "type": "module"
543+
pkgJSONContents.type = "module";
544+
545+
fs.writeFileSync(pkgJSONPath, JSON.stringify(pkgJSONContents));
546+
547+
const config = init.processAnswers(answers);
548+
const filePath = path.resolve(fixtureDir, ".eslintrc.cjs");
549+
550+
init.writeFile(config, answers.format);
551+
552+
assert.isTrue(fs.existsSync(filePath));
553+
554+
fs.unlinkSync(filePath);
555+
fs.unlinkSync(pkgJSONPath);
556+
});
557+
558+
it("should create .eslintrc.json even with type: 'module'", () => {
559+
answers.format = "JSON";
560+
561+
// create package.json with "type": "module"
562+
pkgJSONContents.type = "module";
563+
564+
fs.writeFileSync(pkgJSONPath, JSON.stringify(pkgJSONContents));
565+
566+
const config = init.processAnswers(answers);
567+
const filePath = path.resolve(fixtureDir, ".eslintrc.json");
568+
569+
init.writeFile(config, answers.format);
570+
571+
assert.isTrue(fs.existsSync(filePath));
572+
573+
fs.unlinkSync(filePath);
574+
fs.unlinkSync(pkgJSONPath);
575+
});
576+
});
458577
});

0 commit comments

Comments
 (0)