|
| 1 | +import * as ts from 'typescript'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Build props interface from propTypes object |
| 5 | + * @example |
| 6 | + * { |
| 7 | + * foo: React.PropTypes.string.isRequired |
| 8 | + * } |
| 9 | + * |
| 10 | + * becomes |
| 11 | + * { |
| 12 | + * foo: string; |
| 13 | + * } |
| 14 | + * @param objectLiteral |
| 15 | + */ |
| 16 | +export function buildInterfaceFromPropTypeObjectLiteral(objectLiteral: ts.ObjectLiteralExpression) { |
| 17 | + const members = objectLiteral.properties |
| 18 | + // We only need to process PropertyAssignment: |
| 19 | + // { |
| 20 | + // a: 123 // PropertyAssignment |
| 21 | + // } |
| 22 | + // |
| 23 | + // filter out: |
| 24 | + // { |
| 25 | + // a() {}, // MethodDeclaration |
| 26 | + // b, // ShorthandPropertyAssignment |
| 27 | + // ...c, // SpreadAssignment |
| 28 | + // get d() {}, // AccessorDeclaration |
| 29 | + // } |
| 30 | + .filter(ts.isPropertyAssignment) |
| 31 | + // Ignore children, React types have it |
| 32 | + .filter(property => property.name.getText() !== 'children') |
| 33 | + .map(propertyAssignment => { |
| 34 | + const name = propertyAssignment.name.getText(); |
| 35 | + const initializer = propertyAssignment.initializer; |
| 36 | + const isRequired = isPropTypeRequired(initializer); |
| 37 | + const typeExpression = isRequired |
| 38 | + ? // We have guaranteed the type in `isPropTypeRequired()` |
| 39 | + (initializer as ts.PropertyAccessExpression).expression |
| 40 | + : initializer; |
| 41 | + const typeValue = getTypeFromReactPropTypeExpression(typeExpression); |
| 42 | + |
| 43 | + return ts.createPropertySignature( |
| 44 | + [], |
| 45 | + name, |
| 46 | + isRequired ? undefined : ts.createToken(ts.SyntaxKind.QuestionToken), |
| 47 | + typeValue, |
| 48 | + undefined, |
| 49 | + ); |
| 50 | + }); |
| 51 | + |
| 52 | + return ts.createTypeLiteralNode(members); |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * Turns React.PropTypes.* into TypeScript type value |
| 57 | + * |
| 58 | + * @param node React propTypes value |
| 59 | + */ |
| 60 | +function getTypeFromReactPropTypeExpression(node: ts.Expression): ts.TypeNode { |
| 61 | + let result = null; |
| 62 | + if (ts.isPropertyAccessExpression(node)) { |
| 63 | + /** |
| 64 | + * PropTypes.array, |
| 65 | + * PropTypes.bool, |
| 66 | + * PropTypes.func, |
| 67 | + * PropTypes.number, |
| 68 | + * PropTypes.object, |
| 69 | + * PropTypes.string, |
| 70 | + * PropTypes.symbol, (ignore) |
| 71 | + * PropTypes.node, |
| 72 | + * PropTypes.element, |
| 73 | + * PropTypes.any, |
| 74 | + */ |
| 75 | + const text = node.getText().replace(/React\.PropTypes\./, ''); |
| 76 | + |
| 77 | + if (/string/.test(text)) { |
| 78 | + result = ts.createKeywordTypeNode(ts.SyntaxKind.StringKeyword); |
| 79 | + } else if (/any/.test(text)) { |
| 80 | + result = ts.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword); |
| 81 | + } else if (/array/.test(text)) { |
| 82 | + result = ts.createArrayTypeNode(ts.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword)); |
| 83 | + } else if (/bool/.test(text)) { |
| 84 | + result = ts.createKeywordTypeNode(ts.SyntaxKind.BooleanKeyword); |
| 85 | + } else if (/number/.test(text)) { |
| 86 | + result = ts.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword); |
| 87 | + } else if (/object/.test(text)) { |
| 88 | + result = ts.createKeywordTypeNode(ts.SyntaxKind.ObjectKeyword); |
| 89 | + } else if (/node/.test(text)) { |
| 90 | + result = ts.createTypeReferenceNode('React.ReactNode', []); |
| 91 | + } else if (/element/.test(text)) { |
| 92 | + result = ts.createTypeReferenceNode('JSX.Element', []); |
| 93 | + } else if (/func/.test(text)) { |
| 94 | + const arrayOfAny = ts.createParameter( |
| 95 | + [], |
| 96 | + [], |
| 97 | + ts.createToken(ts.SyntaxKind.DotDotDotToken), |
| 98 | + 'args', |
| 99 | + undefined, |
| 100 | + ts.createArrayTypeNode(ts.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword)), |
| 101 | + undefined, |
| 102 | + ); |
| 103 | + result = ts.createFunctionTypeNode([], [arrayOfAny], ts.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword)); |
| 104 | + } |
| 105 | + } else if (ts.isCallExpression(node)) { |
| 106 | + /** |
| 107 | + * PropTypes.instanceOf(), (ignore) |
| 108 | + * PropTypes.oneOf(), // only support oneOf([1, 2]), oneOf(['a', 'b']) |
| 109 | + * PropTypes.oneOfType(), |
| 110 | + * PropTypes.arrayOf(), |
| 111 | + * PropTypes.objectOf(), |
| 112 | + * PropTypes.shape(), |
| 113 | + */ |
| 114 | + const text = node.expression.getText(); |
| 115 | + if (/oneOf$/.test(text)) { |
| 116 | + const argument = node.arguments[0]; |
| 117 | + if (ts.isArrayLiteralExpression(argument)) { |
| 118 | + if (argument.elements.every(elm => ts.isStringLiteral(elm) || ts.isNumericLiteral(elm))) { |
| 119 | + result = ts.createUnionTypeNode( |
| 120 | + (argument.elements as ts.NodeArray<ts.StringLiteral | ts.NumericLiteral>).map(elm => |
| 121 | + ts.createLiteralTypeNode(elm), |
| 122 | + ), |
| 123 | + ); |
| 124 | + } |
| 125 | + } |
| 126 | + } else if (/oneOfType$/.test(text)) { |
| 127 | + const argument = node.arguments[0]; |
| 128 | + if (ts.isArrayLiteralExpression(argument)) { |
| 129 | + result = ts.createUnionOrIntersectionTypeNode( |
| 130 | + ts.SyntaxKind.UnionType, |
| 131 | + argument.elements.map(elm => getTypeFromReactPropTypeExpression(elm)), |
| 132 | + ); |
| 133 | + } |
| 134 | + } else if (/arrayOf$/.test(text)) { |
| 135 | + const argument = node.arguments[0]; |
| 136 | + if (argument) { |
| 137 | + result = ts.createArrayTypeNode(getTypeFromReactPropTypeExpression(argument)); |
| 138 | + } |
| 139 | + } else if (/objectOf$/.test(text)) { |
| 140 | + const argument = node.arguments[0]; |
| 141 | + if (argument) { |
| 142 | + result = ts.createTypeLiteralNode([ |
| 143 | + ts.createIndexSignature( |
| 144 | + undefined, |
| 145 | + undefined, |
| 146 | + [ |
| 147 | + ts.createParameter( |
| 148 | + undefined, |
| 149 | + undefined, |
| 150 | + undefined, |
| 151 | + 'key', |
| 152 | + undefined, |
| 153 | + ts.createKeywordTypeNode(ts.SyntaxKind.StringKeyword), |
| 154 | + ), |
| 155 | + ], |
| 156 | + getTypeFromReactPropTypeExpression(argument), |
| 157 | + ), |
| 158 | + ]); |
| 159 | + } |
| 160 | + } else if (/shape$/.test(text)) { |
| 161 | + const argument = node.arguments[0]; |
| 162 | + if (ts.isObjectLiteralExpression(argument)) { |
| 163 | + return buildInterfaceFromPropTypeObjectLiteral(argument); |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * customProp, |
| 170 | + * anything others |
| 171 | + */ |
| 172 | + if (result === null) { |
| 173 | + result = ts.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword); |
| 174 | + } |
| 175 | + |
| 176 | + return result; |
| 177 | +} |
| 178 | + |
| 179 | +/** |
| 180 | + * Decide if node is required |
| 181 | + * @param node React propTypes member node |
| 182 | + */ |
| 183 | +function isPropTypeRequired(node: ts.Expression) { |
| 184 | + if (!ts.isPropertyAccessExpression(node)) return false; |
| 185 | + |
| 186 | + const text = node.getText().replace(/React\.PropTypes\./, ''); |
| 187 | + return /\.isRequired/.test(text); |
| 188 | +} |
0 commit comments