MCPcopy Index your code
hub / github.com/dropbox/ts-transform-react-constant-elements / constantElementVisitor

Function constantElementVisitor

src/transform.ts:105–128  ·  view source on GitHub ↗

* Visit nodes recursively and try to determine if node's * considered a constant node. * NOTE: This modifies hoistedVariables inline * * @param {ts.TransformationContext} ctx transformation context * @param {HoistedVariables} hoistedVariables hoistedVariables to populate * @returns {ts.Visitor

(
  ctx: ts.TransformationContext,
  hoistedVariables: ts.VariableStatement[]
)

Source from the content-addressed store, hash-verified

103 * @returns {ts.Visitor}
104 */
105function constantElementVisitor(
106 ctx: ts.TransformationContext,
107 hoistedVariables: ts.VariableStatement[]
108): ts.Visitor {
109 const visitor: ts.Visitor = node => {
110 if (isConstantElement(node)) {
111 const variable = ts.createUniqueName("hoisted_constant_element");
112 const statement = ts.createVariableStatement(
113 undefined,
114 ts.createVariableDeclarationList([
115 ts.createVariableDeclaration(variable, undefined, node)
116 ])
117 );
118 // Store the variable assignement to hoist later
119 hoistedVariables.push(statement);
120
121 // Replace <foo /> with {hoisted_constant_element_1}
122 // TODO: Figure out case like `return <foo />
123 return ts.createJsxExpression(undefined, variable);
124 }
125 return ts.visitEachChild(node, visitor, ctx);
126 };
127 return visitor;
128}
129
130function visitSourceFile(
131 ctx: ts.TransformationContext,

Callers 1

visitSourceFileFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected