Skip to content
Closed
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
36 changes: 34 additions & 2 deletions tsc/internal/transformers/jsxtransforms/jsx.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type JSXTransformer struct {
filenameDeclaration *ast.Node
utilizedImplicitRuntimeImports collections.OrderedMap[string, map[string]*ast.Node]
inJsxChild bool
inSuperCallArguments bool

currentSourceFile *ast.SourceFile
}
Expand Down Expand Up @@ -130,6 +131,33 @@ func (tx *JSXTransformer) visit(node *ast.Node) *ast.Node {
case ast.KindJsxExpression:
tx.setInChild(false)
return tx.visitJsxExpression(node.AsJsxExpression())
case ast.KindCallExpression:
if ast.IsSuperCall(node) {
// `this` is not available in the arguments of a super call.
previous := tx.inSuperCallArguments
tx.inSuperCallArguments = true
result := tx.Visitor().VisitEachChild(node)
tx.inSuperCallArguments = previous
return result
}
case ast.KindFunctionDeclaration,
ast.KindFunctionExpression,
ast.KindMethodDeclaration,
ast.KindGetAccessor,
ast.KindSetAccessor,
ast.KindConstructor,
ast.KindPropertyDeclaration,
ast.KindClassStaticBlockDeclaration,
ast.KindClassDeclaration,
ast.KindClassExpression:
// These introduce their own `this` scope. Arrow functions inherit the
// enclosing one, so they keep the flag.
previous := tx.inSuperCallArguments
tx.inSuperCallArguments = false
tx.setInChild(false)
result := tx.Visitor().VisitEachChild(node)
tx.inSuperCallArguments = previous
return result
}
tx.setInChild(false)
return tx.Visitor().VisitEachChild(node) // by default, do nothing
Expand Down Expand Up @@ -587,8 +615,12 @@ func (tx *JSXTransformer) visitJsxOpeningLikeElementOrFragmentJSX(
tx.Factory().NewPropertyAssignment(nil, tx.Factory().NewIdentifier("lineNumber"), nil, nil, tx.Factory().NewNumericLiteral(strconv.FormatInt(int64(line+1), 10), ast.TokenFlagsNone)),
tx.Factory().NewPropertyAssignment(nil, tx.Factory().NewIdentifier("columnNumber"), nil, nil, tx.Factory().NewNumericLiteral(strconv.FormatInt(int64(col)+1, 10), ast.TokenFlagsNone)),
}), false))
// __self development flag
args = append(args, tx.Factory().NewThisExpression())
// __self development flag. `this` is not available before super() returns.
if tx.inSuperCallArguments {
args = append(args, tx.Factory().NewVoidZeroExpression())
Comment thread
joshbarros marked this conversation as resolved.
Resolved
} else {
args = append(args, tx.Factory().NewThisExpression())
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//// [tests/cases/compiler/jsxDevSelfInSuperArguments.tsx] ////

//// [index.d.ts]
export namespace JSX {
interface Element {}
interface IntrinsicElements {
h2: {};
}
}
export function jsxDEV(type: any, props: any, key?: any, isStatic?: boolean, source?: any, self?: any): JSX.Element;

//// [index.tsx]
class Base {
content: any;
constructor(...args: any[]) { this.content = args[0]; }
}
class Card extends Base {
constructor(title: string) {
super(<h2>{title}</h2>);
}
}
class Nested extends Base {
constructor() {
super({ render() { return <h2 />; } });
}
}
class Outer extends Base {
constructor() {
super(class extends Base { constructor() { super(<h2 />); } }, <h2 />);
}
}


//// [index.js]
import { jsxDEV as _jsxDEV } from "preact/jsx-dev-runtime";
const _jsxFileName = "/index.tsx";
class Base {
content;
constructor(...args) { this.content = args[0]; }
}
class Card extends Base {
constructor(title) {
super(_jsxDEV("h2", { children: title }, void 0, false, { fileName: _jsxFileName, lineNumber: 7, columnNumber: 15 }, void 0));
}
}
class Nested extends Base {
constructor() {
super({ render() { return _jsxDEV("h2", {}, void 0, false, { fileName: _jsxFileName, lineNumber: 12, columnNumber: 35 }, this); } });
}
}
class Outer extends Base {
constructor() {
super(class extends Base {
constructor() { super(_jsxDEV("h2", {}, void 0, false, { fileName: _jsxFileName, lineNumber: 17, columnNumber: 58 }, void 0)); }
}, _jsxDEV("h2", {}, void 0, false, { fileName: _jsxFileName, lineNumber: 17, columnNumber: 72 }, void 0));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//// [tests/cases/compiler/jsxDevSelfInSuperArguments.tsx] ////

=== /node_modules/preact/jsx-dev-runtime/index.d.ts ===
export namespace JSX {
>JSX : Symbol(JSX, Decl(index.d.ts, 0, 0))

interface Element {}
>Element : Symbol(Element, Decl(index.d.ts, 0, 22))

interface IntrinsicElements {
>IntrinsicElements : Symbol(IntrinsicElements, Decl(index.d.ts, 1, 24))

h2: {};
>h2 : Symbol(IntrinsicElements.h2, Decl(index.d.ts, 2, 33))
}
}
export function jsxDEV(type: any, props: any, key?: any, isStatic?: boolean, source?: any, self?: any): JSX.Element;
>jsxDEV : Symbol(jsxDEV, Decl(index.d.ts, 5, 1))
>type : Symbol(type, Decl(index.d.ts, 6, 23))
>props : Symbol(props, Decl(index.d.ts, 6, 33))
>key : Symbol(key, Decl(index.d.ts, 6, 45))
>isStatic : Symbol(isStatic, Decl(index.d.ts, 6, 56))
>source : Symbol(source, Decl(index.d.ts, 6, 76))
>self : Symbol(self, Decl(index.d.ts, 6, 90))
>JSX : Symbol(JSX, Decl(index.d.ts, 0, 0))
>Element : Symbol(JSX.Element, Decl(index.d.ts, 0, 22))

=== /index.tsx ===
class Base {
>Base : Symbol(Base, Decl(index.tsx, 0, 0))

content: any;
>content : Symbol(Base.content, Decl(index.tsx, 0, 12))

constructor(...args: any[]) { this.content = args[0]; }
>args : Symbol(args, Decl(index.tsx, 2, 16))
>this.content : Symbol(Base.content, Decl(index.tsx, 0, 12))
>this : Symbol(Base, Decl(index.tsx, 0, 0))
>content : Symbol(Base.content, Decl(index.tsx, 0, 12))
>args : Symbol(args, Decl(index.tsx, 2, 16))
}
class Card extends Base {
>Card : Symbol(Card, Decl(index.tsx, 3, 1))
>Base : Symbol(Base, Decl(index.tsx, 0, 0))

constructor(title: string) {
>title : Symbol(title, Decl(index.tsx, 5, 16))

super(<h2>{title}</h2>);
>super : Symbol(Base, Decl(index.tsx, 0, 0))
>h2 : Symbol(JSX.IntrinsicElements.h2, Decl(index.d.ts, 2, 33))
>title : Symbol(title, Decl(index.tsx, 5, 16))
>h2 : Symbol(JSX.IntrinsicElements.h2, Decl(index.d.ts, 2, 33))
}
}
class Nested extends Base {
>Nested : Symbol(Nested, Decl(index.tsx, 8, 1))
>Base : Symbol(Base, Decl(index.tsx, 0, 0))

constructor() {
super({ render() { return <h2 />; } });
>super : Symbol(Base, Decl(index.tsx, 0, 0))
>render : Symbol(render, Decl(index.tsx, 11, 15))
>h2 : Symbol(JSX.IntrinsicElements.h2, Decl(index.d.ts, 2, 33))
}
}
class Outer extends Base {
>Outer : Symbol(Outer, Decl(index.tsx, 13, 1))
>Base : Symbol(Base, Decl(index.tsx, 0, 0))

constructor() {
super(class extends Base { constructor() { super(<h2 />); } }, <h2 />);
>super : Symbol(Base, Decl(index.tsx, 0, 0))
>Base : Symbol(Base, Decl(index.tsx, 0, 0))
>super : Symbol(Base, Decl(index.tsx, 0, 0))
>h2 : Symbol(JSX.IntrinsicElements.h2, Decl(index.d.ts, 2, 33))
>h2 : Symbol(JSX.IntrinsicElements.h2, Decl(index.d.ts, 2, 33))
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//// [tests/cases/compiler/jsxDevSelfInSuperArguments.tsx] ////

=== /node_modules/preact/jsx-dev-runtime/index.d.ts ===
export namespace JSX {
interface Element {}
interface IntrinsicElements {
h2: {};
>h2 : {}
}
}
export function jsxDEV(type: any, props: any, key?: any, isStatic?: boolean, source?: any, self?: any): JSX.Element;
>jsxDEV : (type: any, props: any, key?: any, isStatic?: boolean, source?: any, self?: any) => JSX.Element
>type : any
>props : any
>key : any
>isStatic : boolean | undefined
>source : any
>self : any
>JSX : any

=== /index.tsx ===
class Base {
>Base : Base

content: any;
>content : any

constructor(...args: any[]) { this.content = args[0]; }
>args : any[]
>this.content = args[0] : any
>this.content : any
>this : this
>content : any
>args[0] : any
>args : any[]
>0 : 0
}
class Card extends Base {
>Card : Card
>Base : Base

constructor(title: string) {
>title : string

super(<h2>{title}</h2>);
>super(<h2>{title}</h2>) : void
>super : typeof Base
><h2>{title}</h2> : import("preact/jsx-dev-runtime").JSX.Element
>h2 : any
>title : string
>h2 : any
}
}
class Nested extends Base {
>Nested : Nested
>Base : Base

constructor() {
super({ render() { return <h2 />; } });
>super({ render() { return <h2 />; } }) : void
>super : typeof Base
>{ render() { return <h2 />; } } : { render(): import("preact/jsx-dev-runtime").JSX.Element; }
>render : () => import("preact/jsx-dev-runtime").JSX.Element
><h2 /> : import("preact/jsx-dev-runtime").JSX.Element
>h2 : any
}
}
class Outer extends Base {
>Outer : Outer
>Base : Base

constructor() {
super(class extends Base { constructor() { super(<h2 />); } }, <h2 />);
>super(class extends Base { constructor() { super(<h2 />); } }, <h2 />) : void
>super : typeof Base
>class extends Base { constructor() { super(<h2 />); } } : typeof (Anonymous class)
>Base : Base
>super(<h2 />) : void
>super : typeof Base
><h2 /> : import("preact/jsx-dev-runtime").JSX.Element
>h2 : any
><h2 /> : import("preact/jsx-dev-runtime").JSX.Element
>h2 : any
}
}

34 changes: 34 additions & 0 deletions tsc/testdata/tests/cases/compiler/jsxDevSelfInSuperArguments.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// @jsx: react-jsxdev
// @jsxImportSource: preact
// @target: esnext
// @module: esnext

// @filename: /node_modules/preact/jsx-dev-runtime/index.d.ts
export namespace JSX {
interface Element {}
interface IntrinsicElements {
h2: {};
}
}
export function jsxDEV(type: any, props: any, key?: any, isStatic?: boolean, source?: any, self?: any): JSX.Element;

// @filename: /index.tsx
class Base {
content: any;
constructor(...args: any[]) { this.content = args[0]; }
}
class Card extends Base {
constructor(title: string) {
super(<h2>{title}</h2>);
}
}
class Nested extends Base {
constructor() {
super({ render() { return <h2 />; } });
}
}
class Outer extends Base {
constructor() {
super(class extends Base { constructor() { super(<h2 />); } }, <h2 />);
}
}