Skip to content

fix(lint): transform filename from linter context to the absolute one for prefer-import-alias rule - #2817

Open
NriotHrreion wants to merge 1 commit into
cloudflare:mainfrom
NriotHrreion:fix/import-alias-lint-file-path-issue
Open

fix(lint): transform filename from linter context to the absolute one for prefer-import-alias rule#2817
NriotHrreion wants to merge 1 commit into
cloudflare:mainfrom
NriotHrreion:fix/import-alias-lint-file-path-issue

Conversation

@NriotHrreion

Copy link
Copy Markdown

Overview

context.filename provided by the linter is not guaranteed to be absolute, but prefer-import-alias just directly uses the context.filename to resolve the import paths, which causes unintended lint error in editor.

For example, in packages/vinext/src/server/api-handler.ts

import "./server-globals.js";
// Use alias 'vinext/internal/server/server-globals' instead of relative path './server-globals.js'. oxc(vinext-local(prefer-import-alias))

What changed

I added a function getPhysicalFilename(context) to get absolute filename from the linter context. This function resolves context.physicalFilename (if there is not, fallback to context.filename) to absolute with cwd.

And this function is called during createOnce():

+ const filename = getPhysicalFilename(context);
+ if (!filename) return;
- const aliases = loadAliasesForFile(context.filename);
+ const aliases = loadAliasesForFile(filename);
if (aliases.length === 0) return;
- const importerDir = path.dirname(context.filename);
+ const importerDir = path.dirname(filename);

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant