Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/itchy-poets-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-import-x": patch
---

fix: should only use `context.physicalFilename` as fallback instead of main source
2 changes: 1 addition & 1 deletion src/utils/import-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ function isInternalPath(
* @param name The name of the module to check
* @returns `true` if the name is an external looking name, otherwise `false`
*/
function isExternalLookingName(name: string) {
export function isExternalLookingName(name: string) {
return isModule(name) || isScoped(name)
}

Expand Down
9 changes: 6 additions & 3 deletions src/utils/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import type {

import { arraify } from './arraify.js'
import { makeContextCacheKey } from './export-map.js'
import { isExternalLookingName } from './import-type.js'
import {
LEGACY_NODE_RESOLVERS,
normalizeConfigResolvers,
Expand Down Expand Up @@ -304,9 +305,11 @@ function fullResolve(
} // backward compatibility

const sourceFiles =
context.physicalFilename === sourceFile
context.physicalFilename === sourceFile ||
// only try to fallback for external packages
!isExternalLookingName(modulePath)
? [sourceFile]
: [context.physicalFilename, sourceFile]
: [sourceFile, context.physicalFilename]

for (const sourceFile of sourceFiles) {
for (const {
Expand Down Expand Up @@ -355,7 +358,7 @@ function fullResolve(
}

// else, counts
fileExistsCache.set(cacheKey, resolved.path as string | null)
fileExistsCache.set(cacheKey, resolved.path)
return resolved
}
}
Expand Down
Empty file added test/fixtures/config-helper.js
Empty file.
16 changes: 14 additions & 2 deletions test/utils/resolve.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@ import { setTimeout } from 'node:timers/promises'
import { jest } from '@jest/globals'
import type { TSESLint } from '@typescript-eslint/utils'

import { testContext, testFilePath } from '../utils.js'
import { TEST_FILENAME, testContext, testFilePath } from '../utils.js'

import { importXResolverCompat } from 'eslint-plugin-import-x'
import { cjsRequire, importXResolverCompat } from 'eslint-plugin-import-x'
import type {
CjsRequire,
NewResolver,
NormalizedCacheSettings,
RuleContext,
} from 'eslint-plugin-import-x'
import {
CASE_SENSITIVE_FS,
fileExistsWithCaseSync,
relative,
resolve,
} from 'eslint-plugin-import-x/utils'

Expand Down Expand Up @@ -830,5 +832,15 @@ describe('resolve', () => {
}),
).toBe(testFilePath('./bar.tsx'))
})

it('sourceFile should take higher priority than context.physicalFilename', () => {
const sourceFile = cjsRequire.resolve('typescript-eslint')
expect(
relative('./config-helper', sourceFile, {}, {
physicalFilename: TEST_FILENAME,
settings: {},
} as RuleContext),
).toBe(path.resolve(sourceFile, '../config-helper.js'))
})
})
})
Loading