util.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.getExport = exports.getImport = exports.getInterface = exports.getProps = void 0;
  4. function typeAnnotation(typeAnnotation) {
  5. return {
  6. type: 'TypeAnnotation',
  7. typeAnnotation
  8. };
  9. }
  10. function genericTypeAnnotation(id, typeParameters = null) {
  11. return {
  12. type: 'GenericTypeAnnotation',
  13. id,
  14. typeParameters
  15. };
  16. }
  17. function typeParameters(params) {
  18. return {
  19. type: 'TypeParameterInstantiation',
  20. params
  21. };
  22. }
  23. function qualifiedTypeIdentifier(qualification, id) {
  24. return {
  25. type: 'QualifiedTypeIdentifier',
  26. qualification,
  27. id
  28. };
  29. }
  30. function intersectionTypeAnnotation(types) {
  31. return {
  32. type: 'IntersectionTypeAnnotation',
  33. types
  34. };
  35. }
  36. function interfaceDeclaration(id, body) {
  37. return {
  38. type: 'InterfaceDeclaration',
  39. id,
  40. typeParameters: null,
  41. extends: [],
  42. implements: [],
  43. mixins: [],
  44. body
  45. };
  46. }
  47. function objectTypeAnnotation(properties) {
  48. return {
  49. type: 'ObjectTypeAnnotation',
  50. properties
  51. };
  52. }
  53. function objectTypeProperty(key, value, optional = false) {
  54. return {
  55. type: 'ObjectTypeProperty',
  56. key,
  57. static: false,
  58. proto: false,
  59. kind: 'init',
  60. method: false,
  61. value,
  62. variance: null,
  63. optional
  64. };
  65. }
  66. function addTypeAnotation(obj, typeAnnotation, opts) {
  67. if (!opts.typescript) return obj;
  68. return { ...obj,
  69. typeAnnotation
  70. };
  71. }
  72. function getSvgPropsTypeAnnotation(t, opts) {
  73. if (opts.native) {
  74. return t.genericTypeAnnotation(t.identifier('SvgProps'));
  75. }
  76. return genericTypeAnnotation(qualifiedTypeIdentifier(t.identifier('React'), t.identifier('SVGProps')), typeParameters([genericTypeAnnotation(t.identifier('SVGSVGElement'))]));
  77. }
  78. const getProps = ({
  79. types: t
  80. }, opts) => {
  81. const props = [];
  82. if (opts.titleProp) {
  83. props.push(t.objectProperty(t.identifier('title'), t.identifier('title'), false, true));
  84. props.push(t.objectProperty(t.identifier('titleId'), t.identifier('titleId'), false, true));
  85. }
  86. if (opts.expandProps && props.length > 0) {
  87. props.push(t.restElement(t.identifier('props')));
  88. }
  89. const propsArgument = props.length > 0 ? t.objectPattern(props) : t.identifier('props');
  90. let propsTypeAnnotation;
  91. if (props.length > 0) {
  92. propsTypeAnnotation = genericTypeAnnotation(t.identifier('SVGRProps'));
  93. if (opts.expandProps) {
  94. propsTypeAnnotation = intersectionTypeAnnotation([getSvgPropsTypeAnnotation(t, opts), propsTypeAnnotation]);
  95. }
  96. } else {
  97. propsTypeAnnotation = opts.expandProps ? getSvgPropsTypeAnnotation(t, opts) : t.objectPattern([]);
  98. }
  99. const typedPropsArgument = addTypeAnotation(propsArgument, typeAnnotation(propsTypeAnnotation), opts);
  100. const args = [];
  101. if (opts.expandProps || props.length > 0 || opts.ref) args.push(typedPropsArgument);
  102. if (opts.ref) {
  103. const refArgument = t.identifier(opts.typescript ? 'svgRef?' : 'svgRef');
  104. 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);
  105. args.push(typedRefArgument);
  106. }
  107. return args;
  108. };
  109. exports.getProps = getProps;
  110. const getInterface = ({
  111. types: t
  112. }, opts) => {
  113. if (!opts.typescript) return null;
  114. const properties = [];
  115. if (opts.titleProp) {
  116. properties.push(objectTypeProperty(t.identifier('title'), t.identifier('string'), true));
  117. properties.push(objectTypeProperty(t.identifier('titleId'), t.identifier('string'), true));
  118. }
  119. if (properties.length === 0) return null;
  120. return interfaceDeclaration(t.identifier('SVGRProps'), objectTypeAnnotation(properties));
  121. };
  122. exports.getInterface = getInterface;
  123. const getImport = ({
  124. types: t
  125. }, opts) => {
  126. const importDeclarations = [t.importDeclaration([t.importNamespaceSpecifier(t.identifier('React'))], t.stringLiteral('react'))];
  127. if (opts.native) {
  128. if (opts.native.expo) {
  129. importDeclarations.push(t.importDeclaration([], t.stringLiteral('expo')));
  130. } else {
  131. const imports = [t.importDefaultSpecifier(t.identifier('Svg'))];
  132. if (opts.typescript && opts.expandProps) {
  133. imports.push(t.importSpecifier(t.identifier('SvgProps'), t.identifier('SvgProps')));
  134. }
  135. importDeclarations.push(t.importDeclaration(imports, t.stringLiteral('react-native-svg')));
  136. }
  137. }
  138. return importDeclarations;
  139. };
  140. exports.getImport = getImport;
  141. const getExport = ({
  142. template
  143. }, opts) => {
  144. let result = '';
  145. let exportName = opts.state.componentName;
  146. const plugins = ['jsx'];
  147. if (opts.typescript) {
  148. plugins.push('typescript');
  149. }
  150. if (opts.ref) {
  151. const nextExportName = `ForwardRef`;
  152. result += `const ${nextExportName} = React.forwardRef(${exportName})\n\n`;
  153. exportName = nextExportName;
  154. }
  155. if (opts.memo) {
  156. const nextExportName = `Memo${exportName}`;
  157. result += `const ${nextExportName} = React.memo(${exportName})\n\n`;
  158. exportName = nextExportName;
  159. }
  160. if (opts.state.caller && opts.state.caller.previousExport) {
  161. result += `${opts.state.caller.previousExport}\n`;
  162. result += `export { ${exportName} as ${opts.namedExport} }`;
  163. return template.ast(result, {
  164. plugins
  165. });
  166. }
  167. result += `export default ${exportName}`;
  168. return template.ast(result, {
  169. plugins
  170. });
  171. };
  172. exports.getExport = getExport;