Skip to content

Fix crashes when the global JSX namespace is an alias #41314

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

Merged
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
8 changes: 6 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25142,7 +25142,7 @@ namespace ts {
const resolvedNamespace = resolveName(location, namespaceName, SymbolFlags.Namespace, /*diagnosticMessage*/ undefined, namespaceName, /*isUse*/ false);
if (resolvedNamespace) {
const candidate = resolveSymbol(getSymbol(getExportsOfSymbol(resolveSymbol(resolvedNamespace)), JsxNames.JSX, SymbolFlags.Namespace));
if (candidate) {
if (candidate && candidate !== unknownSymbol) {
if (links) {
links.jsxNamespace = candidate;
}
Expand All @@ -25154,7 +25154,11 @@ namespace ts {
}
}
// JSX global fallback
return getGlobalSymbol(JsxNames.JSX, SymbolFlags.Namespace, /*diagnosticMessage*/ undefined)!; // TODO: GH#18217
const s = resolveSymbol(getGlobalSymbol(JsxNames.JSX, SymbolFlags.Namespace, /*diagnosticMessage*/ undefined));
if (s === unknownSymbol) {
return undefined!; // TODO: GH#18217
}
return s!; // TODO: GH#18217
}

/**
Expand Down
114 changes: 114 additions & 0 deletions tests/baselines/reference/jsxNamespaceGlobalReexport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
//// [tests/cases/compiler/jsxNamespaceGlobalReexport.tsx] ////

//// [index.d.ts]
type Defaultize<Props, Defaults> =
// Distribute over unions
Props extends any // Make any properties included in Default optional
? Partial<Pick<Props, Extract<keyof Props, keyof Defaults>>> &
// Include the remaining properties from Props
Pick<Props, Exclude<keyof Props, keyof Defaults>>
: never;
export namespace JSXInternal {
interface HTMLAttributes<T = {}> { }
interface SVGAttributes<T = {}> { }
type LibraryManagedAttributes<Component, Props> = Component extends {
defaultProps: infer Defaults;
}
? Defaultize<Props, Defaults>
: Props;

interface IntrinsicAttributes {
key?: any;
}

interface Element extends VNode<any> { }

interface ElementClass extends Component<any, any> { }

interface ElementAttributesProperty {
props: any;
}

interface ElementChildrenAttribute {
children: any;
}

interface IntrinsicElements {
div: HTMLAttributes;
}
}
export const Fragment: unique symbol;
export type ComponentType<T = {}> = {};
export type ComponentChild = {};
export type ComponentChildren = {};
export type VNode<T = {}> = {};
export type Attributes = {};
export type Component<T = {}, U = {}> = {};
//// [index.d.ts]
export { Fragment } from '..';
import {
ComponentType,
ComponentChild,
ComponentChildren,
VNode,
Attributes
} from '..';
import { JSXInternal } from '..';

export function jsx(
type: string,
props: JSXInternal.HTMLAttributes &
JSXInternal.SVGAttributes &
Record<string, any> & { children?: ComponentChild },
key?: string
): VNode<any>;
export function jsx<P>(
type: ComponentType<P>,
props: Attributes & P & { children?: ComponentChild },
key?: string
): VNode<any>;

export function jsxs(
type: string,
props: JSXInternal.HTMLAttributes &
JSXInternal.SVGAttributes &
Record<string, any> & { children?: ComponentChild[] },
key?: string
): VNode<any>;
export function jsxs<P>(
type: ComponentType<P>,
props: Attributes & P & { children?: ComponentChild[] },
key?: string
): VNode<any>;

export function jsxDEV(
type: string,
props: JSXInternal.HTMLAttributes &
JSXInternal.SVGAttributes &
Record<string, any> & { children?: ComponentChildren },
key?: string
): VNode<any>;
export function jsxDEV<P>(
type: ComponentType<P>,
props: Attributes & P & { children?: ComponentChildren },
key?: string
): VNode<any>;
// This shouldn't be preferred over
//export namespace jsxDEV {
// export import JSX = JSXInternal;
//}
// but it sort-of should work and it shouldn't crash.
declare global {
// @ts-ignore
export import JSX = JSXInternal;
}
//// [index.tsx]
export const Comp = () => <div></div>;

//// [index.js]
"use strict";
exports.__esModule = true;
exports.Comp = void 0;
var jsx_runtime_1 = require("preact/jsx-runtime");
var Comp = function () { return jsx_runtime_1.jsx("div", {}, void 0); };
exports.Comp = Comp;
Loading