-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for transpiling per-file jsx pragmas #21218
Changes from all commits
f4d3143
52a6f22
fa36640
3abda0a
8d5c733
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -299,7 +299,7 @@ namespace ts { | |
resolveName(name, location, meaning, excludeGlobals) { | ||
return resolveName(location, escapeLeadingUnderscores(name), meaning, /*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined, /*isUse*/ false, excludeGlobals); | ||
}, | ||
getJsxNamespace: () => unescapeLeadingUnderscores(getJsxNamespace()), | ||
getJsxNamespace: n => unescapeLeadingUnderscores(getJsxNamespace(n)), | ||
getAccessibleSymbolChain, | ||
getTypePredicateOfSignature, | ||
resolveExternalModuleSymbol, | ||
|
@@ -765,7 +765,23 @@ namespace ts { | |
} | ||
} | ||
|
||
function getJsxNamespace(): __String { | ||
function getJsxNamespace(location: Node | undefined): __String { | ||
if (location) { | ||
const file = getSourceFileOfNode(location); | ||
if (file) { | ||
if (file.localJsxNamespace) { | ||
return file.localJsxNamespace; | ||
} | ||
const jsxPragma = file.pragmas.get("jsx"); | ||
if (jsxPragma) { | ||
const chosenpragma = isArray(jsxPragma) ? jsxPragma[0] : jsxPragma; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not set this one in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It pretty much maps to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I could move the calculation (and make it eager) from the checker into the parser, if you'd like. |
||
file.localJsxFactory = parseIsolatedEntityName(chosenpragma.arguments.factory, languageVersion); | ||
if (file.localJsxFactory) { | ||
return file.localJsxNamespace = getFirstIdentifier(file.localJsxFactory).escapedText; | ||
} | ||
} | ||
} | ||
} | ||
if (!_jsxNamespace) { | ||
_jsxNamespace = "React" as __String; | ||
if (compilerOptions.jsxFactory) { | ||
|
@@ -15082,8 +15098,10 @@ namespace ts { | |
function checkJsxFragment(node: JsxFragment, checkMode: CheckMode): Type { | ||
checkJsxOpeningLikeElementOrOpeningFragment(node.openingFragment, checkMode); | ||
|
||
if (compilerOptions.jsx === JsxEmit.React && compilerOptions.jsxFactory) { | ||
error(node, Diagnostics.JSX_fragment_is_not_supported_when_using_jsxFactory); | ||
if (compilerOptions.jsx === JsxEmit.React && (compilerOptions.jsxFactory || getSourceFileOfNode(node).pragmas.has("jsx"))) { | ||
error(node, compilerOptions.jsxFactory | ||
? Diagnostics.JSX_fragment_is_not_supported_when_using_jsxFactory | ||
: Diagnostics.JSX_fragment_is_not_supported_when_using_an_inline_JSX_factory_pragma); | ||
} | ||
|
||
return getJsxGlobalElementType() || anyType; | ||
|
@@ -15709,7 +15727,7 @@ namespace ts { | |
// The reactNamespace/jsxFactory's root symbol should be marked as 'used' so we don't incorrectly elide its import. | ||
// And if there is no reactNamespace/jsxFactory's symbol in scope when targeting React emit, we should issue an error. | ||
const reactRefErr = diagnostics && compilerOptions.jsx === JsxEmit.React ? Diagnostics.Cannot_find_name_0 : undefined; | ||
const reactNamespace = getJsxNamespace(); | ||
const reactNamespace = getJsxNamespace(node); | ||
const reactLocation = isNodeOpeningLikeElement ? (<JsxOpeningLikeElement>node).tagName : node; | ||
const reactSym = resolveName(reactLocation, reactNamespace, SymbolFlags.Value, reactRefErr, reactNamespace, /*isUse*/ true); | ||
if (reactSym) { | ||
|
@@ -25556,7 +25574,7 @@ namespace ts { | |
return !!(symbol && getCheckFlags(symbol) & CheckFlags.Late); | ||
}, | ||
writeLiteralConstValue, | ||
getJsxFactoryEntity: () => _jsxFactoryEntity | ||
getJsxFactoryEntity: location => location ? (getJsxNamespace(location), (getSourceFileOfNode(location).localJsxFactory || _jsxFactoryEntity)) : _jsxFactoryEntity | ||
}; | ||
|
||
// defined here to avoid outer scope pollution | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is it
jsx
and notjsxNamespace
like the compiler option?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Babel supports
jsx
and I figured we'd want to support the same thing - we can aliasjsxNamespace
, if you'd like?