Skip to content

fix: add more extension aliases for ts source/declaration files #442

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

Merged
merged 1 commit into from
Apr 21, 2025

Conversation

JounQin
Copy link
Collaborator

@JounQin JounQin commented Apr 21, 2025

close #429

cc @silverwind


Important

Add more extension aliases for TypeScript files and update tests and logging accordingly.

  • Extension Aliases:
    • Added more extension aliases for .ts, .tsx, .cts, and .mts in src/constants.ts and README.md.
  • Tests:
    • Added test in unit.spec.ts to verify .d.ts resolution with .ts extension.
  • Logging:
    • Updated log message in unrsResolve() in src/index.ts to correct resolver name.

This description was created by Ellipsis for e371f47. You can customize this summary. It will automatically update as commits are pushed.

Summary by CodeRabbit

  • Bug Fixes

    • Improved TypeScript import resolution by expanding support for extension aliases, ensuring better handling of .ts, .tsx, .cts, and .mts files and their declaration or compiled variants.
  • Documentation

    • Updated documentation to clarify and list the new default extension alias mappings for TypeScript and related file types.
  • Tests

    • Added and refactored tests to verify correct resolution of .ts imports to .d.ts files and improved test structure for maintainability.

@JounQin JounQin requested a review from Copilot April 21, 2025 09:08
Copy link

changeset-bot bot commented Apr 21, 2025

🦋 Changeset detected

Latest commit: d6facb6

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
eslint-import-resolver-typescript Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

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 pull request adds more extension aliases for TypeScript source and declaration files, updating both implementation and documentation. Key changes include:

  • Adding additional alias mappings for .ts, .tsx, .cts, and .mts in the source code.
  • Updating the error log message in the resolver implementation.
  • Updating unit tests and README documentation to support the new aliases.

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

File Description
tests/unit/unit.spec.ts Refactored test structure; added a new test for resolving .d.ts files.
src/index.ts Updated the error log message for consistency.
src/constants.ts Extended the alias mapping for several file extensions.
README.md Updated extension alias documentation to align with implementation.

Copy link

codesandbox-ci bot commented Apr 21, 2025

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Copy link

pkg-pr-new bot commented Apr 21, 2025

Open in StackBlitz

npm i https://pkg.pr.new/eslint-import-resolver-typescript@442

commit: e371f47

Copy link
Contributor

github-actions bot commented Apr 21, 2025

size-limit report 📦

Path Size
lib/index.js 1.4 KB (-0.21% 🔽)

Copy link

codecov bot commented Apr 21, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 64.45%. Comparing base (151fa11) to head (e371f47).
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #442      +/-   ##
==========================================
+ Coverage   63.58%   64.45%   +0.86%     
==========================================
  Files           6        6              
  Lines         368      377       +9     
  Branches       48       48              
==========================================
+ Hits          234      243       +9     
  Misses        134      134              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link

@ellipsis-dev ellipsis-dev bot left a comment

Choose a reason for hiding this comment

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

Caution

Changes requested ❌

Reviewed everything up to 06c2d63 in 2 minutes and 8 seconds. Click for details.
  • Reviewed 116 lines of code in 5 files
  • Skipped 0 files when reviewing.
  • Skipped posting 9 draft comments. View those below.
  • Modify your settings and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.
1. README.md:282
  • Draft comment:
    Please verify that the '.tsx' alias intentionally omits a '.js' entry, despite the comment suggesting it can be compiled as '.js'.
  • Reason this comment was not posted:
    Comment did not seem useful. Confidence is useful = 0% <= threshold 50% The comment is asking the author to verify their intention regarding the omission of a .js entry for a .tsx alias. This falls under asking the author to confirm their intention, which is not allowed according to the rules.
2. src/constants.ts:41
  • Draft comment:
    Check if the '.tsx' alias should include '.js' as noted in the comment for consistency.
  • Reason this comment was not posted:
    Marked as duplicate.
3. src/index.ts:53
  • Draft comment:
    Renamed error log message to 'unrs-resolver error:'. Ensure that all documentation remains consistent with this updated name.
  • Reason this comment was not posted:
    Confidence changes required: 33% <= threshold 50% None
4. tests/unit/unit.spec.ts:41
5. README.md:287
  • Draft comment:
    The '.tsx' alias mapping omits '.js' despite the comment indicating it can compile to .js. Consider adding '.js' if that behavior is desired.
  • Reason this comment was not posted:
    Marked as duplicate.
6. src/index.ts:53
  • Draft comment:
    Updated log message to 'unrs-resolver error:' for clarity. This change looks fine.
  • Reason this comment was not posted:
    Confidence changes required: 0% <= threshold 50% None
