Skip to content
Prev Previous commit
Next Next commit
Address PR
  • Loading branch information
Kanchalai Tanglertsampan committed Jun 24, 2016
commit 0e17ac8e9986ee33e605cd507615b2693502c4ad
11 changes: 6 additions & 5 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: LeftHandSideExpression) {
if (tagName.kind === SyntaxKind.PropertyAccessExpression) {
function isJsxIntrinsicIdentifier(tagName: JsxTagNameExpression) {
// TODO (yuisu): comment
if (tagName.kind === SyntaxKind.PropertyAccessExpression || tagName.kind === SyntaxKind.ThisKeyword) {
return false;
}
Copy link
Copy Markdown
Member

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 assumes tagName: Identifier

else {
Expand Down Expand Up @@ -9830,7 +9831,7 @@ namespace ts {
return anyType;
}
else if (elemType.flags & TypeFlags.StringLiteral) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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 and give
// 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;
Expand All @@ -9843,9 +9844,9 @@ namespace ts {
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;
}
// If we need to report an error, we already done so here. So just return any to prevent any more error downstream
return anyType;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't look like you return anyType/unknownType if you couldn't find an intrinsicElementsType for a string literal.

Why not make it

if (intrinsicElementsType === unknownType) {
    return unknownType;
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that is actually intended that if intrinsicElementsType === unknownType then you just fall through and check for the construct/call though, arguably we should actually return unknownType instead

}

// Get the element instance type (the result of newing or invoking this tag)
Expand Down
15 changes: 10 additions & 5 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3575,7 +3575,7 @@ namespace ts {
return finishNode(node);
}

function tagNamesAreEquivalent(lhs: LeftHandSideExpression, rhs: LeftHandSideExpression): boolean {
function tagNamesAreEquivalent(lhs: JsxTagNameExpression, rhs: JsxTagNameExpression): boolean {
if (lhs.kind !== rhs.kind) {
return false;
}
Expand All @@ -3588,8 +3588,11 @@ namespace ts {
return true;
}

// If we are at this statement then we must have PropertyAccessExpression and because tag name in Jsx element can only
// take forms of JsxTagNameExpression which includes an identifier, "this" expression, or another propertyAccessExpression
// it is safe to case the expression property as such. See parseJsxElementName for how we parse tag name in Jsx element
return (<PropertyAccessExpression>lhs).name.text === (<PropertyAccessExpression>rhs).name.text &&
tagNamesAreEquivalent((<PropertyAccessExpression>lhs).expression, (<PropertyAccessExpression>rhs).expression);
tagNamesAreEquivalent((<PropertyAccessExpression>lhs).expression as JsxTagNameExpression, (<PropertyAccessExpression>rhs).expression as JsxTagNameExpression);
}


Expand Down Expand Up @@ -3720,12 +3723,14 @@ namespace ts {
return finishNode(node);
}

function parseJsxElementName(): LeftHandSideExpression {
function parseJsxElementName(): JsxTagNameExpression {
scanJsxIdentifier();
// JsxElement can have name in the form of property-access expression or an identifier or string literal
// JsxElement can have name in the form of
// propertyAccessExpression
// primaryExpression in the form of an identifier and "this" keyword
// We can't just simply use parseLeftHandSideExpressionOrHigher because then we will start consider class,function etc as a keyword
// We only want to consider "this" as a primaryExpression
let expression: LeftHandSideExpression = token === SyntaxKind.ThisKeyword ?
let expression: JsxTagNameExpression = token === SyntaxKind.ThisKeyword ?
parseTokenNode<PrimaryExpression>() : parseIdentifierName();
while (parseOptional(SyntaxKind.DotToken)) {
const propertyAccess: PropertyAccessExpression = <PropertyAccessExpression>createNode(SyntaxKind.PropertyAccessExpression, expression.pos);
Expand Down
6 changes: 4 additions & 2 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1042,11 +1042,13 @@ namespace ts {
closingElement: JsxClosingElement;
}

export type JsxTagNameExpression = PrimaryExpression | PropertyAccessExpression;

/// The opening element of a <Tag>...</Tag> JsxElement
// @kind(SyntaxKind.JsxOpeningElement)
export interface JsxOpeningElement extends Expression {
_openingElementBrand?: any;
tagName: LeftHandSideExpression;
tagName: JsxTagNameExpression;
attributes: NodeArray<JsxAttribute | JsxSpreadAttribute>;
}

Expand All @@ -1073,7 +1075,7 @@ namespace ts {

// @kind(SyntaxKind.JsxClosingElement)
export interface JsxClosingElement extends Node {
tagName: LeftHandSideExpression;
tagName: JsxTagNameExpression;
}

// @kind(SyntaxKind.JsxExpression)
Expand Down