ember-codemod-add-component-signatures
Version:
Codemod to add component signatures
30 lines (29 loc) • 1.26 kB
JavaScript
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import { toEcma } from '@codemod-utils/ast-template-tag';
import { getBaseComponent, getClassPath } from '../../utils/components.js';
function isSupported(file) {
const { importPath } = getBaseComponent(file);
const isComponent = importPath !== undefined;
const isClassicComponent = importPath === '@ember/component';
return isComponent && !isClassicComponent;
}
export function filterComponents(extensionMap, options) {
const { projectRoot } = options;
const filteredEntries = Array.from(extensionMap.entries()).filter(([componentName, extensions]) => {
const hasClassJavaScript = extensions.has('.gjs') || extensions.has('.js');
if (hasClassJavaScript) {
return false;
}
const hasClassTypeScript = extensions.has('.gts') || extensions.has('.ts');
// hbs file only
if (!hasClassTypeScript) {
return true;
}
const filePath = getClassPath(componentName, extensions, options);
const file = readFileSync(join(projectRoot, filePath), 'utf8');
const ecmaFile = toEcma(file);
return isSupported(ecmaFile);
});
return new Map(filteredEntries);
}