ember-codemod-add-component-signatures
Version:
Codemod to add component signatures
28 lines (27 loc) • 1.09 kB
JavaScript
import { AST } from '@codemod-utils/ast-template';
import { getBlockParameterType, normalizeBlockName, } from '../../../utils/components.js';
export function findBlocks(templateFile) {
const traverse = AST.traverse();
const blocksMap = new Map();
traverse(templateFile, {
MustacheStatement(node) {
if (node.path.type !== 'PathExpression' ||
node.path.original !== 'yield') {
return;
}
const toArgument = node.hash.pairs.find(({ key }) => {
return key === 'to';
});
// @ts-expect-error: Assume that types from external packages are correct
const blockName = normalizeBlockName(toArgument?.value.original);
const positionalArgumentTypes = node.params.map(({ type: recastType }) => {
return getBlockParameterType(recastType);
});
blocksMap.set(blockName, positionalArgumentTypes);
},
});
if (blocksMap.size === 0) {
return;
}
return new Map(Array.from(blocksMap).sort());
}