Skip to content

fix: correct code link for alias#2056

Open
eryue0220 wants to merge 12 commits intonpmx-dev:mainfrom
eryue0220:fix/issue-2044
Open

fix: correct code link for alias#2056
eryue0220 wants to merge 12 commits intonpmx-dev:mainfrom
eryue0220:fix/issue-2044

Conversation

@eryue0220
Copy link
Contributor

🔗 Linked issue

resolve #2044

🧭 Context

support alias path like ~/app/components/Button.vue, @/app/components/Button.vue or #/app/components/Button.vue

📚 Description

@vercel
Copy link

vercel bot commented Mar 13, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
docs.npmx.dev Ready Ready Preview, Comment Mar 16, 2026 10:07am
npmx.dev Ready Ready Preview, Comment Mar 16, 2026 10:07am
1 Skipped Deployment
Project Deployment Actions Updated (UTC)
npmx-lunaria Ignored Ignored Mar 16, 2026 10:07am

Request Review

@codecov
Copy link

codecov bot commented Mar 13, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ All tests successful. No failed tests found.

📢 Thoughts on this report? Let us know!

…ix/issue-2044

* 'fix/issue-2044' of github.com:eryue0220/npmx.dev:
  fix: align weekly chart buckets from end to match npm downloads (npmx-dev#2052)
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 13, 2026

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Adds internal alias import resolution across the codebase. Introduces new types and exports in server/utils/import-resolver.ts (InternalImportTarget, InternalImportsMap, resolveAliasToDir, resolveInternalImport, helpers) and extends createImportResolver to accept an optional internal imports map. Updates server/api/registry/file/[...pkg].get.ts to include imports?: InternalImportsMap on PackageJson and to pass it into the resolver. Modifies server/utils/code-highlight.ts to short‑circuit specifiers starting with #, ~ or @/ to the relative resolver. Adds unit tests for internal import resolution.

Suggested reviewers

  • danielroe
  • graphieros
🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description check ✅ Passed The PR description clearly references resolving issue #2044 and provides context about supporting alias paths like ~/app/components/Button.vue and @/app/components/Button.vue.
Linked Issues check ✅ Passed The changes implement alias path support (#2044) by adding internal import resolution, updating the PackageJson interface with imports property, extending code-highlight logic for alias prefixes, and providing comprehensive test coverage.
Out of Scope Changes check ✅ Passed All changes are directly scoped to resolving issue #2044: adding alias path support to the import resolver, updating the PackageJson interface, extending code-highlight logic, and testing the new functionality.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
📝 Coding Plan
  • Generate coding plan for human review comments

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

Comment @coderabbitai help to get the list of available commands and usage tips.

Tip

CodeRabbit can approve the review once all CodeRabbit's comments are resolved.

Enable the reviews.request_changes_workflow setting to automatically approve the review once all CodeRabbit's comments are resolved.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (1)
test/unit/server/utils/import-resolver.spec.ts (1)

194-299: Great test expansion; add one #/ canonicalisation regression case.

Please add a case where the specifier is #/app/... but the imports key is #app, to lock the slash-variant behaviour.


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 42e2b622-9ae9-4d7c-b79d-c8d2478854a5

📥 Commits

Reviewing files that changed from the base of the PR and between 8976969 and 5099d3b.

📒 Files selected for processing (4)
  • server/api/registry/file/[...pkg].get.ts
  • server/utils/code-highlight.ts
  • server/utils/import-resolver.ts
  • test/unit/server/utils/import-resolver.spec.ts

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
server/utils/import-resolver.ts (1)

264-269: Minor performance optimisation: move normalisation outside the loop.

normalizeAliasPrefix(specifier) is computed on every iteration but the result never changes. Consider hoisting it before the loop.

♻️ Suggested refactor
 function guessInternalImportTarget(
   imports: InternalImportsMap,
   specifier: string,
   files: FileSet,
   currentFile: string,
 ): string | null {
+  const normalizedSpecifier = normalizeAliasPrefix(specifier)
   for (const [key, value] of Object.entries(imports)) {
-    const normalizedSpecifier = normalizeAliasPrefix(specifier)
     const normalizedKey = normalizeAliasPrefix(key)

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: bda51135-cf70-4e3f-90aa-3fd46fb1d0a8

📥 Commits

Reviewing files that changed from the base of the PR and between cfdc81f and a6ad3f9.

📒 Files selected for processing (2)
  • server/api/registry/file/[...pkg].get.ts
  • server/utils/import-resolver.ts

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (1)
server/utils/import-resolver.ts (1)

258-316: Refactor guessInternalImportTarget into smaller helpers.

This method currently handles matching, suffix derivation, probing, and return shaping in one block, which makes it harder to maintain and test safely.

As per coding guidelines, "Keep functions focused and manageable (generally under 50 lines)".


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 2c7399fb-e4e1-44c9-8cf8-a21c19a00fba

📥 Commits

Reviewing files that changed from the base of the PR and between a6ad3f9 and 8c29a0e.

📒 Files selected for processing (1)
  • server/utils/import-resolver.ts

Comment on lines +117 to +133
// Support #app, #/app, ~app, ~/app, @app, @/app
const alias = aliasSpec.replace(/^[#~@]\/?/, '')
const segments = filePath.split('/')

let lastMatchIndex = -1
for (let i = 0; i < segments.length; i++) {
if (segments[i] === alias) {
lastMatchIndex = i
}
}

if (lastMatchIndex === -1) {
return null
}

return segments.slice(0, lastMatchIndex + 1).join('/')
}
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Handle empty alias roots (@, ~, #) explicitly.

After normalisation, aliases like @/~/ become empty, and this segment-based lookup can return null for valid targets such as ./src (no empty segment), so aliased links still fail in common setups.

Proposed fix
 export function resolveAliasToDir(aliasSpec: string, filePath?: string | null): string | null {
   if (
     (!aliasSpec.startsWith('#') && !aliasSpec.startsWith('~') && !aliasSpec.startsWith('@')) ||
     !filePath
   ) {
     return null
   }

   // Support `#app`, `#/app`, ~app, ~/app, `@app`, `@/app`
   const alias = aliasSpec.replace(/^[#~@]\/?/, '')
+  const normalizedFilePath = filePath.replace(/\/+$/, '')
+  if (!normalizedFilePath) {
+    return null
+  }
+  if (alias === '') {
+    return normalizedFilePath
+  }
-  const segments = filePath.split('/')
+  const segments = normalizedFilePath.split('/')

Comment on lines +341 to +353
const importTarget = normalizeInternalImportTarget(imports[cleanSpecifier])
const target =
importTarget != null
? importTarget
: guessInternalImportTarget(imports, cleanSpecifier, files, currentFile)

if (!target || !target.startsWith('./')) {
return null
}

const path = normalizePath(target)
if (!path || path.startsWith('..') || !files.has(path)) {
return null
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Add extension/index fallback for exact internal targets.

Exact imports[specifier] targets are currently accepted only on direct files.has(path). Extensionless or directory targets (for example ./dist/app) will incorrectly resolve to null even when resolvable files exist.

Proposed fix
   const path = normalizePath(target)
-  if (!path || path.startsWith('..') || !files.has(path)) {
+  if (!path || path.startsWith('..')) {
     return null
   }
 
-  return { path }
+  if (files.has(path)) {
+    return { path }
+  }
+
+  for (const extensions of getExtensionPriority(currentFile)) {
+    for (const ext of extensions) {
+      const candidate = `${path}${ext}`
+      if (files.has(candidate)) {
+        return { path: candidate }
+      }
+    }
+  }
+
+  for (const indexFile of getIndexExtensions(currentFile)) {
+    const candidate = `${path}/${indexFile}`
+    if (files.has(candidate)) {
+      return { path: candidate }
+    }
+  }
+
+  return null
 }

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

Labels

needs review This PR is waiting for a review from a maintainer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect links for aliased links in code view

2 participants