For JSX text, construct a single literal node "foo bar"
instead of "foo" + " " + "bar"
.
This commit is contained in:
parent
7f84953347
commit
87ae723b52
|
@ -151,28 +151,29 @@ namespace ts {
|
|||
}
|
||||
}
|
||||
|
||||
function visitJsxText(node: JsxText) {
|
||||
const text = getTextOfNode(node, /*includeTrivia*/ true);
|
||||
let parts: Expression[];
|
||||
function visitJsxText(node: JsxText): StringLiteral | undefined {
|
||||
const fixed = fixupWhitespaceAndDecodeEntities(getTextOfNode(node, /*includeTrivia*/ true));
|
||||
return fixed !== undefined && createLiteral(fixed);
|
||||
}
|
||||
|
||||
/**
|
||||
* JSX trims whitespace at the end and beginning of lines, except that the
|
||||
* start/end of a tag is considered a start/end of a line only if that line is
|
||||
* on the same line as the closing tag. See examples in
|
||||
* tests/cases/conformance/jsx/tsxReactEmitWhitespace.tsx
|
||||
* See also https://www.w3.org/TR/html4/struct/text.html#h-9.1 and https://www.w3.org/TR/CSS2/text.html#white-space-model
|
||||
*/
|
||||
function fixupWhitespaceAndDecodeEntities(text: string): string | undefined {
|
||||
let acc: string | undefined;
|
||||
let firstNonWhitespace = 0;
|
||||
let lastNonWhitespace = -1;
|
||||
|
||||
// JSX trims whitespace at the end and beginning of lines, except that the
|
||||
// start/end of a tag is considered a start/end of a line only if that line is
|
||||
// on the same line as the closing tag. See examples in
|
||||
// tests/cases/conformance/jsx/tsxReactEmitWhitespace.tsx
|
||||
for (let i = 0; i < text.length; i++) {
|
||||
const c = text.charCodeAt(i);
|
||||
if (isLineBreak(c)) {
|
||||
if (firstNonWhitespace !== -1 && (lastNonWhitespace - firstNonWhitespace + 1 > 0)) {
|
||||
const part = text.substr(firstNonWhitespace, lastNonWhitespace - firstNonWhitespace + 1);
|
||||
if (!parts) {
|
||||
parts = [];
|
||||
}
|
||||
|
||||
// We do not escape the string here as that is handled by the printer
|
||||
// when it emits the literal. We do, however, need to decode JSX entities.
|
||||
parts.push(createLiteral(decodeEntities(part)));
|
||||
const part = decodeEntities(text.substr(firstNonWhitespace, lastNonWhitespace - firstNonWhitespace + 1));
|
||||
acc = acc === undefined ? part : acc + " " + part;
|
||||
}
|
||||
|
||||
firstNonWhitespace = -1;
|
||||
|
@ -186,28 +187,12 @@ namespace ts {
|
|||
}
|
||||
|
||||
if (firstNonWhitespace !== -1) {
|
||||
const part = text.substr(firstNonWhitespace);
|
||||
if (!parts) {
|
||||
parts = [];
|
||||
}
|
||||
|
||||
// We do not escape the string here as that is handled by the printer
|
||||
// when it emits the literal. We do, however, need to decode JSX entities.
|
||||
parts.push(createLiteral(decodeEntities(part)));
|
||||
const lastPart = decodeEntities(text.substr(firstNonWhitespace));
|
||||
return acc ? acc + lastPart : lastPart;
|
||||
}
|
||||
|
||||
if (parts) {
|
||||
return reduceLeft(parts, aggregateJsxTextParts);
|
||||
else {
|
||||
return acc;
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Aggregates two expressions by interpolating them with a whitespace literal.
|
||||
*/
|
||||
function aggregateJsxTextParts(left: Expression, right: Expression) {
|
||||
return createAdd(createAdd(left, createLiteral(" ")), right);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -41,7 +41,7 @@ var p = 0;
|
|||
<div>
|
||||
</div>;
|
||||
|
||||
// Emit "foo" + ' ' + "bar"
|
||||
// Emit "foo bar"
|
||||
<div>
|
||||
|
||||
foo
|
||||
|
@ -75,5 +75,5 @@ React.createElement("div", null, " 3 ");
|
|||
React.createElement("div", null, "3");
|
||||
// Emit no args
|
||||
React.createElement("div", null);
|
||||
// Emit "foo" + ' ' + "bar"
|
||||
React.createElement("div", null, "foo" + " " + "bar");
|
||||
// Emit "foo bar"
|
||||
React.createElement("div", null, "foo bar");
|
||||
|
|
|
@ -79,7 +79,7 @@ var p = 0;
|
|||
</div>;
|
||||
>div : Symbol(JSX.IntrinsicElements, Decl(file.tsx, 1, 22))
|
||||
|
||||
// Emit "foo" + ' ' + "bar"
|
||||
// Emit "foo bar"
|
||||
<div>
|
||||
>div : Symbol(JSX.IntrinsicElements, Decl(file.tsx, 1, 22))
|
||||
|
||||
|
|
|
@ -88,7 +88,7 @@ var p = 0;
|
|||
</div>;
|
||||
>div : any
|
||||
|
||||
// Emit "foo" + ' ' + "bar"
|
||||
// Emit "foo bar"
|
||||
<div>
|
||||
><div> foo bar </div> : JSX.Element
|
||||
>div : any
|
||||
|
|
|
@ -42,7 +42,7 @@ var p = 0;
|
|||
<div>
|
||||
</div>;
|
||||
|
||||
// Emit "foo" + ' ' + "bar"
|
||||
// Emit "foo bar"
|
||||
<div>
|
||||
|
||||
foo
|
||||
|
|
Loading…
Reference in a new issue