Skip to content

Commit 108f82f

Browse files
authored
Correctly handle qualified names in TS types import() (#9579)
1 parent 6636403 commit 108f82f

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

packages/core/integration-tests/test/ts-types.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,7 @@ describe('typescript types', function () {
466466
index.ts:
467467
export * from "./ErrorBoundary";
468468
export * from "./ErrorBoundaryContext";
469+
export * from "./Component";
469470
470471
foo.js:
471472
import {baz} from './baz';
@@ -495,6 +496,11 @@ describe('typescript types', function () {
495496
);
496497
}
497498
}
499+
500+
Component.tsx:
501+
export function Foo() {
502+
return <h1>Foo</h1>;
503+
}
498504
`;
499505

500506
let b = await bundle(path.join(dir, '/index.ts'), {
@@ -505,12 +511,13 @@ describe('typescript types', function () {
505511
let output = await outputFS.readFile(b.getBundles()[0].filePath, 'utf8');
506512
assert.equal(
507513
output,
508-
`import { Context, Component, PropsWithChildren, ProviderProps, FunctionComponentElement } from "react";
514+
`import { Context, Component, PropsWithChildren, ProviderProps, FunctionComponentElement, JSX } from "react";
509515
export type ErrorBoundaryContextType = {};
510516
export const ErrorBoundaryContext: Context<ErrorBoundaryContextType>;
511517
export class ErrorBoundary extends Component<PropsWithChildren> {
512518
render(): FunctionComponentElement<ProviderProps<ErrorBoundaryContextType>>;
513519
}
520+
export function Foo(): JSX.Element;
514521
515522
//# sourceMappingURL=types.d.ts.map
516523
`,

packages/transformers/typescript-types/src/collect.js

+24-11
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import type {TSModuleGraph} from './TSModuleGraph';
33

44
import nullthrows from 'nullthrows';
5-
import ts from 'typescript';
5+
import ts, {type EntityName} from 'typescript';
66
import {TSModule} from './TSModule';
77
import {getExportedName, isDeclaration} from './utils';
88

@@ -91,16 +91,9 @@ export function collect(
9191
ts.isStringLiteral(node.argument.literal)
9292
) {
9393
let local = `$$parcel$import$${moduleGraph.syntheticImportCount++}`;
94-
if (node.qualifier) {
95-
currentModule.addImport(
96-
local,
97-
node.argument.literal.text,
98-
node.qualifier.text,
99-
);
100-
} else {
101-
currentModule.addImport(local, node.argument.literal.text, '*');
102-
}
103-
return factory.createTypeReferenceNode(local, node.typeArguments);
94+
let [specifier, entity] = getImportName(node.qualifier, local, factory);
95+
currentModule.addImport(local, node.argument.literal.text, specifier);
96+
return factory.createTypeReferenceNode(entity, node.typeArguments);
10497
}
10598

10699
// Handle `export default name;`
@@ -142,3 +135,23 @@ export function collect(
142135

143136
return ts.visitNode(sourceFile, visit);
144137
}
138+
139+
// Traverse down an EntityName to the root identifier. Return that to use as the named import specifier,
140+
// and collect the remaining parts into a new QualifiedName with the local replacement at the root.
141+
// import('react').JSX.Element => import {JSX} from 'react'; JSX.Element
142+
function getImportName(
143+
qualifier: ?EntityName,
144+
local: string,
145+
factory: typeof ts,
146+
) {
147+
if (!qualifier) {
148+
return ['*', factory.createIdentifier(local)];
149+
}
150+
151+
if (qualifier.kind === ts.SyntaxKind.Identifier) {
152+
return [qualifier.text, factory.createIdentifier(local)];
153+
}
154+
155+
let [name, entity] = getImportName(qualifier.left, local, factory);
156+
return [name, factory.createQualifiedName(entity, qualifier.right)];
157+
}

0 commit comments

Comments
 (0)