7. tests/unit/unit.spec.ts:41
  • Draft comment:
    Good test coverage for the new extension aliases. The added test for resolving '.d.ts' via a '.ts' import clearly verifies issue Unable to resolve ./foo.ts to ./foo.d.ts since 3.9.0 #429.
  • Reason this comment was not posted:
    Confidence changes required: 0% <= threshold 50% None
8. README.md:51
  • Draft comment:
    Typographical error: In the sentence "After version 2.0.0, .d.ts will take higher priority then normal .js/.jsx files", the word "then" should be "than". Please update this for correctness.
  • Reason this comment was not posted:
    Comment was not on a location in the diff, so it can't be submitted as a review comment.
9. src/index.ts:146
  • Draft comment:
    Typographical error: there's a duplicate 'the' in the log message (", trying from the the nearest one"). Consider removing one instance of 'the' to improve clarity.
  • Reason this comment was not posted:
    Comment was not on a location in the diff, so it can't be submitted as a review comment.

Workflow ID: wflow_YYkD03P1cPmgGklS

You can customize Ellipsis by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.

@JounQin JounQin force-pushed the fix/extension_alias branch from d6facb6 to e371f47 Compare April 21, 2025 09:12
Copy link

@ellipsis-dev ellipsis-dev bot left a comment

Choose a reason for hiding this comment

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

Important

Looks good to me! 👍

Reviewed e371f47 in 34 seconds. Click for details.
  • Reviewed 126 lines of code in 5 files
  • Skipped 0 files when reviewing.
  • Skipped posting 13 draft comments. View those below.
  • Modify your settings and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.
1. .changeset/late-elephants-confess.md:2
  • Draft comment:
    Changeset looks fine.
  • Reason this comment was not posted:
    Confidence changes required: 0% <= threshold 50% None
2. README.md:285
  • Draft comment:
    Extension alias for '.ts' is added; ensure documentation matches implementation.
  • Reason this comment was not posted:
    Confidence changes required: 0% <= threshold 50% None
3. README.md:287
  • Draft comment:
    Extension alias for '.tsx' now includes '.js' as a fallback; aligns with source, good.
  • Reason this comment was not posted:
    Confidence changes required: 0% <= threshold 50% None
4. src/constants.ts:39
  • Draft comment:
    New extension alias entries for '.ts', '.tsx', '.cts', and '.mts' are added consistently.
  • Reason this comment was not posted:
    Confidence changes required: 0% <= threshold 50% None
5. src/index.ts:53
  • Draft comment:
    Updated log message now references 'unrs-resolver'; ensure consistency with external logs.
  • Reason this comment was not posted:
    Confidence changes required: 0% <= threshold 50% None
6. tests/unit/unit.spec.ts:41
  • Draft comment:
    New test added to ensure '.d.ts' correctly resolves for '.ts' extension; test covers issue 429.
  • Reason this comment was not posted:
    Confidence changes required: 0% <= threshold 50% None
7. .changeset/late-elephants-confess.md:1
  • Draft comment:
    Changeset file is clear and correctly documents the patch update.
  • Reason this comment was not posted:
    Confidence changes required: 0% <= threshold 50% None
8. README.md:282
  • Draft comment:
    Updated extensionAlias mapping now includes '.ts', '.tsx', '.cts' and '.mts'. This aligns with the reported issue fix.
  • Reason this comment was not posted:
    Confidence changes required: 0% <= threshold 50% None
9. src/constants.ts:31
  • Draft comment:
    Extension aliases updated to include the new TS-related keys; mapping is consistent with README and appears correct.
  • Reason this comment was not posted:
    Confidence changes required: 0% <= threshold 50% None
10. src/index.ts:53
  • Draft comment:
    Updated log message to 'unrs-resolver error:' to better reflect the resolver being used.
  • Reason this comment was not posted:
    Confidence changes required: 0% <= threshold 50% None
11. tests/unit/unit.spec.ts:41
  • Draft comment:
    New test for resolving '.d.ts' from a '.ts' source file correctly validates the bug fix.
  • Reason this comment was not posted:
    Confidence changes required: 0% <= threshold 50% None
12. README.md:51
  • Draft comment:
    Typographical error: In the sentence 'After version 2.0.0, .d.ts will take higher priority then normal .js/.jsx files...', 'then' should be replaced with 'than'.
  • Reason this comment was not posted:
    Comment was not on a location in the diff, so it can't be submitted as a review comment.
13. src/index.ts:146
  • Draft comment:
    Typo: The log message currently reads "trying from the the nearest one". Consider removing one occurrence of 'the' to correct it.
  • Reason this comment was not posted:
    Comment was not on a location in the diff, so it can't be submitted as a review comment.

Workflow ID: wflow_CjJxXqeneFik0P7I

You can customize Ellipsis by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.

@JounQin
Copy link
Collaborator Author

JounQin commented Apr 21, 2025

