Skip to content

add jsx factory and hold text in jsxtext node #29439

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

Merged
merged 3 commits into from
Mar 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11576,7 +11576,7 @@ namespace ts {
// child is of the type of the expression
return { errorNode: child, innerExpression: child.expression, nameType };
case SyntaxKind.JsxText:
if (child.containsOnlyWhiteSpaces) {
if (child.containsOnlyTriviaWhiteSpaces) {
break; // Whitespace only jsx text isn't real jsx text
}
// child is a string
Expand All @@ -11600,7 +11600,7 @@ namespace ts {
const childrenPropName = childPropName === undefined ? "children" : unescapeLeadingUnderscores(childPropName);
const childrenNameType = getLiteralType(childrenPropName);
const childrenTargetType = getIndexedAccessType(target, childrenNameType);
const validChildren = filter(containingElement.children, i => !isJsxText(i) || !i.containsOnlyWhiteSpaces);
const validChildren = filter(containingElement.children, i => !isJsxText(i) || !i.containsOnlyTriviaWhiteSpaces);
if (!length(validChildren)) {
return result;
}
Expand Down Expand Up @@ -18923,7 +18923,7 @@ namespace ts {
// 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.JsxText) {
if (!child.containsOnlyWhiteSpaces) {
if (!child.containsOnlyTriviaWhiteSpaces) {
childrenTypes.push(stringType);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2979,7 +2979,7 @@ namespace ts {
}

function emitJsxText(node: JsxText) {
writer.writeLiteral(getTextOfNode(node, /*includeTrivia*/ true));
writer.writeLiteral(node.text);
}

function emitJsxClosingElementOrFragment(node: JsxClosingElement | JsxClosingFragment) {
Expand Down
22 changes: 22 additions & 0 deletions src/compiler/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2299,6 +2299,28 @@ namespace ts {
return node;
}

export function createJsxText(text: string, containsOnlyTriviaWhiteSpaces?: boolean) {
const node = <JsxText>createSynthesizedNode(SyntaxKind.JsxText);
node.text = text;
node.containsOnlyTriviaWhiteSpaces = !!containsOnlyTriviaWhiteSpaces;
return node;
}

export function updateJsxText(node: JsxText, text: string, containsOnlyTriviaWhiteSpaces?: boolean) {
return node.text !== text
|| node.containsOnlyTriviaWhiteSpaces !== containsOnlyTriviaWhiteSpaces
? updateNode(createJsxText(text, containsOnlyTriviaWhiteSpaces), node)
: node;
}

export function createJsxOpeningFragment() {
return <JsxOpeningFragment>createSynthesizedNode(SyntaxKind.JsxOpeningFragment);
}

export function createJsxJsxClosingFragment() {
return <JsxClosingFragment>createSynthesizedNode(SyntaxKind.JsxClosingFragment);
}

export function updateJsxFragment(node: JsxFragment, openingFragment: JsxOpeningFragment, children: ReadonlyArray<JsxChild>, closingFragment: JsxClosingFragment) {
return node.openingFragment !== openingFragment
|| node.children !== children
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4256,7 +4256,8 @@ namespace ts {

function parseJsxText(): JsxText {
const node = <JsxText>createNode(SyntaxKind.JsxText);
node.containsOnlyWhiteSpaces = currentToken === SyntaxKind.JsxTextAllWhiteSpaces;
node.text = scanner.getTokenValue();
node.containsOnlyTriviaWhiteSpaces = currentToken === SyntaxKind.JsxTextAllWhiteSpaces;
currentToken = scanner.scanJsxToken();
return finishNode(node);
}
Expand Down
1 change: 1 addition & 0 deletions src/compiler/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2013,6 +2013,7 @@ namespace ts {
pos++;
}

tokenValue = text.substring(startPos, pos);
return firstNonWhitespace === -1 ? SyntaxKind.JsxTextAllWhiteSpaces : SyntaxKind.JsxText;
}

Expand Down
2 changes: 1 addition & 1 deletion src/compiler/transformers/jsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ namespace ts {
}

function visitJsxText(node: JsxText): StringLiteral | undefined {
const fixed = fixupWhitespaceAndDecodeEntities(getTextOfNode(node, /*includeTrivia*/ true));
const fixed = fixupWhitespaceAndDecodeEntities(node.text);
return fixed === undefined ? undefined : createLiteral(fixed);
}

Expand Down
4 changes: 2 additions & 2 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1945,9 +1945,9 @@ namespace ts {
expression?: Expression;
}

export interface JsxText extends Node {
export interface JsxText extends LiteralLikeNode {
kind: SyntaxKind.JsxText;
containsOnlyWhiteSpaces: boolean;
containsOnlyTriviaWhiteSpaces: boolean;
parent: JsxElement;
}

Expand Down
2 changes: 1 addition & 1 deletion src/services/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,7 @@ namespace ts {
}

function isWhiteSpaceOnlyJsxText(node: Node): boolean {
return isJsxText(node) && node.containsOnlyWhiteSpaces;
return isJsxText(node) && node.containsOnlyTriviaWhiteSpaces;
}

export function isInTemplateString(sourceFile: SourceFile, position: number) {
Expand Down
8 changes: 6 additions & 2 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1203,9 +1203,9 @@ declare namespace ts {
dotDotDotToken?: Token<SyntaxKind.DotDotDotToken>;
expression?: Expression;
}
interface JsxText extends Node {
interface JsxText extends LiteralLikeNode {
kind: SyntaxKind.JsxText;
containsOnlyWhiteSpaces: boolean;
containsOnlyTriviaWhiteSpaces: boolean;
parent: JsxElement;
}
type JsxChild = JsxText | JsxExpression | JsxElement | JsxSelfClosingElement | JsxFragment;
Expand Down Expand Up @@ -3992,6 +3992,10 @@ declare namespace ts {
function createJsxClosingElement(tagName: JsxTagNameExpression): JsxClosingElement;
function updateJsxClosingElement(node: JsxClosingElement, tagName: JsxTagNameExpression): JsxClosingElement;
function createJsxFragment(openingFragment: JsxOpeningFragment, children: ReadonlyArray<JsxChild>, closingFragment: JsxClosingFragment): JsxFragment;
function createJsxText(text: string, containsOnlyTriviaWhiteSpaces?: boolean): JsxText;
function updateJsxText(node: JsxText, text: string, containsOnlyTriviaWhiteSpaces?: boolean): JsxText;
function createJsxOpeningFragment(): JsxOpeningFragment;
function createJsxJsxClosingFragment(): JsxClosingFragment;
function updateJsxFragment(node: JsxFragment, openingFragment: JsxOpeningFragment, children: ReadonlyArray<JsxChild>, closingFragment: JsxClosingFragment): JsxFragment;
function createJsxAttribute(name: Identifier, initializer: StringLiteral | JsxExpression): JsxAttribute;
function updateJsxAttribute(node: JsxAttribute, name: Identifier, initializer: StringLiteral | JsxExpression): JsxAttribute;
Expand Down
8 changes: 6 additions & 2 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1203,9 +1203,9 @@ declare namespace ts {
dotDotDotToken?: Token<SyntaxKind.DotDotDotToken>;
expression?: Expression;
}
interface JsxText extends Node {
interface JsxText extends LiteralLikeNode {
kind: SyntaxKind.JsxText;
containsOnlyWhiteSpaces: boolean;
containsOnlyTriviaWhiteSpaces: boolean;
parent: JsxElement;
}
type JsxChild = JsxText | JsxExpression | JsxElement | JsxSelfClosingElement | JsxFragment;
Expand Down Expand Up @@ -3992,6 +3992,10 @@ declare namespace ts {
function createJsxClosingElement(tagName: JsxTagNameExpression): JsxClosingElement;
function updateJsxClosingElement(node: JsxClosingElement, tagName: JsxTagNameExpression): JsxClosingElement;
function createJsxFragment(openingFragment: JsxOpeningFragment, children: ReadonlyArray<JsxChild>, closingFragment: JsxClosingFragment): JsxFragment;
function createJsxText(text: string, containsOnlyTriviaWhiteSpaces?: boolean): JsxText;
function updateJsxText(node: JsxText, text: string, containsOnlyTriviaWhiteSpaces?: boolean): JsxText;
function createJsxOpeningFragment(): JsxOpeningFragment;
function createJsxJsxClosingFragment(): JsxClosingFragment;
function updateJsxFragment(node: JsxFragment, openingFragment: JsxOpeningFragment, children: ReadonlyArray<JsxChild>, closingFragment: JsxClosingFragment): JsxFragment;
function createJsxAttribute(name: Identifier, initializer: StringLiteral | JsxExpression): JsxAttribute;
function updateJsxAttribute(node: JsxAttribute, name: Identifier, initializer: StringLiteral | JsxExpression): JsxAttribute;
Expand Down