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!: mixed-esm-and-cjs should only add the deviating type aside from base #300

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions lib/configs/recommended-env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"use strict"

const globals = require("globals")
const { getPackageJson } = require("../util/get-package-json")

const packageJson = getPackageJson()

const isModule =
packageJson != null &&
typeof packageJson === "object" &&
"type" in packageJson &&
packageJson.type === "module"

const moduleEnv = {
name: "node/env/module",
languageOptions: {
globals: {
...globals.nodeBuiltin,
},
},
}

const scriptEnv = {
name: "node/env/script",
languageOptions: {
globals: {
...globals.node,
},
},
}

const recommendedEnv = [
{
...(isModule ? { ignores: ["**/*.cjs"] } : { files: ["**/*.mjs"] }),
...moduleEnv,
},
{
...(isModule ? { files: ["**/*.cjs"] } : { ignores: ["**/*.mjs"] }),
...scriptEnv,
},
]

module.exports = {
isModule,
recommendedEnv,
}
16 changes: 16 additions & 0 deletions lib/configs/recommended-mixed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"use strict"

const { commonRules } = require("./_commons")
const { recommendedEnv } = require("./recommended-env")

/**
* https://eslint.org/docs/latest/use/configure/configuration-files-new
* @type {import('eslint').Linter.FlatConfig[]}
*/
module.exports = [
...recommendedEnv,
{
name: "node/recommended",
rules: commonRules,
},
]
9 changes: 1 addition & 8 deletions lib/configs/recommended.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
"use strict"

const { getPackageJson } = require("../util/get-package-json")
const { isModule } = require("./recommended-env")
const moduleConfig = require("./recommended-module")
const scriptConfig = require("./recommended-script")

const packageJson = getPackageJson()

const isModule =
packageJson != null &&
typeof packageJson === "object" &&
"type" in packageJson &&
packageJson.type === "module"
const recommendedConfig = isModule ? moduleConfig : scriptConfig

/**
Expand Down
19 changes: 10 additions & 9 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
"use strict"

const pkg = require("../package.json")
const { recommendedEnv } = require("./configs/recommended-env")
const mixedConfig = require("./configs/recommended-mixed")
const esmConfig = require("./configs/recommended-module")
const cjsConfig = require("./configs/recommended-script")
const recommendedConfig = require("./configs/recommended")

const recommendedMixed = structuredClone(mixedConfig)

/**
* @typedef {{
'recommended-module': import('eslint').ESLint.ConfigData;
'recommended-script': import('eslint').ESLint.ConfigData;
'recommended': import('eslint').ESLint.ConfigData;
'flat/recommended-env': import('eslint').Linter.FlatConfig[];
'flat/recommended-mixed': import('eslint').Linter.FlatConfig[];
'flat/recommended-module': import('eslint').Linter.FlatConfig;
'flat/recommended-script': import('eslint').Linter.FlatConfig;
'flat/recommended': import('eslint').Linter.FlatConfig;
Expand Down Expand Up @@ -71,23 +77,18 @@ const plugin = {
"recommended-module": { plugins: ["n"], ...esmConfig.eslintrc },
"recommended-script": { plugins: ["n"], ...cjsConfig.eslintrc },
recommended: { plugins: ["n"], ...recommendedConfig.eslintrc },
"flat/recommended-env": structuredClone(recommendedEnv),
"flat/recommended-mixed": recommendedMixed,
"flat/recommended-module": { ...esmConfig.flat },
"flat/recommended-script": { ...cjsConfig.flat },
"flat/recommended": { ...recommendedConfig.flat },
"flat/mixed-esm-and-cjs": [
{ files: ["**/*.js"], ...recommendedConfig.flat },
{ files: ["**/*.mjs"], ...esmConfig.flat },
{ files: ["**/*.cjs"], ...cjsConfig.flat },
],
"flat/mixed-esm-and-cjs": recommendedMixed,
},
}

plugin.configs["flat/recommended-module"].plugins = { n: plugin }
plugin.configs["flat/recommended-script"].plugins = { n: plugin }
plugin.configs["flat/recommended"].plugins = { n: plugin }

for (const config of plugin.configs["flat/mixed-esm-and-cjs"]) {
config.plugins = { n: plugin }
}
recommendedMixed[recommendedMixed.length - 1].plugins = { n: plugin }

module.exports = plugin
Loading