Skip to content

Commit 41108db

Browse files
author
Kanchalai Tanglertsampan
committed
Update emitter to use JSXAttributes node instead of JSXAttribute node array
# Conflicts: # src/compiler/visitor.ts
1 parent 4671685 commit 41108db

4 files changed

Lines changed: 47 additions & 13 deletions

File tree

src/compiler/emitter.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,8 @@ namespace ts {
538538
return emitJsxClosingElement(<JsxClosingElement>node);
539539
case SyntaxKind.JsxAttribute:
540540
return emitJsxAttribute(<JsxAttribute>node);
541+
case SyntaxKind.JsxAttributes:
542+
return emitJsxAttributes(<JsxAttributes>node);
541543
case SyntaxKind.JsxSpreadAttribute:
542544
return emitJsxSpreadAttribute(<JsxSpreadAttribute>node);
543545
case SyntaxKind.JsxExpression:
@@ -1824,15 +1826,21 @@ namespace ts {
18241826
write("<");
18251827
emitJsxTagName(node.tagName);
18261828
write(" ");
1827-
emitList(node, node.attributes, ListFormat.JsxElementAttributes);
1829+
// We are checking here so we won't re-enter the emiting pipeline and emit extra sourcemap
1830+
if (node.attributes.properties && node.attributes.properties.length > 0) {
1831+
emit(node.attributes);
1832+
}
18281833
write("/>");
18291834
}
18301835

18311836
function emitJsxOpeningElement(node: JsxOpeningElement) {
18321837
write("<");
18331838
emitJsxTagName(node.tagName);
1834-
writeIfAny(node.attributes, " ");
1835-
emitList(node, node.attributes, ListFormat.JsxElementAttributes);
1839+
writeIfAny(node.attributes.properties, " ");
1840+
// We are checking here so we won't re-enter the emiting pipeline and emit extra sourcemap
1841+
if (node.attributes.properties && node.attributes.properties.length > 0) {
1842+
emit(node.attributes);
1843+
}
18361844
write(">");
18371845
}
18381846

@@ -1846,6 +1854,10 @@ namespace ts {
18461854
write(">");
18471855
}
18481856

1857+
function emitJsxAttributes(node: JsxAttributes) {
1858+
emitList(node, node.properties, ListFormat.JsxElementAttributes);
1859+
}
1860+
18491861
function emitJsxAttribute(node: JsxAttribute) {
18501862
emit(node.name);
18511863
emitWithPrefix("=", node.initializer);

src/compiler/factory.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,28 +1249,28 @@ namespace ts {
12491249
return node;
12501250
}
12511251

1252-
export function createJsxSelfClosingElement(tagName: JsxTagNameExpression, attributes: JsxAttributeLike[], location?: TextRange) {
1252+
export function createJsxSelfClosingElement(tagName: JsxTagNameExpression, attributes: JsxAttributes, location?: TextRange) {
12531253
const node = <JsxSelfClosingElement>createNode(SyntaxKind.JsxSelfClosingElement, location);
12541254
node.tagName = tagName;
1255-
node.attributes = createNodeArray(attributes);
1255+
node.attributes = attributes;
12561256
return node;
12571257
}
12581258

1259-
export function updateJsxSelfClosingElement(node: JsxSelfClosingElement, tagName: JsxTagNameExpression, attributes: JsxAttributeLike[]) {
1259+
export function updateJsxSelfClosingElement(node: JsxSelfClosingElement, tagName: JsxTagNameExpression, attributes: JsxAttributes) {
12601260
if (node.tagName !== tagName || node.attributes !== attributes) {
12611261
return updateNode(createJsxSelfClosingElement(tagName, attributes, node), node);
12621262
}
12631263
return node;
12641264
}
12651265

1266-
export function createJsxOpeningElement(tagName: JsxTagNameExpression, attributes: JsxAttributeLike[], location?: TextRange) {
1266+
export function createJsxOpeningElement(tagName: JsxTagNameExpression, attributes: JsxAttributes, location?: TextRange) {
12671267
const node = <JsxOpeningElement>createNode(SyntaxKind.JsxOpeningElement, location);
12681268
node.tagName = tagName;
1269-
node.attributes = createNodeArray(attributes);
1269+
node.attributes = attributes;
12701270
return node;
12711271
}
12721272

1273-
export function updateJsxOpeningElement(node: JsxOpeningElement, tagName: JsxTagNameExpression, attributes: JsxAttributeLike[]) {
1273+
export function updateJsxOpeningElement(node: JsxOpeningElement, tagName: JsxTagNameExpression, attributes: JsxAttributes) {
12741274
if (node.tagName !== tagName || node.attributes !== attributes) {
12751275
return updateNode(createJsxOpeningElement(tagName, attributes, node), node);
12761276
}
@@ -1290,6 +1290,20 @@ namespace ts {
12901290
return node;
12911291
}
12921292

1293+
export function createJsxAttributes(properties: JsxAttributeLike[], location?: TextRange) {
1294+
const jsxAttributes = <JsxAttributes>createNode(SyntaxKind.JsxAttributes, location);
1295+
setEmitFlags(jsxAttributes, EmitFlags.NoSourceMap);
1296+
jsxAttributes.properties = createNodeArray(properties);
1297+
return jsxAttributes;
1298+
}
1299+
1300+
export function updateJsxAttributes(jsxAttributes: JsxAttributes, properties: JsxAttributeLike[]) {
1301+
if (jsxAttributes.properties !== properties) {
1302+
return updateNode(createJsxAttributes(properties, jsxAttributes), jsxAttributes);
1303+
}
1304+
return jsxAttributes;
1305+
}
1306+
12931307
export function createJsxAttribute(name: Identifier, initializer: StringLiteral | JsxExpression, location?: TextRange) {
12941308
const node = <JsxAttribute>createNode(SyntaxKind.JsxAttribute, location);
12951309
node.name = name;

src/compiler/transformers/jsx.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ namespace ts {
8585
function visitJsxOpeningLikeElement(node: JsxOpeningLikeElement, children: JsxChild[], isChild: boolean, location: TextRange) {
8686
const tagName = getTagName(node);
8787
let objectProperties: Expression;
88-
const attrs = node.attributes;
88+
const attrs = node.attributes.properties;
8989
if (attrs.length === 0) {
9090
// When there are no attributes, React wants "null"
9191
objectProperties = createNull();

src/compiler/visitor.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -469,13 +469,17 @@ namespace ts {
469469
case SyntaxKind.JsxSelfClosingElement:
470470
case SyntaxKind.JsxOpeningElement:
471471
result = reduceNode((<JsxSelfClosingElement | JsxOpeningElement>node).tagName, cbNode, result);
472-
result = reduceNodes((<JsxSelfClosingElement | JsxOpeningElement>node).attributes, cbNodes, result);
472+
result = reduceNode((<JsxSelfClosingElement | JsxOpeningElement>node).attributes, cbNode, result);
473473
break;
474474

475475
case SyntaxKind.JsxClosingElement:
476476
result = reduceNode((<JsxClosingElement>node).tagName, cbNode, result);
477477
break;
478478

479+
case SyntaxKind.JsxAttributes:
480+
result = reduceNodes((<JsxAttributes>node).properties, cbNodes, result);
481+
break;
482+
479483
case SyntaxKind.JsxAttribute:
480484
result = reduceNode((<JsxAttribute>node).name, cbNode, result);
481485
result = reduceNode((<JsxAttribute>node).initializer, cbNode, result);
@@ -1117,15 +1121,19 @@ namespace ts {
11171121
visitNodes((<JsxElement>node).children, visitor, isJsxChild),
11181122
visitNode((<JsxElement>node).closingElement, visitor, isJsxClosingElement));
11191123

1124+
case SyntaxKind.JsxAttributes:
1125+
return updateJsxAttributes(<JsxAttributes>node,
1126+
visitNodes((<JsxAttributes>node).properties, visitor, isJsxAttributeLike));
1127+
11201128
case SyntaxKind.JsxSelfClosingElement:
11211129
return updateJsxSelfClosingElement(<JsxSelfClosingElement>node,
11221130
visitNode((<JsxSelfClosingElement>node).tagName, visitor, isJsxTagNameExpression),
1123-
visitNodes((<JsxSelfClosingElement>node).attributes, visitor, isJsxAttributeLike));
1131+
visitNode((<JsxSelfClosingElement>node).attributes, visitor, isJsxAttributes));
11241132

11251133
case SyntaxKind.JsxOpeningElement:
11261134
return updateJsxOpeningElement(<JsxOpeningElement>node,
11271135
visitNode((<JsxOpeningElement>node).tagName, visitor, isJsxTagNameExpression),
1128-
visitNodes((<JsxOpeningElement>node).attributes, visitor, isJsxAttributeLike));
1136+
visitNode((<JsxOpeningElement>node).attributes, visitor, isJsxAttributes));
11291137

11301138
case SyntaxKind.JsxClosingElement:
11311139
return updateJsxClosingElement(<JsxClosingElement>node,

0 commit comments

Comments
 (0)