Skip to content
Permalink
Browse files
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
  • Loading branch information
snitin315 committed May 6, 2021
1 parent 6791dec commit ee3a3ead893d185cc4b1ae9041940cb0968767e1
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 0 deletions.
@@ -117,6 +117,7 @@ function writeJSConfigFile(config, filePath) {
function write(config, filePath) {
switch (path.extname(filePath)) {
case ".js":
case ".cjs":
writeJSConfigFile(config, filePath);
break;

@@ -12,6 +12,7 @@

const util = require("util"),
path = require("path"),
fs = require("fs"),
enquirer = require("enquirer"),
ProgressBar = require("progress"),
semver = require("semver"),
@@ -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;
@@ -684,6 +695,7 @@ const init = {
hasESLintVersionConflict,
installModules,
processAnswers,
writeFile,
/* istanbul ignore next */initializeConfig() {
return promptUser();
}
@@ -172,6 +172,7 @@ function checkPackageJson(startDir) {
module.exports = {
installSyncSaveDev,
fetchPeerDependencies,
findPackageJson,
checkDeps,
checkDevDeps,
checkPackageJson
@@ -27,6 +27,8 @@ const proxyquire = require("proxyquire").noPreserveCache();
//------------------------------------------------------------------------------

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

describe("configInitializer", () => {

@@ -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.