Skip to content

Commit 7fcfd01

Browse files
Copilotjakebailey
andcommitted
Fix regression: only apply contextual type for fragment children, not regular JSX elements
Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com>
1 parent 483093f commit 7fcfd01

7 files changed

+36
-21
lines changed

src/compiler/checker.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33791,25 +33791,19 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3379133791
(isJsxElement(parent) && parent.openingElement === openingLikeElement || isJsxFragment(parent) && parent.openingFragment === openingLikeElement) &&
3379233792
getSemanticJsxChildren(parent.children).length > 0
3379333793
) {
33794-
// Compute contextual type for children before checking them
33794+
// Compute contextual type for fragment children before checking them
3379533795
let childrenContextualType: Type | undefined;
33796-
if (jsxChildrenPropertyName && jsxChildrenPropertyName !== "") {
33797-
let contextualType: Type | undefined;
33798-
if (isJsxOpeningElement(openingLikeElement)) {
33799-
contextualType = getApparentTypeOfContextualType(openingLikeElement.attributes, /*contextFlags*/ undefined);
33796+
if (isJsxOpeningFragment(openingLikeElement) && jsxChildrenPropertyName && jsxChildrenPropertyName !== "") {
33797+
// For fragments, get the props type from the Fragment factory's signature
33798+
const fragmentType = getJSXFragmentType(openingLikeElement);
33799+
const signatures = getSignaturesOfType(fragmentType, SignatureKind.Call);
33800+
if (signatures.length > 0) {
33801+
const contextualType = getTypeOfFirstParameterOfSignature(signatures[0]);
33802+
childrenContextualType = contextualType && getTypeOfPropertyOfContextualType(contextualType, jsxChildrenPropertyName);
3380033803
}
33801-
else if (isJsxOpeningFragment(openingLikeElement)) {
33802-
// For fragments, get the props type from the Fragment factory's signature
33803-
const fragmentType = getJSXFragmentType(openingLikeElement);
33804-
const signatures = getSignaturesOfType(fragmentType, SignatureKind.Call);
33805-
if (signatures.length > 0) {
33806-
contextualType = getTypeOfFirstParameterOfSignature(signatures[0]);
33807-
}
33808-
}
33809-
childrenContextualType = contextualType && getTypeOfPropertyOfContextualType(contextualType, jsxChildrenPropertyName);
3381033804
}
3381133805

33812-
// Check children with contextual type
33806+
// Check children with contextual type (only for fragments)
3381333807
const childrenTypes: Type[] = checkJsxChildren(parent, checkMode, childrenContextualType);
3381433808

3381533809
if (!hasSpreadAnyType && jsxChildrenPropertyName && jsxChildrenPropertyName !== "") {
@@ -33819,6 +33813,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3381933813
if (explicitlySpecifyChildrenAttribute) {
3382033814
error(attributeParent, Diagnostics._0_are_specified_twice_The_attribute_named_0_will_be_overwritten, unescapeLeadingUnderscores(jsxChildrenPropertyName));
3382133815
}
33816+
33817+
const contextualType = isJsxOpeningElement(openingLikeElement) ? getApparentTypeOfContextualType(openingLikeElement.attributes, /*contextFlags*/ undefined) : undefined;
33818+
const childrenContextualType = contextualType && getTypeOfPropertyOfContextualType(contextualType, jsxChildrenPropertyName);
3382233819
// If there are children in the body of JSX element, create dummy attribute "children" with the union of children types so that it will pass the attribute checking process
3382333820
const childrenPropSymbol = createSymbol(SymbolFlags.Property, jsxChildrenPropertyName);
3382433821
childrenPropSymbol.links.type = childrenTypes.length === 1 ? childrenTypes[0] :
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
jsxFragmentFactoryReference.tsx(3,9): error TS2875: This JSX tag requires the module path 'react/jsx-runtime' to exist, but none could be found. Make sure you have types for the appropriate package installed.
2+
jsxFragmentFactoryReference.tsx(3,9): error TS2879: Using JSX fragments requires fragment factory 'React' to be in scope, but it could not be found.
23

34

4-
==== jsxFragmentFactoryReference.tsx (1 errors) ====
5+
==== jsxFragmentFactoryReference.tsx (2 errors) ====
56
export class LoggedOut {
67
content = () => (
78
<></>
89
~~
910
!!! error TS2875: This JSX tag requires the module path 'react/jsx-runtime' to exist, but none could be found. Make sure you have types for the appropriate package installed.
11+
~~
12+
!!! error TS2879: Using JSX fragments requires fragment factory 'React' to be in scope, but it could not be found.
1013
)
1114
}
1215

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
jsxFragmentFactoryReference.tsx(3,9): error TS2875: This JSX tag requires the module path 'react/jsx-dev-runtime' to exist, but none could be found. Make sure you have types for the appropriate package installed.
2+
jsxFragmentFactoryReference.tsx(3,9): error TS2879: Using JSX fragments requires fragment factory 'React' to be in scope, but it could not be found.
23

34

4-
==== jsxFragmentFactoryReference.tsx (1 errors) ====
5+
==== jsxFragmentFactoryReference.tsx (2 errors) ====
56
export class LoggedOut {
67
content = () => (
78
<></>
89
~~
910
!!! error TS2875: This JSX tag requires the module path 'react/jsx-dev-runtime' to exist, but none could be found. Make sure you have types for the appropriate package installed.
11+
~~
12+
!!! error TS2879: Using JSX fragments requires fragment factory 'React' to be in scope, but it could not be found.
1013
)
1114
}
1215

tests/baselines/reference/jsxJsxsCjsTransformCustomImport(jsx=react-jsx).errors.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
jsxJsxsCjsTransformCustomImport.tsx(2,11): error TS2875: This JSX tag requires the module path 'preact/jsx-runtime' to exist, but none could be found. Make sure you have types for the appropriate package installed.
2+
jsxJsxsCjsTransformCustomImport.tsx(2,11): error TS2879: Using JSX fragments requires fragment factory 'React' to be in scope, but it could not be found.
23

34

4-
==== jsxJsxsCjsTransformCustomImport.tsx (1 errors) ====
5+
==== jsxJsxsCjsTransformCustomImport.tsx (2 errors) ====
56
/// <reference path="/.lib/react16.d.ts" />
67
const a = <>
78
~~
89
!!! error TS2875: This JSX tag requires the module path 'preact/jsx-runtime' to exist, but none could be found. Make sure you have types for the appropriate package installed.
10+
~~
11+
!!! error TS2879: Using JSX fragments requires fragment factory 'React' to be in scope, but it could not be found.
912
<p></p>
1013
text
1114
<div className="foo"></div>

tests/baselines/reference/jsxJsxsCjsTransformCustomImport(jsx=react-jsxdev).errors.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
jsxJsxsCjsTransformCustomImport.tsx(2,11): error TS2875: This JSX tag requires the module path 'preact/jsx-dev-runtime' to exist, but none could be found. Make sure you have types for the appropriate package installed.
2+
jsxJsxsCjsTransformCustomImport.tsx(2,11): error TS2879: Using JSX fragments requires fragment factory 'React' to be in scope, but it could not be found.
23

34

4-
==== jsxJsxsCjsTransformCustomImport.tsx (1 errors) ====
5+
==== jsxJsxsCjsTransformCustomImport.tsx (2 errors) ====
56
/// <reference path="/.lib/react16.d.ts" />
67
const a = <>
78
~~
89
!!! error TS2875: This JSX tag requires the module path 'preact/jsx-dev-runtime' to exist, but none could be found. Make sure you have types for the appropriate package installed.
10+
~~
11+
!!! error TS2879: Using JSX fragments requires fragment factory 'React' to be in scope, but it could not be found.
912
<p></p>
1013
text
1114
<div className="foo"></div>

tests/baselines/reference/jsxJsxsCjsTransformCustomImportPragma(jsx=react-jsx).errors.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
preact.tsx(3,11): error TS2875: This JSX tag requires the module path 'preact/jsx-runtime' to exist, but none could be found. Make sure you have types for the appropriate package installed.
2+
preact.tsx(3,11): error TS2879: Using JSX fragments requires fragment factory 'React' to be in scope, but it could not be found.
23

34

45
==== react.tsx (0 errors) ====
@@ -12,12 +13,14 @@ preact.tsx(3,11): error TS2875: This JSX tag requires the module path 'preact/js
1213
</>
1314

1415
export {};
15-
==== preact.tsx (1 errors) ====
16+
==== preact.tsx (2 errors) ====
1617
/// <reference path="/.lib/react16.d.ts" />
1718
/* @jsxImportSource preact */
1819
const a = <>
1920
~~
2021
!!! error TS2875: This JSX tag requires the module path 'preact/jsx-runtime' to exist, but none could be found. Make sure you have types for the appropriate package installed.
22+
~~
23+
!!! error TS2879: Using JSX fragments requires fragment factory 'React' to be in scope, but it could not be found.
2124
<p></p>
2225
text
2326
<div className="foo"></div>

tests/baselines/reference/jsxJsxsCjsTransformCustomImportPragma(jsx=react-jsxdev).errors.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
preact.tsx(3,11): error TS2875: This JSX tag requires the module path 'preact/jsx-dev-runtime' to exist, but none could be found. Make sure you have types for the appropriate package installed.
2+
preact.tsx(3,11): error TS2879: Using JSX fragments requires fragment factory 'React' to be in scope, but it could not be found.
23

34

45
==== react.tsx (0 errors) ====
@@ -12,12 +13,14 @@ preact.tsx(3,11): error TS2875: This JSX tag requires the module path 'preact/js
1213
</>
1314

1415
export {};
15-
==== preact.tsx (1 errors) ====
16+
==== preact.tsx (2 errors) ====
1617
/// <reference path="/.lib/react16.d.ts" />
1718
/* @jsxImportSource preact */
1819
const a = <>
1920
~~
2021
!!! error TS2875: This JSX tag requires the module path 'preact/jsx-dev-runtime' to exist, but none could be found. Make sure you have types for the appropriate package installed.
22+
~~
23+
!!! error TS2879: Using JSX fragments requires fragment factory 'React' to be in scope, but it could not be found.
2124
<p></p>
2225
text
2326
<div className="foo"></div>

0 commit comments

Comments
 (0)