Store scanning information whether JSXText is just an all whitespaces

This commit is contained in:
Kanchalai Tanglertsampan 2017-04-11 14:55:26 -07:00
parent 17417e9a88
commit 4562fd089c
4 changed files with 12 additions and 5 deletions

View File

@ -13318,8 +13318,13 @@ namespace ts {
for (const child of (parent as JsxElement).children) {
// In React, JSX text that contains only whitespaces will be ignored so we don't want to type-check that
// because then type of children property will have constituent of string type.
if (child.kind !== SyntaxKind.JsxTextAllWhiteSpaces) {
childrenTypes.push(child.kind === SyntaxKind.JsxText ? stringType : checkExpression(child as Expression, checkMode));
if (child.kind === SyntaxKind.JsxText) {
if (!child.containsOnlyWhiteSpaces) {
childrenTypes.push(stringType);
}
}
else {
childrenTypes.push(checkExpression(child, checkMode));
}
}
childrenPropSymbol.type = getUnionType(childrenTypes, /*subtypeReduction*/ false);

View File

@ -3825,7 +3825,8 @@ namespace ts {
}
function parseJsxText(): JsxText {
const node = <JsxText>createNode(currentToken, scanner.getStartPos());
const node = <JsxText>createNode(SyntaxKind.JsxText, scanner.getStartPos());
node.containsOnlyWhiteSpaces = currentToken === SyntaxKind.JsxTextAllWhiteSpaces;
currentToken = scanner.scanJsxToken();
return finishNode(node);
}

View File

@ -1729,7 +1729,6 @@ namespace ts {
// firstNonWhitespace = 0 to indicate that we want leading whitspace,
while (pos < end) {
pos++;
char = text.charCodeAt(pos);
if (char === CharacterCodes.openBrace) {
break;
@ -1754,6 +1753,7 @@ namespace ts {
else if (!isWhiteSpaceSingleLine(char)) {
firstNonWhitespace = pos;
}
pos++;
}
return firstNonWhitespace === -1 ? SyntaxKind.JsxTextAllWhiteSpaces : SyntaxKind.JsxText;

View File

@ -1572,7 +1572,8 @@
}
export interface JsxText extends Node {
kind: SyntaxKind.JsxText;
kind: SyntaxKind.JsxText,
containsOnlyWhiteSpaces: boolean,
parent?: JsxElement;
}