@coderabbitai review

Copy link

coderabbitai bot commented Apr 21, 2025

Walkthrough

This change updates the TypeScript import resolver to improve its handling of extension aliases, specifically for .ts and .d.ts files. The defaultExtensionAlias mapping is expanded to include more comprehensive aliases for .ts, .tsx, .cts, and .mts extensions, ensuring that imports can resolve to their corresponding declaration files when needed. The README is updated to reflect these changes in documentation. Additionally, a new test verifies that importing a .ts file correctly resolves to a .d.ts file. Minor adjustments are made to error logging for clarity.

Changes

File(s) Change Summary
.changeset/late-elephants-confess.md Added a changeset documenting the patch update and its rationale.
README.md Updated documentation to detail new extension alias mappings for .ts, .tsx, .cts, and .mts.
src/constants.ts Expanded defaultExtensionAlias to include new aliases for .ts, .tsx, .cts, and .mts extensions.
src/index.ts Updated error log message for unrsResolve failures for clarity.
tests/unit/unit.spec.ts Refactored resolver instantiation in tests and added a test to verify .ts resolves to .d.ts.

Sequence Diagram(s)

sequenceDiagram
    participant Importer
    participant Resolver
    participant FileSystem

    Importer->>Resolver: import './foo.ts'
    Resolver->>FileSystem: Check for './foo.ts'
    alt Not found
        Resolver->>FileSystem: Check for './foo.d.ts'
        alt Found
            Resolver-->>Importer: Resolve to './foo.d.ts'
        else Not found
            Resolver-->>Importer: Not found
        end
    else Found
        Resolver-->>Importer: Resolve to './foo.ts'
    end
Loading

Assessment against linked issues

