-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Fix 8549: Using variable as Jsx tagname #9337
Changes from all commits
24f3f18
49957a1
1369108
2e39d1a
d5976a5
e117d02
7a3f88a
0e17ac8
cc4a5c7
ae38ea5
d07b27e
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 |
---|---|---|
|
@@ -9628,8 +9628,9 @@ namespace ts { | |
/** | ||
* Returns true iff React would emit this tag name as a string rather than an identifier or qualified name | ||
*/ | ||
function isJsxIntrinsicIdentifier(tagName: Identifier | QualifiedName) { | ||
if (tagName.kind === SyntaxKind.QualifiedName) { | ||
function isJsxIntrinsicIdentifier(tagName: JsxTagNameExpression) { | ||
// TODO (yuisu): comment | ||
if (tagName.kind === SyntaxKind.PropertyAccessExpression || tagName.kind === SyntaxKind.ThisKeyword) { | ||
return false; | ||
} | ||
else { | ||
|
@@ -9825,6 +9826,29 @@ namespace ts { | |
})); | ||
} | ||
|
||
// If the elemType is a string type, we have to return anyType to prevent an error downstream as we will try to find construct or call signature of the type | ||
if (elemType.flags & TypeFlags.String) { | ||
return anyType; | ||
} | ||
else if (elemType.flags & TypeFlags.StringLiteral) { | ||
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. Finish sentence in comment |
||
// If the elemType is a stringLiteral type, we can then provide a check to make sure that the string literal type is one of the Jsx intrinsic element type | ||
const intrinsicElementsType = getJsxType(JsxNames.IntrinsicElements); | ||
if (intrinsicElementsType !== unknownType) { | ||
const stringLiteralTypeName = (<StringLiteralType>elemType).text; | ||
const intrinsicProp = getPropertyOfType(intrinsicElementsType, stringLiteralTypeName); | ||
if (intrinsicProp) { | ||
return getTypeOfSymbol(intrinsicProp); | ||
} | ||
const indexSignatureType = getIndexTypeOfType(intrinsicElementsType, IndexKind.String); | ||
if (indexSignatureType) { | ||
return indexSignatureType; | ||
} | ||
error(node, Diagnostics.Property_0_does_not_exist_on_type_1, stringLiteralTypeName, "JSX." + JsxNames.IntrinsicElements); | ||
} | ||
// If we need to report an error, we already done so here. So just return any to prevent any more error downstream | ||
return anyType; | ||
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. Doesn't look like you return Why not make it if (intrinsicElementsType === unknownType) {
return unknownType;
} 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. that is actually intended that if |
||
} | ||
|
||
// Get the element instance type (the result of newing or invoking this tag) | ||
const elemInstanceType = getJsxElementInstanceType(node, elemType); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
//// [tsxDynamicTagName1.tsx] | ||
|
||
var CustomTag = "h1"; | ||
<CustomTag> Hello World </CustomTag> // No error | ||
|
||
//// [tsxDynamicTagName1.jsx] | ||
var CustomTag = "h1"; | ||
<CustomTag> Hello World </CustomTag>; // No error |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
=== tests/cases/conformance/jsx/tsxDynamicTagName1.tsx === | ||
|
||
var CustomTag = "h1"; | ||
>CustomTag : Symbol(CustomTag, Decl(tsxDynamicTagName1.tsx, 1, 3)) | ||
|
||
<CustomTag> Hello World </CustomTag> // No error | ||
>CustomTag : Symbol(CustomTag, Decl(tsxDynamicTagName1.tsx, 1, 3)) | ||
>CustomTag : Symbol(CustomTag, Decl(tsxDynamicTagName1.tsx, 1, 3)) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
=== tests/cases/conformance/jsx/tsxDynamicTagName1.tsx === | ||
|
||
var CustomTag = "h1"; | ||
>CustomTag : string | ||
>"h1" : string | ||
|
||
<CustomTag> Hello World </CustomTag> // No error | ||
><CustomTag> Hello World </CustomTag> : any | ||
>CustomTag : string | ||
>CustomTag : string | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
tests/cases/conformance/jsx/tsxDynamicTagName2.tsx(10,1): error TS2339: Property 'customTag' does not exist on type 'JSX.IntrinsicElements'. | ||
tests/cases/conformance/jsx/tsxDynamicTagName2.tsx(10,25): error TS2339: Property 'customTag' does not exist on type 'JSX.IntrinsicElements'. | ||
|
||
|
||
==== tests/cases/conformance/jsx/tsxDynamicTagName2.tsx (2 errors) ==== | ||
|
||
declare module JSX { | ||
interface Element { } | ||
interface IntrinsicElements { | ||
div: any | ||
} | ||
} | ||
|
||
var customTag = "h1"; | ||
<customTag> Hello World </customTag> // This should be an error. The lower-case is look up as an intrinsic element name | ||
~~~~~~~~~~~ | ||
!!! error TS2339: Property 'customTag' does not exist on type 'JSX.IntrinsicElements'. | ||
~~~~~~~~~~~~ | ||
!!! error TS2339: Property 'customTag' does not exist on type 'JSX.IntrinsicElements'. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
//// [tsxDynamicTagName2.tsx] | ||
|
||
declare module JSX { | ||
interface Element { } | ||
interface IntrinsicElements { | ||
div: any | ||
} | ||
} | ||
|
||
var customTag = "h1"; | ||
<customTag> Hello World </customTag> // This should be an error. The lower-case is look up as an intrinsic element name | ||
|
||
//// [tsxDynamicTagName2.jsx] | ||
var customTag = "h1"; | ||
<customTag> Hello World </customTag>; // This should be an error. The lower-case is look up as an intrinsic element name |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
tests/cases/conformance/jsx/tsxDynamicTagName3.tsx(10,1): error TS2339: Property 'h1' does not exist on type 'JSX.IntrinsicElements'. | ||
|
||
|
||
==== tests/cases/conformance/jsx/tsxDynamicTagName3.tsx (1 errors) ==== | ||
|
||
declare module JSX { | ||
interface Element { } | ||
interface IntrinsicElements { | ||
div: any | ||
} | ||
} | ||
|
||
var CustomTag: "h1" = "h1"; | ||
<CustomTag> Hello World </CustomTag> // This should be an error. we will try look up string literal type in JSX.IntrinsicElements | ||
~~~~~~~~~~~ | ||
!!! error TS2339: Property 'h1' does not exist on type 'JSX.IntrinsicElements'. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
//// [tsxDynamicTagName3.tsx] | ||
|
||
declare module JSX { | ||
interface Element { } | ||
interface IntrinsicElements { | ||
div: any | ||
} | ||
} | ||
|
||
var CustomTag: "h1" = "h1"; | ||
<CustomTag> Hello World </CustomTag> // This should be an error. we will try look up string literal type in JSX.IntrinsicElements | ||
|
||
//// [tsxDynamicTagName3.jsx] | ||
var CustomTag = "h1"; | ||
<CustomTag> Hello World </CustomTag>; // This should be an error. we will try look up string literal type in JSX.IntrinsicElements |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
//// [tsxDynamicTagName4.tsx] | ||
|
||
declare module JSX { | ||
interface Element { } | ||
interface IntrinsicElements { | ||
div: any | ||
h1: any | ||
} | ||
} | ||
|
||
var CustomTag: "h1" = "h1"; | ||
<CustomTag> Hello World </CustomTag> | ||
|
||
//// [tsxDynamicTagName4.jsx] | ||
var CustomTag = "h1"; | ||
<CustomTag> Hello World </CustomTag>; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
=== tests/cases/conformance/jsx/tsxDynamicTagName4.tsx === | ||
|
||
declare module JSX { | ||
>JSX : Symbol(JSX, Decl(tsxDynamicTagName4.tsx, 0, 0)) | ||
|
||
interface Element { } | ||
>Element : Symbol(Element, Decl(tsxDynamicTagName4.tsx, 1, 20)) | ||
|
||
interface IntrinsicElements { | ||
>IntrinsicElements : Symbol(IntrinsicElements, Decl(tsxDynamicTagName4.tsx, 2, 22)) | ||
|
||
div: any | ||
>div : Symbol(IntrinsicElements.div, Decl(tsxDynamicTagName4.tsx, 3, 30)) | ||
|
||
h1: any | ||
>h1 : Symbol(IntrinsicElements.h1, Decl(tsxDynamicTagName4.tsx, 4, 10)) | ||
} | ||
} | ||
|
||
var CustomTag: "h1" = "h1"; | ||
>CustomTag : Symbol(CustomTag, Decl(tsxDynamicTagName4.tsx, 9, 3)) | ||
|
||
<CustomTag> Hello World </CustomTag> | ||
>CustomTag : Symbol(CustomTag, Decl(tsxDynamicTagName4.tsx, 9, 3)) | ||
>CustomTag : Symbol(CustomTag, Decl(tsxDynamicTagName4.tsx, 9, 3)) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
=== tests/cases/conformance/jsx/tsxDynamicTagName4.tsx === | ||
|
||
declare module JSX { | ||
>JSX : any | ||
|
||
interface Element { } | ||
>Element : Element | ||
|
||
interface IntrinsicElements { | ||
>IntrinsicElements : IntrinsicElements | ||
|
||
div: any | ||
>div : any | ||
|
||
h1: any | ||
>h1 : any | ||
} | ||
} | ||
|
||
var CustomTag: "h1" = "h1"; | ||
>CustomTag : "h1" | ||
>"h1" : "h1" | ||
|
||
<CustomTag> Hello World </CustomTag> | ||
><CustomTag> Hello World </CustomTag> : JSX.Element | ||
>CustomTag : "h1" | ||
>CustomTag : "h1" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
//// [tests/cases/conformance/jsx/tsxDynamicTagName5.tsx] //// | ||
|
||
//// [react.d.ts] | ||
|
||
declare module 'react' { | ||
class Component<T, U> { } | ||
} | ||
|
||
//// [app.tsx] | ||
import * as React from 'react'; | ||
|
||
export class Text extends React.Component<{}, {}> { | ||
_tagName: string = 'div'; | ||
|
||
render() { | ||
return ( | ||
<this._tagName /> | ||
); | ||
} | ||
} | ||
|
||
//// [app.jsx] | ||
"use strict"; | ||
var __extends = (this && this.__extends) || function (d, b) { | ||
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
var React = require('react'); | ||
var Text = (function (_super) { | ||
__extends(Text, _super); | ||
function Text() { | ||
_super.apply(this, arguments); | ||
this._tagName = 'div'; | ||
} | ||
Text.prototype.render = function () { | ||
return (<this._tagName />); | ||
}; | ||
return Text; | ||
}(React.Component)); | ||
exports.Text = Text; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
=== tests/cases/conformance/jsx/react.d.ts === | ||
|
||
declare module 'react' { | ||
class Component<T, U> { } | ||
>Component : Symbol(Component, Decl(react.d.ts, 1, 24)) | ||
>T : Symbol(T, Decl(react.d.ts, 2, 17)) | ||
>U : Symbol(U, Decl(react.d.ts, 2, 19)) | ||
} | ||
|
||
=== tests/cases/conformance/jsx/app.tsx === | ||
import * as React from 'react'; | ||
>React : Symbol(React, Decl(app.tsx, 0, 6)) | ||
|
||
export class Text extends React.Component<{}, {}> { | ||
>Text : Symbol(Text, Decl(app.tsx, 0, 31)) | ||
>React.Component : Symbol(React.Component, Decl(react.d.ts, 1, 24)) | ||
>React : Symbol(React, Decl(app.tsx, 0, 6)) | ||
>Component : Symbol(React.Component, Decl(react.d.ts, 1, 24)) | ||
|
||
_tagName: string = 'div'; | ||
>_tagName : Symbol(Text._tagName, Decl(app.tsx, 2, 51)) | ||
|
||
render() { | ||
>render : Symbol(Text.render, Decl(app.tsx, 3, 27)) | ||
|
||
return ( | ||
<this._tagName /> | ||
>this._tagName : Symbol(Text._tagName, Decl(app.tsx, 2, 51)) | ||
>this : Symbol(Text, Decl(app.tsx, 0, 31)) | ||
>_tagName : Symbol(Text._tagName, Decl(app.tsx, 2, 51)) | ||
|
||
); | ||
} | ||
} |
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.
The
else
case here needs to be hardened since it assumestagName: Identifier