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

Fix: create .eslintrc.cjs for module type #14304

Merged
merged 7 commits into from May 6, 2021
Merged
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
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);
snitin315 marked this conversation as resolved.
Show resolved Hide resolved
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);
});
});
});