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: make plugin-kit types usable in CommonJS #143

Merged
merged 10 commits into from
Jan 8, 2025

Conversation

fasttime
Copy link
Member

@fasttime fasttime commented Dec 27, 2024

Prerequisites checklist

What is the purpose of this pull request?

This PR ensures that the plugin-kit types can be imported from a CommonJS module when moduleResolution is one of 'node16' or 'nodenext'. This is currently not possible because of the way TypeScript handles imports of ESM modules in CommonJS.

Repro

What changes did you make? (Give an overview)

Made sure that packages/plugin/dist/cjs/index.d.cts uses the "resolution-mode": "import" import attribute to import ./types.ts.

- export type StringConfig = import("./types.ts").StringConfig;
- export type BooleanConfig = import("./types.ts").BooleanConfig;
+ export type StringConfig = import("./types.ts", { with: { "resolution-mode": "import" } }).StringConfig;
+ export type BooleanConfig = import("./types.ts", { with: { "resolution-mode": "import" } }).BooleanConfig;

It's not possible to add the import attribute directly to the source code in packages/plugin-kit/src/config-comment-parser.js because then the same import attribute would appear also in the generated ESM types, and jsr does not like that (maybe because import attributes are not supported in TypeScript < 5.0).

Downloading JSR release binary...
[00:15] [#################################################>] 40.8 MiB/40.9 MiB
Checking for slow types in the public API...
error: Failed preparing '@eslint/plugin-kit'.

Caused by:
    0: Failed to create a tarball
    1: Expected '{', got 'StringConfig' at file:///.../rewrite/packages/plugin-kit/dist/esm/index.d.ts:12:13
       
         export type StringConfig = import("./types.ts", { with: { "resolution-mode": "im...
                     ~~~~~~~~~~~~
Child process exited with: 1
npm error Lifecycle script `test:jsr` failed with error:
npm error code 1
npm error path .../eslint/rewrite/packages/plugin-kit
npm error workspace @eslint/[email protected]
npm error location .../eslint/rewrite/packages/plugin-kit
npm error command failed
npm error command sh -c jsr publish --dry-run

This is not the only possible fix, see also: eslint/json#66 (comment).

Other changes included in this PR:

  • Made @eslint/core a runtime dependency of @eslint/plugin-kit.
  • Added tests to ensure that @eslint/core and @eslint/plugin-kit can be imported from a .cts file.
  • Added type tests to @eslint/plugin-kit.

Related Issues

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

@eslint-github-bot eslint-github-bot bot added the bug Something isn't working label Dec 27, 2024
@fasttime fasttime marked this pull request as ready for review December 31, 2024 13:41
packages/plugin-kit/build-cts.js Show resolved Hide resolved
const oldSourceText = await readFile("dist/esm/index.d.ts", "utf-8");
const newSourceText = oldSourceText.replaceAll(
'import("./types.ts")',
'import("./types.ts", { with: { "resolution-mode": "import" } })',
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 curious: Will this work with TypeScript 4? And if not, does it matter?

Copy link
Member Author

@fasttime fasttime Jan 1, 2025

Choose a reason for hiding this comment

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

It will not work because TypeScript 4 doesn't support import attributes. The alternative would be renaming types.ts to types.cts in dist/cjs and updating the imports in index.d.cts accordingly.

Update:

Copy link
Member

Choose a reason for hiding this comment

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

I think renaming to types.cts and updating the references seems like a safer option for best compatibility. What do you think?

Copy link
Member Author

Choose a reason for hiding this comment

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

I agree. I switched to importing from types.cts in 38bc8e9. We don't have policy about TypeScript versions support like we have for Node.js versions, but since the import attributes proposal is still not finalized, types.cts looks like a safer choice.

Copy link
Member

@nzakas nzakas left a comment

Choose a reason for hiding this comment

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

LGTM. Would like @mdjermanovic to review before merging.

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 f77ba17 into main Jan 8, 2025
16 checks passed
@mdjermanovic mdjermanovic deleted the fix-plugin-kit-cjs-types branch January 8, 2025 20:41
@github-actions github-actions bot mentioned this pull request Jan 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: Complete
Development

Successfully merging this pull request may close these issues.

3 participants