Skip to content

Commit 4562fd0

Browse files
author
Kanchalai Tanglertsampan
committed
Store scanning information whether JSXText is just an all whitespaces
1 parent 17417e9 commit 4562fd0

File tree

4 files changed

+12
-5
lines changed

4 files changed

+12
-5
lines changed

src/compiler/checker.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13318,8 +13318,13 @@ namespace ts {
1331813318
for (const child of (parent as JsxElement).children) {
1331913319
// In React, JSX text that contains only whitespaces will be ignored so we don't want to type-check that
1332013320
// because then type of children property will have constituent of string type.
13321-
if (child.kind !== SyntaxKind.JsxTextAllWhiteSpaces) {
13322-
childrenTypes.push(child.kind === SyntaxKind.JsxText ? stringType : checkExpression(child as Expression, checkMode));
13321+
if (child.kind === SyntaxKind.JsxText) {
13322+
if (!child.containsOnlyWhiteSpaces) {
13323+
childrenTypes.push(stringType);
13324+
}
13325+
}
13326+
else {
13327+
childrenTypes.push(checkExpression(child, checkMode));
1332313328
}
1332413329
}
1332513330
childrenPropSymbol.type = getUnionType(childrenTypes, /*subtypeReduction*/ false);

src/compiler/parser.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3825,7 +3825,8 @@ namespace ts {
38253825
}
38263826

38273827
function parseJsxText(): JsxText {
3828-
const node = <JsxText>createNode(currentToken, scanner.getStartPos());
3828+
const node = <JsxText>createNode(SyntaxKind.JsxText, scanner.getStartPos());
3829+
node.containsOnlyWhiteSpaces = currentToken === SyntaxKind.JsxTextAllWhiteSpaces;
38293830
currentToken = scanner.scanJsxToken();
38303831
return finishNode(node);
38313832
}

src/compiler/scanner.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1729,7 +1729,6 @@ namespace ts {
17291729
// firstNonWhitespace = 0 to indicate that we want leading whitspace,
17301730

17311731
while (pos < end) {
1732-
pos++;
17331732
char = text.charCodeAt(pos);
17341733
if (char === CharacterCodes.openBrace) {
17351734
break;
@@ -1754,6 +1753,7 @@ namespace ts {
17541753
else if (!isWhiteSpaceSingleLine(char)) {
17551754
firstNonWhitespace = pos;
17561755
}
1756+
pos++;
17571757
}
17581758

17591759
return firstNonWhitespace === -1 ? SyntaxKind.JsxTextAllWhiteSpaces : SyntaxKind.JsxText;

src/compiler/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1572,7 +1572,8 @@
15721572
}
15731573

15741574
export interface JsxText extends Node {
1575-
kind: SyntaxKind.JsxText;
1575+
kind: SyntaxKind.JsxText,
1576+
containsOnlyWhiteSpaces: boolean,
15761577
parent?: JsxElement;
15771578
}
15781579

0 commit comments

Comments
 (0)