UNPKG

ember-codemod-add-component-signatures

Version:
35 lines (34 loc) 1.52 kB
import { AST } from '@codemod-utils/ast-javascript'; export function updateReferences(file, options) { const traverse = AST.traverse(true); const { baseComponentName, componentName, data } = options; const ast = traverse(file, { visitExportDefaultDeclaration(path) { switch (path.node.declaration.type) { case 'CallExpression': { if (path.node.declaration.callee.type !== 'Identifier' || path.node.declaration.callee.name !== baseComponentName) { return false; } const nodesToAdd = [ AST.builders.noop(), AST.builders.exportDefaultDeclaration(AST.builders.identifier(data.entity.pascalizedName)), ]; path.parentPath.value.push(...nodesToAdd); return AST.builders.variableDeclaration('const', [ AST.builders.variableDeclarator(AST.builders.identifier(data.entity.pascalizedName), path.node.declaration), ]); } case 'Identifier': { if (path.node.declaration.name !== componentName) { return false; } path.node.declaration.name = data.entity.pascalizedName; return false; } } return false; }, }); return AST.print(ast); }