Skip to content

Commit eb2885b

Browse files
author
Josue Barros
committed
Pass undefined for jsxDEV self inside super arguments
The react-jsxdev transform always passed this as the __self argument. In a derived constructor, JSX in the arguments of super() is evaluated before this is available, so the emitted code threw the 'Must call super constructor' ReferenceError at runtime. Track when the visitor is inside a super call and pass void 0 for __self there. Adds a compiler test with a local jsx-dev-runtime; the full compiler suite passes.
1 parent ef0c06e commit eb2885b

5 files changed

Lines changed: 167 additions & 2 deletions

File tree

‎tsc/internal/transformers/jsxtransforms/jsx.go‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type JSXTransformer struct {
2525
filenameDeclaration *ast.Node
2626
utilizedImplicitRuntimeImports collections.OrderedMap[string, map[string]*ast.Node]
2727
inJsxChild bool
28+
inSuperCallArguments bool
2829

2930
currentSourceFile *ast.SourceFile
3031
}
@@ -130,6 +131,14 @@ func (tx *JSXTransformer) visit(node *ast.Node) *ast.Node {
130131
case ast.KindJsxExpression:
131132
tx.setInChild(false)
132133
return tx.visitJsxExpression(node.AsJsxExpression())
134+
case ast.KindCallExpression:
135+
if ast.IsSuperCall(node) {
136+
// `this` is not available in the arguments of a super call.
137+
tx.inSuperCallArguments = true
138+
result := tx.Visitor().VisitEachChild(node)
139+
tx.inSuperCallArguments = false
140+
return result
141+
}
133142
}
134143
tx.setInChild(false)
135144
return tx.Visitor().VisitEachChild(node) // by default, do nothing
@@ -587,8 +596,12 @@ func (tx *JSXTransformer) visitJsxOpeningLikeElementOrFragmentJSX(
587596
tx.Factory().NewPropertyAssignment(nil, tx.Factory().NewIdentifier("lineNumber"), nil, nil, tx.Factory().NewNumericLiteral(strconv.FormatInt(int64(line+1), 10), ast.TokenFlagsNone)),
588597
tx.Factory().NewPropertyAssignment(nil, tx.Factory().NewIdentifier("columnNumber"), nil, nil, tx.Factory().NewNumericLiteral(strconv.FormatInt(int64(col)+1, 10), ast.TokenFlagsNone)),
589598
}), false))
590-
// __self development flag
591-
args = append(args, tx.Factory().NewThisExpression())
599+
// __self development flag. `this` is not available before super() returns.
600+
if tx.inSuperCallArguments {
601+
args = append(args, tx.Factory().NewVoidZeroExpression())
602+
} else {
603+
args = append(args, tx.Factory().NewThisExpression())
604+
}
592605
}
593606
}
594607

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//// [tests/cases/compiler/jsxDevSelfInSuperArguments.tsx] ////
2+
3+
//// [index.d.ts]
4+
export namespace JSX {
5+
interface Element {}
6+
interface IntrinsicElements {
7+
h2: {};
8+
}
9+
}
10+
export function jsxDEV(type: any, props: any, key?: any, isStatic?: boolean, source?: any, self?: any): JSX.Element;
11+
12+
//// [index.tsx]
13+
class Base {
14+
constructor(public content: any) {}
15+
}
16+
class Card extends Base {
17+
constructor(title: string) {
18+
super(<h2>{title}</h2>);
19+
}
20+
}
21+
22+
23+
//// [index.js]
24+
import { jsxDEV as _jsxDEV } from "preact/jsx-dev-runtime";
25+
const _jsxFileName = "/index.tsx";
26+
class Base {
27+
content;
28+
constructor(content) {
29+
this.content = content;
30+
}
31+
}
32+
class Card extends Base {
33+
constructor(title) {
34+
super(_jsxDEV("h2", { children: title }, void 0, false, { fileName: _jsxFileName, lineNumber: 6, columnNumber: 15 }, void 0));
35+
}
36+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//// [tests/cases/compiler/jsxDevSelfInSuperArguments.tsx] ////
2+
3+
=== /node_modules/preact/jsx-dev-runtime/index.d.ts ===
4+
export namespace JSX {
5+
>JSX : Symbol(JSX, Decl(index.d.ts, 0, 0))
6+
7+
interface Element {}
8+
>Element : Symbol(Element, Decl(index.d.ts, 0, 22))
9+
10+
interface IntrinsicElements {
11+
>IntrinsicElements : Symbol(IntrinsicElements, Decl(index.d.ts, 1, 24))
12+
13+
h2: {};
14+
>h2 : Symbol(IntrinsicElements.h2, Decl(index.d.ts, 2, 33))
15+
}
16+
}
17+
export function jsxDEV(type: any, props: any, key?: any, isStatic?: boolean, source?: any, self?: any): JSX.Element;
18+
>jsxDEV : Symbol(jsxDEV, Decl(index.d.ts, 5, 1))
19+
>type : Symbol(type, Decl(index.d.ts, 6, 23))
20+
>props : Symbol(props, Decl(index.d.ts, 6, 33))
21+
>key : Symbol(key, Decl(index.d.ts, 6, 45))
22+
>isStatic : Symbol(isStatic, Decl(index.d.ts, 6, 56))
23+
>source : Symbol(source, Decl(index.d.ts, 6, 76))
24+
>self : Symbol(self, Decl(index.d.ts, 6, 90))
25+
>JSX : Symbol(JSX, Decl(index.d.ts, 0, 0))
26+
>Element : Symbol(JSX.Element, Decl(index.d.ts, 0, 22))
27+
28+
=== /index.tsx ===
29+
class Base {
30+
>Base : Symbol(Base, Decl(index.tsx, 0, 0))
31+
32+
constructor(public content: any) {}
33+
>content : Symbol(Base.content, Decl(index.tsx, 1, 16))
34+
}
35+
class Card extends Base {
36+
>Card : Symbol(Card, Decl(index.tsx, 2, 1))
37+
>Base : Symbol(Base, Decl(index.tsx, 0, 0))
38+
39+
constructor(title: string) {
40+
>title : Symbol(title, Decl(index.tsx, 4, 16))
41+
42+
super(<h2>{title}</h2>);
43+
>super : Symbol(Base, Decl(index.tsx, 0, 0))
44+
>h2 : Symbol(JSX.IntrinsicElements.h2, Decl(index.d.ts, 2, 33))
45+
>title : Symbol(title, Decl(index.tsx, 4, 16))
46+
>h2 : Symbol(JSX.IntrinsicElements.h2, Decl(index.d.ts, 2, 33))
47+
}
48+
}
49+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//// [tests/cases/compiler/jsxDevSelfInSuperArguments.tsx] ////
2+
3+
=== /node_modules/preact/jsx-dev-runtime/index.d.ts ===
4+
export namespace JSX {
5+
interface Element {}
6+
interface IntrinsicElements {
7+
h2: {};
8+
>h2 : {}
9+
}
10+
}
11+
export function jsxDEV(type: any, props: any, key?: any, isStatic?: boolean, source?: any, self?: any): JSX.Element;
12+
>jsxDEV : (type: any, props: any, key?: any, isStatic?: boolean, source?: any, self?: any) => JSX.Element
13+
>type : any
14+
>props : any
15+
>key : any
16+
>isStatic : boolean | undefined
17+
>source : any
18+
>self : any
19+
>JSX : any
20+
21+
=== /index.tsx ===
22+
class Base {
23+
>Base : Base
24+
25+
constructor(public content: any) {}
26+
>content : any
27+
}
28+
class Card extends Base {
29+
>Card : Card
30+
>Base : Base
31+
32+
constructor(title: string) {
33+
>title : string
34+
35+
super(<h2>{title}</h2>);
36+
>super(<h2>{title}</h2>) : void
37+
>super : typeof Base
38+
><h2>{title}</h2> : import("preact/jsx-dev-runtime").JSX.Element
39+
>h2 : any
40+
>title : string
41+
>h2 : any
42+
}
43+
}
44+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// @jsx: react-jsxdev
2+
// @jsxImportSource: preact
3+
// @target: esnext
4+
// @module: esnext
5+
6+
// @filename: /node_modules/preact/jsx-dev-runtime/index.d.ts
7+
export namespace JSX {
8+
interface Element {}
9+
interface IntrinsicElements {
10+
h2: {};
11+
}
12+
}
13+
export function jsxDEV(type: any, props: any, key?: any, isStatic?: boolean, source?: any, self?: any): JSX.Element;
14+
15+
// @filename: /index.tsx
16+
class Base {
17+
constructor(public content: any) {}
18+
}
19+
class Card extends Base {
20+
constructor(title: string) {
21+
super(<h2>{title}</h2>);
22+
}
23+
}

0 commit comments

Comments
 (0)