Skip to content

Conversation

nzakas
Copy link
Member

@nzakas nzakas commented Mar 26, 2025

Prerequisites checklist

What is the purpose of this pull request?

Better support migration .eslintrc.js files to eslint.config.js.

What changes did you make? (Give an overview)

  • Updated CLI so that it takes a different path for .js, .mjs, and .cjs files
  • Added functionality to parse and modify existing JS code found in files
  • Added tests to verify the functionality works

Related Issues

fixes #64

Is there anything you'd like reviewers to focus on?

I made some assumptions about JS config files in order to move forward, mostly assuming that certain keys (i.e., env) will be object literals rather than variable references or function calls. I also assumed that the file is exporting an object expression rather than a variable or a function call. A quick search of GitHub showed that the overwhelming majority of .eslintrc.js files export an object expression.

I'm outputting warnings when things are found that I don't know how to convert. This won't be perfect but it generally results in retaining more of the dynamic portions of a config file than the static approach.

@nzakas nzakas requested a review from Copilot March 26, 2025 19:14
@github-project-automation github-project-automation bot moved this to Needs Triage in Triage Mar 26, 2025
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR improves the migration of .eslintrc.js files to eslint.config.js by updating the CLI behavior to handle different JavaScript file types and enhancing the parsing of existing configuration code. Key changes include:

  • Adjusting the CLI to differentiate handling based on file extensions (js, cjs, mjs).
  • Updating tests and expected fixtures to validate these new behaviors.
  • Modifying how configuration data is loaded and processed in the migration.

Reviewed Changes

Copilot reviewed 17 out of 19 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
packages/migrate-config/tests/migrate-config-cli.test.js Updates to test fixtures and migration path handling for various JS file types
packages/migrate-config/tests/fixtures/prisma/expected.mjs Removal of the expected migrated code for this fixture
packages/migrate-config/tests/fixtures/prisma-js/expected.cjs Adjustments in expected output for JS configurations with added globals and linterOptions
packages/migrate-config/tests/fixtures/prisma-js/.eslintrc.cjs Minor configuration adjustments in the test fixture
packages/migrate-config/tests/fixtures/import-duplicate/expected.mjs Expected file now empty to reflect duplicate import handling changes
packages/migrate-config/tests/fixtures/formidable-js/expected.cjs New expected migrated configuration for formidable-js fixture
packages/migrate-config/tests/fixtures/formidable-js/.eslintrc.cjs New configuration fixture for formidable-js
packages/migrate-config/tests/fixtures/backend-nx-skeleton-js/expected.cjs New expected migrated configuration for backend nx skeleton (CommonJS)
packages/migrate-config/tests/fixtures/backend-nx-skeleton-js/.eslintrc.cjs New configuration fixture for backend nx skeleton (CommonJS)
packages/migrate-config/tests/fixtures/backend-nx-skeleton-esm-js/expected.mjs New expected migrated configuration for backend nx skeleton (ES module)
packages/migrate-config/tests/fixtures/backend-nx-skeleton-esm-js/.eslintrc.mjs New configuration fixture for backend nx skeleton (ES module)
packages/migrate-config/src/migrate-config-cli.js Updates the CLI to use the new migrateJSConfig for JavaScript file migration
Files not reviewed (2)
  • packages/migrate-config/package.json: Language not supported
  • packages/migrate-config/tests/fixtures/formidable-js/LICENSE: Language not supported

},

extends: compat.extends(
Copy link
Preview

Copilot AI Mar 26, 2025

Choose a reason for hiding this comment

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

The identifier 'compat' is used without being defined in this file. Consider importing or defining 'compat' (e.g., via 'const { FlatCompat } = require("@eslint/eslintrc");') to prevent reference errors.

Copilot uses AI. Check for mistakes.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good bug found here. Need to see why compat is undefined.

@nzakas nzakas force-pushed the migrate-js-config branch from 4aec2aa to fc597ef Compare March 27, 2025 20:05
@fasttime fasttime moved this from Needs Triage to Triaging in Triage Apr 7, 2025
@nzakas nzakas added the tsc waiting Issues that require feedback from a TSC member label Apr 22, 2025
@mdjermanovic mdjermanovic moved this from Triaging to Implementing in Triage Apr 24, 2025
Comment on lines +1557 to +1558
const isModule = !cjsExports;
const esmExport = isModule ? findDefaultExport(ast) : null;
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure if this is needed, since the eslintrc config system didn't support ESM config files.

Copy link
Member Author

Choose a reason for hiding this comment

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

It didn't natively, I'm just not sure how folks were processing their config files before passing to ESLint, so I figured it's safer to check.

Comment on lines 1590 to 1594
// add ignore patterns from .eslintignore
if (ignorePatterns) {
migration.imports.get("eslint/config").bindings.push("globalIgnores");
configArrayElements.push(createGlobalIgnores(ignorePatterns));
}
Copy link
Member

Choose a reason for hiding this comment

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

If there's no .eslintignore, but only ignorePatterns in the config, this binding would be missing.

// .eslintrc.js
module.exports = {
    ignorePatterns: ["foo"]
};
// created eslint.config.js
const {
    defineConfig, // missing `globalIgnores`
} = require("eslint/config");

module.exports = defineConfig([{}, globalIgnores(["**/foo"])]);

Copy link
Member

Choose a reason for hiding this comment

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

Also, patterns from .eslintignore should come after ignore patterns from the config.

Currently:

// .eslintrc.js
module.exports = {
    ignorePatterns: ["foo"]
};
# .eslintignore
bar
// created eslint.config.js
const {
    defineConfig,
    globalIgnores,
} = require("eslint/config");

module.exports = defineConfig([globalIgnores(["**/bar"]), {}, globalIgnores(["**/foo"])]);

globalIgnores(["**/bar"]) should be after globalIgnores(["**/foo"]) in the array, or merged into a single element globalIgnores(["**/foo", "**/bar"]).

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catches. 👍

Copy link
Member

@mdjermanovic mdjermanovic left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@mdjermanovic mdjermanovic merged commit 63cb367 into main Apr 29, 2025
18 checks passed
@mdjermanovic mdjermanovic deleted the migrate-js-config branch April 29, 2025 11:10
@github-project-automation github-project-automation bot moved this from Implementing to Complete in Triage Apr 29, 2025
@nzakas nzakas removed the tsc waiting Issues that require feedback from a TSC member label Apr 30, 2025
This was referenced Apr 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Complete
Development

Successfully merging this pull request may close these issues.

Change Request: Better support migrating JS config files
2 participants