Skip to content

Commit 3d6950d

Browse files
committed
When emitting use jsx factory entity expression if provided
1 parent 06affa6 commit 3d6950d

File tree

6 files changed

+38
-10
lines changed

6 files changed

+38
-10
lines changed

src/compiler/factory.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1628,7 +1628,34 @@ namespace ts {
16281628
return react;
16291629
}
16301630

1631-
export function createReactCreateElement(reactNamespace: string, tagName: Expression, props: Expression, children: Expression[], parentElement: JsxOpeningLikeElement, location: TextRange): LeftHandSideExpression {
1631+
function createJsxFactoryExpressionFromEntityName(jsxFactory: EntityName, parent: JsxOpeningLikeElement): Expression {
1632+
if (isQualifiedName(jsxFactory)) {
1633+
return createPropertyAccess(
1634+
createJsxFactoryExpressionFromEntityName(
1635+
jsxFactory.left,
1636+
parent
1637+
),
1638+
setEmitFlags(
1639+
getMutableClone(jsxFactory.right),
1640+
EmitFlags.NoSourceMap
1641+
)
1642+
);
1643+
}
1644+
else {
1645+
return createReactNamespace(jsxFactory.text, parent);
1646+
}
1647+
}
1648+
1649+
function createJsxFactoryExpression(jsxFactoryEntity: EntityName, reactNamespace: string, parent: JsxOpeningLikeElement): Expression {
1650+
return jsxFactoryEntity ?
1651+
createJsxFactoryExpressionFromEntityName(jsxFactoryEntity, parent) :
1652+
createPropertyAccess(
1653+
createReactNamespace(reactNamespace, parent),
1654+
"createElement"
1655+
);
1656+
}
1657+
1658+
export function createExpressionForJsxElement(jsxFactoryEntity: EntityName, reactNamespace: string, tagName: Expression, props: Expression, children: Expression[], parentElement: JsxOpeningLikeElement, location: TextRange): LeftHandSideExpression {
16321659
const argumentsList = [tagName];
16331660
if (props) {
16341661
argumentsList.push(props);
@@ -1651,10 +1678,7 @@ namespace ts {
16511678
}
16521679

16531680
return createCall(
1654-
createPropertyAccess(
1655-
createReactNamespace(reactNamespace, parentElement),
1656-
"createElement"
1657-
),
1681+
createJsxFactoryExpression(jsxFactoryEntity, reactNamespace, parentElement),
16581682
/*typeArguments*/ undefined,
16591683
argumentsList,
16601684
location

src/compiler/program.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,7 @@ namespace ts {
731731
writeFile: writeFileCallback || (
732732
(fileName, data, writeByteOrderMark, onError, sourceFiles) => host.writeFile(fileName, data, writeByteOrderMark, onError, sourceFiles)),
733733
isEmitBlocked,
734+
getJsxFactoryEntity: program.getJsxFactoryEntity,
734735
};
735736
}
736737

src/compiler/transformers/jsx.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ namespace ts {
113113
|| createAssignHelper(currentSourceFile.externalHelpersModuleName, segments);
114114
}
115115

116-
const element = createReactCreateElement(
116+
const element = createExpressionForJsxElement(
117+
context.getEmitHost().getJsxFactoryEntity(),
117118
compilerOptions.reactNamespace,
118119
tagName,
119120
objectProperties,

src/compiler/utilities.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ namespace ts {
3434
/* @internal */
3535
isSourceFileFromExternalLibrary(file: SourceFile): boolean;
3636

37+
/* @internal */
38+
getJsxFactoryEntity(): EntityName;
3739
getCommonSourceDirectory(): string;
3840
getCanonicalFileName(fileName: string): string;
3941
getNewLine(): string;

tests/baselines/reference/jsxFactoryIdentifier.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ let c;
7575
class A {
7676
view() {
7777
return [
78-
React.createElement("meta", { content: "helloworld" }),
79-
React.createElement("meta", { content: c.a.b })
78+
createElement("meta", { content: "helloworld" }),
79+
createElement("meta", { content: c.a.b })
8080
];
8181
}
8282
}

tests/baselines/reference/jsxFactoryQualifiedName.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ let c;
7474
class A {
7575
view() {
7676
return [
77-
React.createElement("meta", { content: "helloworld" }),
78-
React.createElement("meta", { content: c.a.b })
77+
Element_1.Element.createElement("meta", { content: "helloworld" }),
78+
Element_1.Element.createElement("meta", { content: c.a.b })
7979
];
8080
}
8181
}

0 commit comments

Comments
 (0)