Objective Addressed Explanation
Restore behavior to resolve ./foo.ts imports to ./foo.d.ts if the former does not exist (#429)

Poem

A patch for the resolver, neat and precise,
Now .ts finds .d.ts—isn't that nice?
With aliases mapped and the docs up to date,
Our imports resolve at a wonderful rate!
🐇✨

— A rabbit who loves when things just work!

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

yarn install v1.22.22
warning Resolution field "workspace:" has an invalid version entry and may be ignored
[1/4] Resolving packages...
info There appears to be trouble with your network connection. Retrying...
info There appears to be trouble with your network connection. Retrying...
info There appears to be trouble with the npm registry (returned undefined). Retrying...
info There appears to be trouble with the npm registry (returned undefined). Retrying...
info There appears to be trouble with your network connection. Retrying...
info There appears to be trouble with your network connection. Retrying...
info There appears to be trouble with your network connection. Retrying...
error Couldn't find any versions for "eslint-import-resolver-typescript" that matches "workspace:
"
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 151fa11 and e371f47.

📒 Files selected for processing (5)
  • .changeset/late-elephants-confess.md (1 hunks)
  • README.md (1 hunks)
  • src/constants.ts (1 hunks)
  • src/index.ts (1 hunks)
  • tests/unit/unit.spec.ts (2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
src/index.ts (1)
src/logger.ts (1)
  • log (5-5)
tests/unit/unit.spec.ts (1)
src/index.ts (1)
  • createTypeScriptImportResolver (205-217)
🔇 Additional comments (13)
src/constants.ts (4)

39-39: Good addition of .ts extension aliases.

The new mapping for .ts with aliases ['.ts', '.d.ts', '.js'] improves the resolver's ability to find TypeScript source and declaration files when resolving imports.


41-47: Good improvement to the .tsx aliases.

Properly including .js extension in the .tsx alias array aligns with the comment that .tsx can be compiled to .js. This fixes a gap in the previous implementation.


49-49: Good addition of .cts extension aliases.

Adding the .cts mapping with appropriate aliases will help resolve CommonJS TypeScript modules correctly.


51-51: Good addition of .mts extension aliases.

Adding the .mts mapping with appropriate aliases will help resolve ESM TypeScript modules correctly.

src/index.ts (1)

53-53: Improved error log message clarity.

Changing from 'oxc resolve error:' to 'unrs-resolver error:' makes the error message more accurate and aligned with the resolver module name, which improves debugging.

.changeset/late-elephants-confess.md (1)

1-5: Appropriate changeset for this patch.

The changeset correctly identifies this as a patch update and provides a clear description of the changes being made.

tests/unit/unit.spec.ts (3)

10-13: Good refactoring of test structure.

Moving the resolver creation to the top level of the describe block improves test organization and reduces duplication.


15-23: Improved structure for pnp test.

Moving the path resolution and yarn execution inside the test improves clarity and encapsulation.


41-52: Good test coverage for the new feature.

This test case directly verifies that imports with .ts extension correctly resolve to .d.ts files, which is exactly the functionality being improved in this PR. Great addition!

README.md (4)

285-285: Documented .ts extension aliases correctly reflect source, declaration, and JavaScript outputs

The new alias array for the .ts extension now includes .ts, .d.ts, and .js, aligning with the resolver’s intended behavior.


287-293: Expanded .tsx alias list includes JavaScript output

Great addition of .js to the .tsx alias list, resolving the previous omission and ensuring TSX sources correctly fall back to compiled .js files.


295-295: Added .cts alias mapping for CommonJS-based TypeScript

The .cts extension now correctly maps to [".cts", ".d.cts", ".cjs"], matching the new resolver behavior.


297-297: Added .mts alias mapping for ESM-based TypeScript

The .mts extension alias array [".mts", ".d.mts", ".mjs"] is now documented, ensuring proper resolution of modern ESM TypeScript files.

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

coderabbitai bot commented Apr 21, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@JounQin
Copy link
Collaborator Author

JounQin commented Apr 21, 2025

warning Resolution field "workspace:" has an invalid version entry and may be ignored

@coderabbitai Please you yarn v4 to install!

@JounQin JounQin merged commit 57611d9 into master Apr 21, 2025
32 checks passed
@JounQin JounQin deleted the fix/extension_alias branch April 21, 2025 09:29
Copy link

coderabbitai bot commented Apr 21, 2025

Tip

For best results, initiate chat on the files or code changes.

🧩 Analysis chain

@JounQin I understand that you're requesting the use of Yarn v4 for installation, as the current Yarn v1.22.22 doesn't properly support the workspace:* resolution field in your project. Let's check the available Yarn versions in our environment and update our approach.


🏁 Script executed:

#!/bin/bash
# Check available Yarn versions
echo "Currently installed Yarn version:"
yarn --version

# See if corepack is available, which can help us use Yarn v4
echo -e "\nChecking if corepack is available:"
if command -v corepack &> /dev/null; then
    echo "Corepack is available"
    corepack --version
else
    echo "Corepack is not available"
fi

# Check Node.js version as well
echo -e "\nNode.js version:"
node --version

Length of output: 361


@JounQin Thanks for your suggestion! I checked our environment and found that we're actually already using Yarn v4.9.1 with Node.js v22.9.0, and corepack 0.29.3 is available:

Currently installed Yarn version:
4.9.1

Checking if corepack is available:
Corepack is available
0.29.3

Node.js version:
v22.9.0

Despite having Yarn v4, it seems there might still be an issue with how the workspace protocol is being used in your project. The warning about "Resolution field 'workspace:*' has an invalid version entry" suggests there could be a configuration issue in your package.json.

Let me know if you'd like me to examine your package.json files to better understand the workspace configuration and provide more specific guidance.

renovate bot added a commit to andrei-picus-tink/auto-renovate that referenced this pull request Apr 23, 2025
| datasource | package                           | from  | to    |
| ---------- | --------------------------------- | ----- | ----- |
| npm        | eslint-import-resolver-typescript | 4.3.1 | 4.3.4 |


## [v4.3.4](https://github.com/import-js/eslint-import-resolver-typescript/blob/HEAD/CHANGELOG.md#434)

##### Patch Changes

-   [#442](import-js/eslint-import-resolver-typescript#442) [`57611d9`](import-js/eslint-import-resolver-typescript@57611d9) Thanks [@JounQin](https://github.com/JounQin)! - fix: add more extension aliases for ts source/declaration files

-   [#444](import-js/eslint-import-resolver-typescript#444) [`bd45fcd`](import-js/eslint-import-resolver-typescript@bd45fcd) Thanks [@JounQin](https://github.com/JounQin)! - fix(deps): bump `unrs-resolver` which resolves [#406](import-js/eslint-import-resolver-typescript#406), [#409](import-js/eslint-import-resolver-typescript#409), [#437](import-js/eslint-import-resolver-typescript#437)


## [v4.3.3](https://github.com/import-js/eslint-import-resolver-typescript/blob/HEAD/CHANGELOG.md#433)

##### Patch Changes

-   [#433](import-js/eslint-import-resolver-typescript#433) [`834b11e`](import-js/eslint-import-resolver-typescript@834b11e) Thanks [@JounQin](https://github.com/JounQin)! - chore: bump `unrs-resolver` to v1.6.0


## [v4.3.2](https://github.com/import-js/eslint-import-resolver-typescript/blob/HEAD/CHANGELOG.md#432)

##### Patch Changes

-   [#427](import-js/eslint-import-resolver-typescript#427) [`dabba8e`](import-js/eslint-import-resolver-typescript@dabba8e) Thanks [@JounQin](https://github.com/JounQin)! - chore: bump `unrs-resolver` to v1.4.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Unable to resolve ./foo.ts to ./foo.d.ts since 3.9.0
1 participant