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