|
| 1 | +function setParamTypeName(path) { |
| 2 | + if ( |
| 3 | + path.parentPath.node.id.typeAnnotation && |
| 4 | + path.parentPath.node.id.typeAnnotation.typeAnnotation.typeParameters && |
| 5 | + path.parentPath.node.id.typeAnnotation.typeAnnotation.typeParameters.params[0].typeName.name |
| 6 | + ) { |
| 7 | + return path.parentPath.node.id.typeAnnotation.typeAnnotation.typeParameters.params[0] |
| 8 | + .typeName.name; |
| 9 | + } |
| 10 | + |
| 11 | + return path.parentPath.node.id.name; |
| 12 | +} |
| 13 | + |
| 14 | +function getTypePath(path) { |
| 15 | + if ( |
| 16 | + path.parentPath.parentPath.parentPath.parentPath.value && |
| 17 | + path.parentPath.parentPath.parentPath.parentPath.value.find |
| 18 | + ) { |
| 19 | + return path.parentPath.parentPath.parentPath.parentPath.value; |
| 20 | + } |
| 21 | + |
| 22 | + if ( |
| 23 | + path.parentPath.parentPath.parentPath.parentPath.parentPath && |
| 24 | + path.parentPath.parentPath.parentPath.parentPath.parentPath.value |
| 25 | + ) { |
| 26 | + return path.parentPath.parentPath.parentPath.parentPath.parentPath.value; |
| 27 | + } |
| 28 | + |
| 29 | + return []; |
| 30 | +} |
| 31 | + |
| 32 | +function checkForProptypes(path, paramTypeName) { |
| 33 | + const propsDefinition = getTypePath(path).find(propertyPath => { |
| 34 | + return ( |
| 35 | + propertyPath.type && |
| 36 | + propertyPath.type === 'ExpressionStatement' && |
| 37 | + propertyPath.expression && |
| 38 | + propertyPath.expression.left.object.name === paramTypeName && |
| 39 | + propertyPath.expression.right.type === 'ObjectExpression' && |
| 40 | + Boolean(propertyPath.expression.right.properties) |
| 41 | + ); |
| 42 | + }); |
| 43 | + |
| 44 | + return Boolean(propsDefinition); |
| 45 | +} |
| 46 | + |
| 47 | +function setParamsTypeDefinitionFromFunctionType(documentation, path) { |
| 48 | + if (path.parentPath.node.init && path.parentPath.node.init.params.length === 0) { |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + if ( |
| 53 | + path.node.type === 'ArrowFunctionExpression' && |
| 54 | + !path.parentPath.node.init.params[0].typeAnnotation |
| 55 | + ) { |
| 56 | + const paramTypeName = setParamTypeName(path); |
| 57 | + |
| 58 | + const hasPropTypes = checkForProptypes(path, paramTypeName); |
| 59 | + |
| 60 | + if (hasPropTypes) { |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + let typePath = getTypePath(path).find(propertyPath => { |
| 65 | + if ( |
| 66 | + propertyPath.type === 'ExportNamedDeclaration' && |
| 67 | + propertyPath.declaration && |
| 68 | + propertyPath.declaration.id && |
| 69 | + propertyPath.declaration.id.name === paramTypeName |
| 70 | + ) { |
| 71 | + return true; |
| 72 | + } |
| 73 | + |
| 74 | + if ( |
| 75 | + propertyPath.type === 'TSInterfaceDeclaration' && |
| 76 | + propertyPath.id && |
| 77 | + propertyPath.id.name === paramTypeName |
| 78 | + ) { |
| 79 | + return true; |
| 80 | + } |
| 81 | + |
| 82 | + return ( |
| 83 | + propertyPath.type === 'TSTypeAliasDeclaration' && |
| 84 | + propertyPath.id.name === paramTypeName |
| 85 | + ); |
| 86 | + }); |
| 87 | + |
| 88 | + typePath = |
| 89 | + typePath && typePath.type && typePath.type === 'ExportNamedDeclaration' |
| 90 | + ? typePath.declaration |
| 91 | + : typePath; |
| 92 | + |
| 93 | + if (typePath) { |
| 94 | + const typedParam = Object.assign({}, path.parentPath.node.init.params[0], { |
| 95 | + typeAnnotation: typePath, |
| 96 | + }); |
| 97 | + |
| 98 | + path.parentPath.node.init.params = [typedParam]; |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +module.exports = setParamsTypeDefinitionFromFunctionType; |
0 commit comments