Skip to content

Commit b7f6070

Browse files
author
Rowan Wyborn
committed
Initial check in - Support other JSX factories Issue #3788
- added jsxNamespace compile option - when jsx mode is "react", jsxNamespace optionally specifies the emit namespace for React calls, eg "--jsxNamespace MyDOMLib" will emit calls as MyDOMLib.createElement (instead of React.createElement) - symbol specified by jsxNamespace must be present, else compile error is generated (same handling as is done for React symbol when no jsxNamespace is specified)
1 parent bb1e5ab commit b7f6070

File tree

5 files changed

+17
-6
lines changed

5 files changed

+17
-6
lines changed

src/compiler/checker.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8491,13 +8491,14 @@ namespace ts {
84918491
checkGrammarJsxElement(node);
84928492
checkJsxPreconditions(node);
84938493

8494-
// If we're compiling under --jsx react, the symbol 'React' should
8494+
// If we're compiling under --jsx react, the JSX namespace symbol should
84958495
// be marked as 'used' so we don't incorrectly elide its import. And if there
8496-
// is no 'React' symbol in scope, we should issue an error.
8496+
// is no JSX namespace symbol in scope, we should issue an error.
84978497
if (compilerOptions.jsx === JsxEmit.React) {
8498-
const reactSym = resolveName(node.tagName, "React", SymbolFlags.Value, Diagnostics.Cannot_find_name_0, "React");
8499-
if (reactSym) {
8500-
getSymbolLinks(reactSym).referenced = true;
8498+
const jsxNamespace = compilerOptions.jsxNamespace ? compilerOptions.jsxNamespace : "React";
8499+
const jsxSym = resolveName(node.tagName, jsxNamespace, SymbolFlags.Value, Diagnostics.Cannot_find_name_0, jsxNamespace);
8500+
if (jsxSym) {
8501+
getSymbolLinks(jsxSym).referenced = true;
85018502
}
85028503
}
85038504

src/compiler/commandLineParser.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ namespace ts {
5454
description: Diagnostics.Specify_JSX_code_generation_Colon_preserve_or_react,
5555
error: Diagnostics.Argument_for_jsx_must_be_preserve_or_react
5656
},
57+
{
58+
name: "jsxNamespace",
59+
type: "string",
60+
description: Diagnostics.Specify_JSX_emit_namespace_when_JSX_code_generation_mode_is_react
61+
},
5762
{
5863
name: "listFiles",
5964
type: "boolean",

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2389,6 +2389,10 @@
23892389
"category": "Message",
23902390
"code": 6083
23912391
},
2392+
"Specify JSX emit namespace when JSX code generation mode is 'react'": {
2393+
"category": "Message",
2394+
"code": 6084
2395+
},
23922396

23932397
"Variable '{0}' implicitly has an '{1}' type.": {
23942398
"category": "Error",

src/compiler/emitter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1192,7 +1192,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
11921192

11931193
function emitJsxElement(openingNode: JsxOpeningLikeElement, children?: JsxChild[]) {
11941194
const syntheticReactRef = <Identifier>createSynthesizedNode(SyntaxKind.Identifier);
1195-
syntheticReactRef.text = "React";
1195+
syntheticReactRef.text = compilerOptions.jsxNamespace ? compilerOptions.jsxNamespace : "React";
11961196
syntheticReactRef.parent = openingNode;
11971197

11981198
// Call React.createElement(tag, ...

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2382,6 +2382,7 @@ namespace ts {
23822382
inlineSourceMap?: boolean;
23832383
inlineSources?: boolean;
23842384
jsx?: JsxEmit;
2385+
jsxNamespace? : string;
23852386
listFiles?: boolean;
23862387
locale?: string;
23872388
mapRoot?: string;

0 commit comments

Comments
 (0)