Skip to content

Commit

Permalink
fix: add cjs override for esm projects (fixes #59) (#63)
Browse files Browse the repository at this point in the history
* fix: add `globals module` in generated .eslintrc.js (fixes #59)

see the discussion: #59 (comment)

* --wip-- [skip ci]

* fix: add cjs override for esm projects (fixes #59)

* Update config-initializer.js
  • Loading branch information
aladdin-add committed Jun 14, 2023
1 parent a5146fd commit 2568629
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
36 changes: 28 additions & 8 deletions lib/init/config-initializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,24 @@ const { ConfigOps, naming, ModuleResolver } = Legacy;
// Private
//------------------------------------------------------------------------------

/**
* check if the package.type === "module"
* @returns {boolean} return true if the package.type === "module"
*/
function isPackageTypeModule() {
const pkgJSONPath = npmUtils.findPackageJson();

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

if (pkgJSONContents.type === "module") {
return true;
}
}

return false;
}

/* istanbul ignore next: hard to test fs function */
/**
* Create .eslintrc file in the current working directory
Expand All @@ -41,14 +59,8 @@ async function writeFile(config, format) {
} 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";
}
if (isPackageTypeModule()) {
extname = ".cjs";
}
}

Expand Down Expand Up @@ -163,6 +175,14 @@ function processAnswers(answers) {
config.parserOptions.ecmaVersion = "latest";
config.env.es2021 = true;

if (answers.format === "JavaScript") {
config.overrides.push({
files: [".eslintrc.{js,cjs}"],
parserOptions: { sourceType: "script" },
env: { node: true }
});
}

// set the module type
if (answers.moduleType === "esm") {
config.parserOptions.sourceType = "module";
Expand Down
12 changes: 12 additions & 0 deletions tests/init/config-initializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,18 @@ describe("configInitializer", () => {

assert.isTrue(config.env.commonjs);
});

it("should set commonjs config for `.eslintrc.cjs` in esm projects", () => {
answers.moduleType = "esm";
answers.format = "JavaScript";
const config = init.processAnswers(answers);

assert.isArray(config.overrides, "should have overrides config");
assert.strictEqual(config.overrides.length, 1);
assert.deepStrictEqual(config.overrides[0].parserOptions, { sourceType: "script" });
assert.deepStrictEqual(config.overrides[0].env, { node: true });
});

});

describe("guide", () => {
Expand Down

4 comments on commit 2568629

@aladdin-add
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies for the inconvenience caused. The CI was not triggered because the WIP commit message was squashed as "* --wip-- [skip ci]". I should have amended the commit.

@mdjermanovic
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on: push GitHub Actions are not triggered when the commit description contains the word wip?

@aladdin-add
Copy link
Member Author

@aladdin-add aladdin-add commented on 2568629 Jun 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's [skip ci].

6c32b32

@mdjermanovic
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.