` tags don't have `key` attributes the Stencil vdom will
// re-use the same div element between re-renders, and will just swap out
// the children (the text nodes in this case). If our key insertion
// transformer put unique keys onto each tag then this wouldn't happen any
// longer.
if (ts.isMethodDeclaration(node) && node.name.getText() === 'render' && numReturnStatements(node) === 1) {
return ts.visitEachChild(node, jsxElementVisitor, transformCtx);
} else {
return ts.visitEachChild(node, findRenderMethodVisitor, transformCtx);
}
}
/**
* Our inner-most visitor function. This will edit any JSX nodes that it
* finds, adding a `key` attribute to them via {@link addKeyAttr}.
*
* @param node a typescript syntax tree node
* @returns the result of handling the node
*/
function jsxElementVisitor(node: ts.Node): ts.VisitResult
{
if (ts.isCallExpression(node)) {
// if there are any JSX nodes which are children of the call expression
// (i.e. arguments) we don't want to transform them since we can't know
// _a priori_ what could be done with them at runtime
return node;
} else if (ts.isConditionalExpression(node)) {
// we're going to encounter the same problem here that we encounter with
// multiple return statements, so we just return the node and don't recur into
// its children
return node;
} else if (isJSXElWithAttrs(node)) {
return addKeyAttr(node);
} else {
return ts.visitEachChild(node, jsxElementVisitor, transformCtx);
}
}
return (tsSourceFile) => {
return ts.visitEachChild(tsSourceFile, findClassDeclVisitor, transformCtx);
};
};
/**
* Count the number of return statements in a {@link ts.MethodDeclaration}
*
* @param method the node within which we're going to count `return` statements
* @returns the number of return statements found
*/
function numReturnStatements(method: ts.MethodDeclaration): number {
let count = 0;
function walker(node: ts.Node) {
for (const child of node.getChildren()) {
if (ts.isReturnStatement(child)) {
count++;
} else {
walker(child);
}
}
}
walker(method);
return count;
}
/**
* Type guard to see if a TypeScript syntax node is one of the node types which
* corresponds to a JSX element that can have attributes on it. This is either
* an opening node, like ``, or a 'self-closing' node like
* ``.
*
* @param node a typescript syntax tree node
* @returns whether or not the node is JSX node which could have attributes
*/
function isJSXElWithAttrs(node: ts.Node): node is ts.JsxOpeningElement | ts.JsxSelfClosingElement {
return ts.isJsxOpeningElement(node) || ts.isJsxSelfClosingElement(node);
}
/**
* Given a JSX syntax tree node update it to include a unique key attribute.
* This will respect any attributes already set on the node, including a
* pre-existing, user-defined `key` attribute.
*
* @param jsxElement a typescript JSX syntax tree node
* @returns an updated JSX element, with a key added.
*/
function addKeyAttr(
jsxElement: ts.JsxOpeningElement | ts.JsxSelfClosingElement,
): ts.JsxOpeningElement | ts.JsxSelfClosingElement {
if (jsxElement.attributes.properties.some(isKeyAttr)) {
// this node already has a key! let's get out of here
return jsxElement;
}
const updatedAttributes = ts.factory.createJsxAttributes([
ts.factory.createJsxAttribute(
ts.factory.createIdentifier('key'),
ts.factory.createStringLiteral(deriveJSXKey(jsxElement)),
),
...jsxElement.attributes.properties,
]);
if (ts.isJsxOpeningElement(jsxElement)) {
return ts.factory.updateJsxOpeningElement(
jsxElement,
jsxElement.tagName,
jsxElement.typeArguments,
updatedAttributes,
);
} else {
return ts.factory.updateJsxSelfClosingElement(
jsxElement,
jsxElement.tagName,
jsxElement.typeArguments,
updatedAttributes,
);
}
}
/**
* Check whether or not a JSX attribute node (well, technically a
* {@link ts.JsxAttributeLike} node) has the name `"key"` or not
*
* @param attr the JSX attribute node to check
* @returns whether or not this node has the name 'key'
*/
function isKeyAttr(attr: ts.JsxAttributeLike): boolean {
return !!attr.name && attrNameToString(attr) === 'key';
}
/**
* Given a JSX attribute get its name as a string
*
* @param attr the attribute of interest
* @returns the attribute's name, formatted as a string
*/
function attrNameToString(attr: ts.JsxAttributeLike): string {
switch (attr.name?.kind) {
case ts.SyntaxKind.Identifier:
case ts.SyntaxKind.PrivateIdentifier:
case ts.SyntaxKind.StringLiteral:
case ts.SyntaxKind.NumericLiteral:
return attr.name.text;
case ts.SyntaxKind.JsxNamespacedName:
// this is a JSX attribute name like `foo:bar`
// see https://facebook.github.io/jsx/#prod-JSXNamespacedName
return attr.name.getText();
case ts.SyntaxKind.ComputedPropertyName:
const expression = attr.name.expression;
if (ts.isStringLiteral(expression) || ts.isNumericLiteral(expression)) {
return expression.text;
}
return '';
default:
return '';
}
}