123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- "use strict";
- exports.__esModule = true;
- exports.getExport = exports.getImport = exports.getInterface = exports.getProps = void 0;
- function typeAnnotation(typeAnnotation) {
- return {
- type: 'TypeAnnotation',
- typeAnnotation
- };
- }
- function genericTypeAnnotation(id, typeParameters = null) {
- return {
- type: 'GenericTypeAnnotation',
- id,
- typeParameters
- };
- }
- function typeParameters(params) {
- return {
- type: 'TypeParameterInstantiation',
- params
- };
- }
- function qualifiedTypeIdentifier(qualification, id) {
- return {
- type: 'QualifiedTypeIdentifier',
- qualification,
- id
- };
- }
- function intersectionTypeAnnotation(types) {
- return {
- type: 'IntersectionTypeAnnotation',
- types
- };
- }
- function interfaceDeclaration(id, body) {
- return {
- type: 'InterfaceDeclaration',
- id,
- typeParameters: null,
- extends: [],
- implements: [],
- mixins: [],
- body
- };
- }
- function objectTypeAnnotation(properties) {
- return {
- type: 'ObjectTypeAnnotation',
- properties
- };
- }
- function objectTypeProperty(key, value, optional = false) {
- return {
- type: 'ObjectTypeProperty',
- key,
- static: false,
- proto: false,
- kind: 'init',
- method: false,
- value,
- variance: null,
- optional
- };
- }
- function addTypeAnotation(obj, typeAnnotation, opts) {
- if (!opts.typescript) return obj;
- return { ...obj,
- typeAnnotation
- };
- }
- function getSvgPropsTypeAnnotation(t, opts) {
- if (opts.native) {
- return t.genericTypeAnnotation(t.identifier('SvgProps'));
- }
- return genericTypeAnnotation(qualifiedTypeIdentifier(t.identifier('React'), t.identifier('SVGProps')), typeParameters([genericTypeAnnotation(t.identifier('SVGSVGElement'))]));
- }
- const getProps = ({
- types: t
- }, opts) => {
- const props = [];
- if (opts.titleProp) {
- props.push(t.objectProperty(t.identifier('title'), t.identifier('title'), false, true));
- props.push(t.objectProperty(t.identifier('titleId'), t.identifier('titleId'), false, true));
- }
- if (opts.expandProps && props.length > 0) {
- props.push(t.restElement(t.identifier('props')));
- }
- const propsArgument = props.length > 0 ? t.objectPattern(props) : t.identifier('props');
- let propsTypeAnnotation;
- if (props.length > 0) {
- propsTypeAnnotation = genericTypeAnnotation(t.identifier('SVGRProps'));
- if (opts.expandProps) {
- propsTypeAnnotation = intersectionTypeAnnotation([getSvgPropsTypeAnnotation(t, opts), propsTypeAnnotation]);
- }
- } else {
- propsTypeAnnotation = opts.expandProps ? getSvgPropsTypeAnnotation(t, opts) : t.objectPattern([]);
- }
- const typedPropsArgument = addTypeAnotation(propsArgument, typeAnnotation(propsTypeAnnotation), opts);
- const args = [];
- if (opts.expandProps || props.length > 0 || opts.ref) args.push(typedPropsArgument);
- if (opts.ref) {
- const refArgument = t.identifier(opts.typescript ? 'svgRef?' : 'svgRef');
- const typedRefArgument = addTypeAnotation(refArgument, typeAnnotation(genericTypeAnnotation(qualifiedTypeIdentifier(t.identifier('React'), t.identifier('Ref')), typeParameters([opts.native ? genericTypeAnnotation(qualifiedTypeIdentifier(t.identifier('React'), t.identifier('Component')), typeParameters([genericTypeAnnotation(t.identifier('SvgProps'))])) : genericTypeAnnotation(t.identifier('SVGSVGElement'))]))), opts);
- args.push(typedRefArgument);
- }
- return args;
- };
- exports.getProps = getProps;
- const getInterface = ({
- types: t
- }, opts) => {
- if (!opts.typescript) return null;
- const properties = [];
- if (opts.titleProp) {
- properties.push(objectTypeProperty(t.identifier('title'), t.identifier('string'), true));
- properties.push(objectTypeProperty(t.identifier('titleId'), t.identifier('string'), true));
- }
- if (properties.length === 0) return null;
- return interfaceDeclaration(t.identifier('SVGRProps'), objectTypeAnnotation(properties));
- };
- exports.getInterface = getInterface;
- const getImport = ({
- types: t
- }, opts) => {
- const importDeclarations = [t.importDeclaration([t.importNamespaceSpecifier(t.identifier('React'))], t.stringLiteral('react'))];
- if (opts.native) {
- if (opts.native.expo) {
- importDeclarations.push(t.importDeclaration([], t.stringLiteral('expo')));
- } else {
- const imports = [t.importDefaultSpecifier(t.identifier('Svg'))];
- if (opts.typescript && opts.expandProps) {
- imports.push(t.importSpecifier(t.identifier('SvgProps'), t.identifier('SvgProps')));
- }
- importDeclarations.push(t.importDeclaration(imports, t.stringLiteral('react-native-svg')));
- }
- }
- return importDeclarations;
- };
- exports.getImport = getImport;
- const getExport = ({
- template
- }, opts) => {
- let result = '';
- let exportName = opts.state.componentName;
- const plugins = ['jsx'];
- if (opts.typescript) {
- plugins.push('typescript');
- }
- if (opts.ref) {
- const nextExportName = `ForwardRef`;
- result += `const ${nextExportName} = React.forwardRef(${exportName})\n\n`;
- exportName = nextExportName;
- }
- if (opts.memo) {
- const nextExportName = `Memo${exportName}`;
- result += `const ${nextExportName} = React.memo(${exportName})\n\n`;
- exportName = nextExportName;
- }
- if (opts.state.caller && opts.state.caller.previousExport) {
- result += `${opts.state.caller.previousExport}\n`;
- result += `export { ${exportName} as ${opts.namedExport} }`;
- return template.ast(result, {
- plugins
- });
- }
- result += `export default ${exportName}`;
- return template.ast(result, {
- plugins
- });
- };
- exports.getExport = getExport;
|