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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

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/plugin-kit@0.2.4
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:

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: Needs Triage
Development

Successfully merging this pull request may close these issues.

2 participants