Skip to content

Commit f0f3862

Browse files
authored
Merge pull request #41314 from weswigham/fix-global-jsx-ns-alias-crashes
Fix crashes when the global JSX namespace is an alias
2 parents 3de6ed0 + d722392 commit f0f3862

10 files changed

+1580
-2
lines changed

src/compiler/checker.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25142,7 +25142,7 @@ namespace ts {
2514225142
const resolvedNamespace = resolveName(location, namespaceName, SymbolFlags.Namespace, /*diagnosticMessage*/ undefined, namespaceName, /*isUse*/ false);
2514325143
if (resolvedNamespace) {
2514425144
const candidate = resolveSymbol(getSymbol(getExportsOfSymbol(resolveSymbol(resolvedNamespace)), JsxNames.JSX, SymbolFlags.Namespace));
25145-
if (candidate) {
25145+
if (candidate && candidate !== unknownSymbol) {
2514625146
if (links) {
2514725147
links.jsxNamespace = candidate;
2514825148
}
@@ -25154,7 +25154,11 @@ namespace ts {
2515425154
}
2515525155
}
2515625156
// JSX global fallback
25157-
return getGlobalSymbol(JsxNames.JSX, SymbolFlags.Namespace, /*diagnosticMessage*/ undefined)!; // TODO: GH#18217
25157+
const s = resolveSymbol(getGlobalSymbol(JsxNames.JSX, SymbolFlags.Namespace, /*diagnosticMessage*/ undefined));
25158+
if (s === unknownSymbol) {
25159+
return undefined!; // TODO: GH#18217
25160+
}
25161+
return s!; // TODO: GH#18217
2515825162
}
2515925163

2516025164
/**
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
//// [tests/cases/compiler/jsxNamespaceGlobalReexport.tsx] ////
2+
3+
//// [index.d.ts]
4+
type Defaultize<Props, Defaults> =
5+
// Distribute over unions
6+
Props extends any // Make any properties included in Default optional
7+
? Partial<Pick<Props, Extract<keyof Props, keyof Defaults>>> &
8+
// Include the remaining properties from Props
9+
Pick<Props, Exclude<keyof Props, keyof Defaults>>
10+
: never;
11+
export namespace JSXInternal {
12+
interface HTMLAttributes<T = {}> { }
13+
interface SVGAttributes<T = {}> { }
14+
type LibraryManagedAttributes<Component, Props> = Component extends {
15+
defaultProps: infer Defaults;
16+
}
17+
? Defaultize<Props, Defaults>
18+
: Props;
19+
20+
interface IntrinsicAttributes {
21+
key?: any;
22+
}
23+
24+
interface Element extends VNode<any> { }
25+
26+
interface ElementClass extends Component<any, any> { }
27+
28+
interface ElementAttributesProperty {
29+
props: any;
30+
}
31+
32+
interface ElementChildrenAttribute {
33+
children: any;
34+
}
35+
36+
interface IntrinsicElements {
37+
div: HTMLAttributes;
38+
}
39+
}
40+
export const Fragment: unique symbol;
41+
export type ComponentType<T = {}> = {};
42+
export type ComponentChild = {};
43+
export type ComponentChildren = {};
44+
export type VNode<T = {}> = {};
45+
export type Attributes = {};
46+
export type Component<T = {}, U = {}> = {};
47+
//// [index.d.ts]
48+
export { Fragment } from '..';
49+
import {
50+
ComponentType,
51+
ComponentChild,
52+
ComponentChildren,
53+
VNode,
54+
Attributes
55+
} from '..';
56+
import { JSXInternal } from '..';
57+
58+
export function jsx(
59+
type: string,
60+
props: JSXInternal.HTMLAttributes &
61+
JSXInternal.SVGAttributes &
62+
Record<string, any> & { children?: ComponentChild },
63+
key?: string
64+
): VNode<any>;
65+
export function jsx<P>(
66+
type: ComponentType<P>,
67+
props: Attributes & P & { children?: ComponentChild },
68+
key?: string
69+
): VNode<any>;
70+
71+
export function jsxs(
72+
type: string,
73+
props: JSXInternal.HTMLAttributes &
74+
JSXInternal.SVGAttributes &
75+
Record<string, any> & { children?: ComponentChild[] },
76+
key?: string
77+
): VNode<any>;
78+
export function jsxs<P>(
79+
type: ComponentType<P>,
80+
props: Attributes & P & { children?: ComponentChild[] },
81+
key?: string
82+
): VNode<any>;
83+
84+
export function jsxDEV(
85+
type: string,
86+
props: JSXInternal.HTMLAttributes &
87+
JSXInternal.SVGAttributes &
88+
Record<string, any> & { children?: ComponentChildren },
89+
key?: string
90+
): VNode<any>;
91+
export function jsxDEV<P>(
92+
type: ComponentType<P>,
93+
props: Attributes & P & { children?: ComponentChildren },
94+
key?: string
95+
): VNode<any>;
96+
// This shouldn't be preferred over
97+
//export namespace jsxDEV {
98+
// export import JSX = JSXInternal;
99+
//}
100+
// but it sort-of should work and it shouldn't crash.
101+
declare global {
102+
// @ts-ignore
103+
export import JSX = JSXInternal;
104+
}
105+
//// [index.tsx]
106+
export const Comp = () => <div></div>;
107+
108+
//// [index.js]
109+
"use strict";
110+
exports.__esModule = true;
111+
exports.Comp = void 0;
112+
var jsx_runtime_1 = require("preact/jsx-runtime");
113+
var Comp = function () { return jsx_runtime_1.jsx("div", {}, void 0); };
114+
exports.Comp = Comp;

0 commit comments

Comments
 (0)