jscodeshaft
Version:
Collection of more or less primitive helpers and abstractions for JSCodeShift, build for design system migrations and upgrades.
25 lines (22 loc) • 645 B
JavaScript
import { createNamedImportSpecifier } from '../../createNode';
import { hasFragment } from '../hasFragment';
/**
* Ensure that Fragment is imported and exposed as `F`
* @param {function} j JSCodeShift instance
* @param {object} root AST-ified file content
* @returns {void}
*/
export const importFragment = (j, root) => {
root
.find('ImportDeclaration')
.filter(({node}) => (
node.source.value === 'react'
&& node.importKind === 'value'
&& !hasFragment(node)
))
.forEach(({node}) => {
node.specifiers.push(
createNamedImportSpecifier(j)('Fragment', 'F'),
);
});
};