Skip to content
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

fix(editor): don't create a duplicate import for aliased files #6062

Merged
merged 2 commits into from
Jul 11, 2024
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
4 changes: 3 additions & 1 deletion editor/src/core/shared/import-shared-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { importAlias, importDetails } from './project-file-types'
import { absolutePathFromRelativePath } from '../../utils/path-utils'
import { stripExtension } from '../../components/custom-code/custom-code-utils'
import type { FilePathMappings } from '../model/project-file-utils'
import { applyFilePathMappingsToFilePath } from '../workers/common/project-file-utils'

export function renameDuplicateImports(
existingImports: Imports,
Expand All @@ -13,7 +14,8 @@ export function renameDuplicateImports(
): ImportsMergeResolution {
function absolutePath(relativePath: string): string {
const rawAbsolutePath = absolutePathFromRelativePath(targetFilePath, false, relativePath)
const absoluteImportSource = stripExtension(rawAbsolutePath)
const afterFilePathMapping = applyFilePathMappingsToFilePath(rawAbsolutePath, filePathMappings)
const absoluteImportSource = stripExtension(afterFilePathMapping)
return absoluteImportSource
}
const existingNames = getAllImportsUniqueNames(existingImports)
Expand Down
19 changes: 19 additions & 0 deletions editor/src/core/workers/common/project-file-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,25 @@ describe('mergeImports', () => {
})
})

it('does not duplicate same import from aliased sources', () => {
const result = mergeImports(
'/src/code.js',
[[/@h2\/([^/]*)/y, ['/app/components/hydrogen/$1']]],
{ '@h2/ProductCard': importDetails(null, [importAlias('Card')], null) },
{
'/app/components/hydrogen/ProductCard.jsx': importDetails(
null,
[importAlias('Card')],
null,
),
},
)
expect(result.imports).toEqual({
'@h2/ProductCard': importDetails(null, [importAlias('Card')], null),
})
expect(result.duplicateNameMapping).toEqual(new Map())
})

it('handles duplicate imports', () => {
const result = mergeImports(
'/src/code.js',
Expand Down
3 changes: 2 additions & 1 deletion editor/src/core/workers/common/project-file-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ export function mergeImports(

allImportSources.forEach((importSource) => {
const rawAbsolutePath = absolutePathFromRelativePath(fileUri, false, importSource)
const absoluteImportSource = stripExtension(rawAbsolutePath)
const unaliasedPath = applyFilePathMappingsToFilePath(rawAbsolutePath, filePathMappings)
const absoluteImportSource = stripExtension(unaliasedPath)
if (fileUriWithoutExtension === absoluteImportSource) {
// Prevent accidentally importing the current file
return
Expand Down
Loading