From b7f60704bb7f6ad941148cab586afd3e26eff41e Mon Sep 17 00:00:00 2001 From: Rowan Wyborn Date: Fri, 18 Dec 2015 21:56:08 +1100 Subject: [PATCH 1/2] 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) --- src/compiler/checker.ts | 11 ++++++----- src/compiler/commandLineParser.ts | 5 +++++ src/compiler/diagnosticMessages.json | 4 ++++ src/compiler/emitter.ts | 2 +- src/compiler/types.ts | 1 + 5 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 2b3852eede..76f218906b 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -8491,13 +8491,14 @@ namespace ts { checkGrammarJsxElement(node); checkJsxPreconditions(node); - // If we're compiling under --jsx react, the symbol 'React' should + // If we're compiling under --jsx react, the JSX namespace symbol should // be marked as 'used' so we don't incorrectly elide its import. And if there - // is no 'React' symbol in scope, we should issue an error. + // is no JSX namespace symbol in scope, we should issue an error. if (compilerOptions.jsx === JsxEmit.React) { - const reactSym = resolveName(node.tagName, "React", SymbolFlags.Value, Diagnostics.Cannot_find_name_0, "React"); - if (reactSym) { - getSymbolLinks(reactSym).referenced = true; + const jsxNamespace = compilerOptions.jsxNamespace ? compilerOptions.jsxNamespace : "React"; + const jsxSym = resolveName(node.tagName, jsxNamespace, SymbolFlags.Value, Diagnostics.Cannot_find_name_0, jsxNamespace); + if (jsxSym) { + getSymbolLinks(jsxSym).referenced = true; } } diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index 034b7022e8..210c6f7293 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -54,6 +54,11 @@ namespace ts { description: Diagnostics.Specify_JSX_code_generation_Colon_preserve_or_react, error: Diagnostics.Argument_for_jsx_must_be_preserve_or_react }, + { + name: "jsxNamespace", + type: "string", + description: Diagnostics.Specify_JSX_emit_namespace_when_JSX_code_generation_mode_is_react + }, { name: "listFiles", type: "boolean", diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 1d1510e6eb..ec57722c9a 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -2389,6 +2389,10 @@ "category": "Message", "code": 6083 }, + "Specify JSX emit namespace when JSX code generation mode is 'react'": { + "category": "Message", + "code": 6084 + }, "Variable '{0}' implicitly has an '{1}' type.": { "category": "Error", diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index f0b4985473..a5f2228f7e 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -1192,7 +1192,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi function emitJsxElement(openingNode: JsxOpeningLikeElement, children?: JsxChild[]) { const syntheticReactRef = createSynthesizedNode(SyntaxKind.Identifier); - syntheticReactRef.text = "React"; + syntheticReactRef.text = compilerOptions.jsxNamespace ? compilerOptions.jsxNamespace : "React"; syntheticReactRef.parent = openingNode; // Call React.createElement(tag, ... diff --git a/src/compiler/types.ts b/src/compiler/types.ts index a8c4ee211c..977b2efac6 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2382,6 +2382,7 @@ namespace ts { inlineSourceMap?: boolean; inlineSources?: boolean; jsx?: JsxEmit; + jsxNamespace? : string; listFiles?: boolean; locale?: string; mapRoot?: string; From 2395890268ebfe6daf053d9c3b3e29555c307226 Mon Sep 17 00:00:00 2001 From: Rowan Wyborn Date: Fri, 18 Dec 2015 22:25:23 +1100 Subject: [PATCH 2/2] fix trailing whitespace --- src/compiler/types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 977b2efac6..d09ebfef81 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2382,7 +2382,7 @@ namespace ts { inlineSourceMap?: boolean; inlineSources?: boolean; jsx?: JsxEmit; - jsxNamespace? : string; + jsxNamespace? : string; listFiles?: boolean; locale?: string; mapRoot?: string;