Skip to content

[web-tree-sitter] Fix type definitions in exports#4229

Merged
ObserverOfTime merged 1 commit intotree-sitter:masterfrom
savetheclocktower:fix-web-tree-sitter-types
Jun 15, 2025
Merged

[web-tree-sitter] Fix type definitions in exports#4229
ObserverOfTime merged 1 commit intotree-sitter:masterfrom
savetheclocktower:fix-web-tree-sitter-types

Conversation

@savetheclocktower
Copy link
Contributor

@savetheclocktower savetheclocktower commented Feb 20, 2025

…and generate .d.cts files for CommonJS exports.

Fixes #4228, though someone might want to verify that with actual testing in TypeScript.

The current structure of "exports" in package.json is incorrect; this TypeScript blog post details how it ought to look, and reminds us that the "types" field must come before the "default" field.

Arethetypeswrong gives us a good grade here, if not an A+:

Screenshot 2025-02-20 at 12 37 20 AM

The “Resolution failed” there is because node10 is the oldest TypeScript resolution algorithm, one which predates the invention of the "exports" field in package.json. If we absolutely wanted the extra credit here, we could copy the .d.ts and .d.cts files into the debug folder, but I'm not sure it's even worth doing.

Once I changed the format of the "exports" field, arethetypeswrong chided me for having one .d.ts file — an ESM module's type definition, since we do "type": "module" in package.json — pulling double duty as also documenting the .cjs file. This is supposedly bad for the reasons explained here; and if someone writes decent prose in an authoritative voice, I tend to believe it.

To fix this, I'm having the build:dts task generate both .d.ts and .d.cts files at once. This felt more elegant than just copying one file to the other destination on a build, especially since the corresponding .map files reference specific file names.

To test, I just recapitulated the steps in the CI workflow locally, ran npm pack, and gave arethetypeswrong the resulting .tgz. I may or may not get around to testing in an actual dummy TypeScript project.

@sqs
Copy link

sqs commented Feb 22, 2025

Confirming I experienced the issue in #4228 and that this PR fixes it for me. In the meantime as workaround I just used pnpm patch.

@amaanq amaanq force-pushed the fix-web-tree-sitter-types branch from f9f8c11 to fc8ddd3 Compare April 19, 2025 04:11
…and generate `.d.cts` files for CommonJS exports.
@amaanq amaanq force-pushed the fix-web-tree-sitter-types branch from fc8ddd3 to 8476c94 Compare April 19, 2025 05:22
@ObserverOfTime ObserverOfTime merged commit 02fff92 into tree-sitter:master Jun 15, 2025
14 checks passed
@tree-sitter-ci-bot
Copy link

Backport failed for release-0.25, because it was unable to cherry-pick the commit(s).

Please cherry-pick the changes locally and resolve any conflicts.

git fetch origin release-0.25
git worktree add -d .worktree/backport-4229-to-release-0.25 origin/release-0.25
cd .worktree/backport-4229-to-release-0.25
git switch --create backport-4229-to-release-0.25
git cherry-pick -x 02fff92b91fbb60cab1ff7691163c6019b408e05

@ObserverOfTime
Copy link
Member

Looks like some previous related change didn't make it in 0.25 so this can't make it either.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

web-tree-sitter does not expose type information

4 participants