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

feat: drop support for jiti v1.21 #18996

Merged
merged 2 commits into from
Oct 12, 2024
Merged

feat: drop support for jiti v1.21 #18996

merged 2 commits into from
Oct 12, 2024

Conversation

fasttime
Copy link
Member

@fasttime fasttime commented Oct 7, 2024

Prerequisites checklist

What is the purpose of this pull request? (put an "X" next to an item)

[ ] Documentation update
[ ] Bug fix (template)
[ ] New rule (template)
[ ] Changes an existing rule (template)
[ ] Add autofix to a rule
[ ] Add a CLI option
[ ] Add something to the core
[X] Other, please explain:

  • Drop support for jiti versions prior to v2
  • Improve the error message in a special case
  • Add unit tests for when jiti is outdated or not found

What changes did you make? (Give an overview)

Support for jiti v2 was added in PR #18954, but jiti v1.21 is still supported. This can lead to a confusing error message when a project with jiti v1.21 tries to use a TypeScript ESLint config with top-level await:

Oops! Something went wrong! :(

ESLint: 9.12.0

.../project/eslint.config.ts:1
(function (exports, require, module, __filename, __dirname) { "use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.default = void 0;const { default: pkg } = await Promise.resolve().then(() => require('./package.json'));var _default = exports.default =
                                                                                                                                                                                           ^^^^^

SyntaxError: await is only valid in async functions and the top level bodies of modules
    at new Script (node:vm:117:7)
    at createScript (node:vm:269:10)
    at Object.runInThisContext (node:vm:317:10)
    at evalModule (.../project/node_modules/jiti/dist/jiti.js:1:247124)
    at jiti (.../project/node_modules/jiti/dist/jiti.js:1:245241)
    at .../project/node_modules/jiti/dist/jiti.js:1:248272
    at Generator.next (<anonymous>)
    at .../project/node_modules/jiti/dist/jiti.js:1:238153
    at new Promise (<anonymous>)
    at __awaiter (.../project/node_modules/jiti/dist/jiti.js:1:237714)
    at jiti.import (.../project/node_modules/jiti/dist/jiti.js:1:248217)
    at loadConfigFile (.../project/node_modules/eslint/lib/config/config-loader.js:191:41)
    at async calculateConfigArray (.../project/node_modules/eslint/lib/config/config-loader.js:235:28)
    at async #calculateConfigArray (.../project/node_modules/eslint/lib/config/config-loader.js:622:25)
    at async entryFilter (.../project/node_modules/eslint/lib/eslint/eslint-helpers.js:285:33)
    at async NodeHfs.<anonymous> (file://.../project/node_modules/@humanfs/core/src/hfs.js:560:24)
    at async NodeHfs.walk (file://.../project/node_modules/@humanfs/core/src/hfs.js:600:3)
    at async globSearch (.../project/node_modules/eslint/lib/eslint/eslint-helpers.js:327:26)
    at async Promise.allSettled (index 0)
    at async globMultiSearch (.../project/node_modules/eslint/lib/eslint/eslint-helpers.js:412:21)
    at async findFiles (.../project/node_modules/eslint/lib/eslint/eslint-helpers.js:591:27)
    at async ESLint.lintFiles (.../project/node_modules/eslint/lib/eslint/eslint.js:729:27)
    at async Object.execute (.../project/node_modules/eslint/lib/cli.js:497:23)
    at async main (.../project/node_modules/eslint/bin/eslint.js:153:22)

The correct message should state:

You are using an outdated version of the 'jiti' library. Please update to the latest version of 'jiti' to ensure compatibility and access to the latest features.

StackBlitz Repro

This is not a regression in #18954. We were getting the same error message previously with jiti v1 where TLA is not supported. Now that jiti v2 is available it seems no longer necessary to maintain compatibility with jiti v1. TypeScript config files are still an experimental feature, so this change will not be a breaking one.

I've also added unit tests to check the error behavior when an outdated jiti versions is installed, or when jiti is not installed.

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

@eslint-github-bot eslint-github-bot bot added the feature This change adds a new feature to ESLint label Oct 7, 2024
@github-actions github-actions bot added the core Relates to ESLint's core APIs and features label Oct 7, 2024
Copy link

netlify bot commented Oct 7, 2024

Deploy Preview for docs-eslint canceled.

Name Link
🔨 Latest commit e5d3a9b
🔍 Latest deploy log https://app.netlify.com/sites/docs-eslint/deploys/670988399ef8e000087dd566

Comment on lines +520 to +528
/**
* Used to import the jiti dependency. This method is exposed internally for testing purposes.
* @returns {Promise<Record<string, unknown>>} A promise that fulfills with a module object
* or rejects with an error if jiti is not found.
*/
static loadJiti() {
return import("jiti");
}

Copy link
Member Author

Choose a reason for hiding this comment

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

Any better way to make this function mockable?

@fasttime fasttime marked this pull request as ready for review October 7, 2024 20:19
@fasttime fasttime requested a review from a team as a code owner October 7, 2024 20:19
@mdjermanovic
Copy link
Member

Now that jiti v2 is available it seems no longer necessary to maintain compatibility with jiti v1. TypeScript config files are still an experimental feature, so this change will not be a breaking one.

👍 It sounds reasonable to me that we don't maintain compatibility with the previous major line, especially since this is still an experimental feature.

snitin315
snitin315 previously approved these changes Oct 11, 2024
Copy link
Contributor

@snitin315 snitin315 left a comment

Choose a reason for hiding this comment

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

LGTM. Awaiting 2nd review.

@snitin315 snitin315 added the accepted There is consensus among the team that this change meets the criteria for inclusion label Oct 11, 2024
Comment on lines 195 to 196
return config?.default ?? config;
return config.default ?? config;
Copy link
Member

Choose a reason for hiding this comment

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

Without the ?, a config like this would result in TypeError: Cannot read properties of undefined (reading 'default'):

// eslint.config.cts
module.exports = undefined;

Copy link
Contributor

Choose a reason for hiding this comment

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

is that a valid config/usecase?

Copy link
Member

Choose a reason for hiding this comment

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

Not really, but currently it's caught by validations rather than crashing so the error message is more descriptive:

Oops! Something went wrong! :(

ESLint: 9.12.0

TypeError: Config (unnamed): Unexpected undefined config at user-defined index 0.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks! I reverted the change and added unit tests for this case in e5d3a9b.

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 1def4cd into main Oct 12, 2024
26 checks passed
@mdjermanovic mdjermanovic deleted the drop-jiti-v1 branch October 12, 2024 06:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion core Relates to ESLint's core APIs and features feature This change adds a new feature to ESLint
Projects
Status: Complete
Development

Successfully merging this pull request may close these issues.

3 participants