|
| 1 | +import * as astqts from 'astq'; |
| 2 | +const ASTQ: typeof astqts.ASTQ = astqts as any; |
| 3 | +import { IOptions, InstanceOfResolver } from './index'; |
| 4 | +import { Generator } from './generator'; |
| 5 | +import { parsePropTypes } from './analyzer'; |
| 6 | + |
| 7 | +export enum ExportType { |
| 8 | + default, |
| 9 | + named |
| 10 | +} |
| 11 | + |
| 12 | +export interface IProp { |
| 13 | + type: string; |
| 14 | + optional: boolean; |
| 15 | + importType?: string; |
| 16 | + importPath?: string; |
| 17 | + documentation?: string; |
| 18 | +} |
| 19 | + |
| 20 | +export interface IPropTypes { |
| 21 | + [name: string]: IProp; |
| 22 | +} |
| 23 | + |
| 24 | +export function generateTypings(moduleName: string|null, ast: any, options: IOptions): string { |
| 25 | + const parsingResult = parseAst(ast, options.instanceOfResolver); |
| 26 | + return deprecatedGenerator(options.generator as Generator, moduleName, parsingResult); |
| 27 | +} |
| 28 | + |
| 29 | +function deprecatedGenerator(generator: Generator, moduleName: string|null, |
| 30 | + {exportType, classname, propTypes}: IParsingResult): string { |
| 31 | + const componentName = classname || 'Anonymous'; |
| 32 | + const generateTypings = () => { |
| 33 | + generator.import('* as React', 'react'); |
| 34 | + if (propTypes) { |
| 35 | + Object.keys(propTypes).forEach(propName => { |
| 36 | + const prop = propTypes[propName]; |
| 37 | + if (prop.importType && prop.importPath) { |
| 38 | + generator.import(prop.importType, prop.importPath); |
| 39 | + } |
| 40 | + }); |
| 41 | + } |
| 42 | + generator.nl(); |
| 43 | + generator.props(componentName, propTypes); |
| 44 | + generator.nl(); |
| 45 | + generator.exportDeclaration(exportType, () => { |
| 46 | + generator.class(componentName, !!propTypes); |
| 47 | + }); |
| 48 | + }; |
| 49 | + |
| 50 | + if (moduleName === null) { |
| 51 | + generateTypings(); |
| 52 | + } else { |
| 53 | + generator.declareModule(moduleName, generateTypings); |
| 54 | + } |
| 55 | + return generator.toString(); |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * @internal |
| 60 | + */ |
| 61 | +export interface IParsingResult { |
| 62 | + exportType: ExportType; |
| 63 | + classname: string|undefined; |
| 64 | + functionname: string|undefined; |
| 65 | + propTypes: IPropTypes; |
| 66 | +} |
| 67 | + |
| 68 | +function parseAst(ast: any, instanceOfResolver?: InstanceOfResolver): IParsingResult { |
| 69 | + let exportType: ExportType|undefined; |
| 70 | + let functionname: string|undefined; |
| 71 | + let propTypes: IPropTypes|undefined; |
| 72 | + |
| 73 | + let classname = getClassName(ast); |
| 74 | + if (classname) { |
| 75 | + propTypes = getEs7StyleClassPropTypes(ast, classname, instanceOfResolver); |
| 76 | + exportType = getClassExportType(ast, classname); |
| 77 | + } |
| 78 | + if (!propTypes) { |
| 79 | + const componentName = getComponentNameByPropTypeAssignment(ast); |
| 80 | + if (componentName) { |
| 81 | + const astq = new ASTQ(); |
| 82 | + const exportTypeNodes = astq.query(ast, ` |
| 83 | + //ExportNamedDeclaration // VariableDeclarator[ |
| 84 | + /:id Identifier[@name=='${componentName}'] && |
| 85 | + /:init ArrowFunctionExpression // JSXElement |
| 86 | + ], |
| 87 | + //ExportNamedDeclaration // FunctionDeclaration[/:id Identifier[@name == '${componentName}']] // JSXElement, |
| 88 | + //ExportDefaultDeclaration // AssignmentExpression[/:left Identifier[@name == '${componentName}']] |
| 89 | + // ArrowFunctionExpression // JSXElement, |
| 90 | + //ExportDefaultDeclaration // FunctionDeclaration[/:id Identifier[@name == '${componentName}']] // JSXElement |
| 91 | + `); |
| 92 | + if (exportTypeNodes.length > 0) { |
| 93 | + functionname = componentName; |
| 94 | + exportType = ExportType.named; |
| 95 | + } |
| 96 | + propTypes = getPropTypesFromAssignment(ast, componentName, instanceOfResolver); |
| 97 | + } |
| 98 | + if (!exportType) { |
| 99 | + const astq = new ASTQ(); |
| 100 | + const commonJsExports = astq.query(ast, ` |
| 101 | + // AssignmentExpression[ |
| 102 | + /:left MemberExpression[ |
| 103 | + /:object Identifier[@name == 'exports'] && |
| 104 | + /:property Identifier[@name == 'default'] |
| 105 | + ] && |
| 106 | + /:right Identifier[@name == '${componentName}'] |
| 107 | + ] |
| 108 | + `); |
| 109 | + if (commonJsExports.length > 0) { |
| 110 | + classname = componentName; |
| 111 | + exportType = ExportType.default; |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + if (exportType === undefined) { |
| 117 | + throw new Error('No exported component found'); |
| 118 | + } |
| 119 | + return { |
| 120 | + exportType, |
| 121 | + classname, |
| 122 | + functionname, |
| 123 | + propTypes: propTypes || {} |
| 124 | + }; |
| 125 | +} |
| 126 | + |
| 127 | +function getClassName(ast: any): string|undefined { |
| 128 | + const astq = new ASTQ(); |
| 129 | + const classDeclarationNodes = astq.query(ast, ` |
| 130 | + //ClassDeclaration[ |
| 131 | + /:id Identifier[@name] |
| 132 | + ] |
| 133 | + `); |
| 134 | + if (classDeclarationNodes.length > 0) { |
| 135 | + return classDeclarationNodes[0].id.name; |
| 136 | + } |
| 137 | + return undefined; |
| 138 | +} |
| 139 | + |
| 140 | +function getEs7StyleClassPropTypes(ast: any, classname: string, |
| 141 | + instanceOfResolver?: InstanceOfResolver): IPropTypes|undefined { |
| 142 | + const astq = new ASTQ(); |
| 143 | + const propTypesNodes = astq.query(ast, ` |
| 144 | + //ClassDeclaration[/:id Identifier[@name == '${classname}']] |
| 145 | + //ClassProperty[/:key Identifier[@name == 'propTypes']] |
| 146 | + `); |
| 147 | + if (propTypesNodes.length > 0) { |
| 148 | + return parsePropTypes(propTypesNodes[0].value, instanceOfResolver); |
| 149 | + } |
| 150 | + return undefined; |
| 151 | +} |
| 152 | + |
| 153 | +function getClassExportType(ast: any, classname: string): ExportType|undefined { |
| 154 | + const astq = new ASTQ(); |
| 155 | + const exportTypeNodes = astq.query(ast, ` |
| 156 | + //ExportNamedDeclaration [ |
| 157 | + /ClassDeclaration [ /:id Identifier[@name=='${classname}'] ] |
| 158 | + ], |
| 159 | + //ExportDefaultDeclaration [ |
| 160 | + /ClassDeclaration [ /:id Identifier[@name=='${classname}'] ] |
| 161 | + ] |
| 162 | + `); |
| 163 | + if (exportTypeNodes.length > 0) { |
| 164 | + return exportTypeNodes[0].type === 'ExportDefaultDeclaration' ? ExportType.default : ExportType.named; |
| 165 | + } |
| 166 | + return undefined; |
| 167 | +} |
| 168 | + |
| 169 | +function getComponentNameByPropTypeAssignment(ast: any): string|undefined { |
| 170 | + const astq = new ASTQ(); |
| 171 | + const componentNames = astq.query(ast, ` |
| 172 | + //AssignmentExpression |
| 173 | + /:left MemberExpression[ |
| 174 | + /:object Identifier && |
| 175 | + /:property Identifier[@name == 'propTypes'] |
| 176 | + ] |
| 177 | + `); |
| 178 | + if (componentNames.length > 0) { |
| 179 | + return componentNames[0].object.name; |
| 180 | + } |
| 181 | + return undefined; |
| 182 | +} |
| 183 | + |
| 184 | +function getPropTypesFromAssignment(ast: any, componentName: string, |
| 185 | + instanceOfResolver?: InstanceOfResolver): IPropTypes|undefined { |
| 186 | + const astq = new ASTQ(); |
| 187 | + const propTypesNodes = astq.query(ast, ` |
| 188 | + //AssignmentExpression[ |
| 189 | + /:left MemberExpression[ |
| 190 | + /:object Identifier[@name == '${componentName}'] && |
| 191 | + /:property Identifier[@name == 'propTypes'] |
| 192 | + ] |
| 193 | + ] /:right * |
| 194 | + `); |
| 195 | + if (propTypesNodes.length > 0) { |
| 196 | + return parsePropTypes(propTypesNodes[0], instanceOfResolver); |
| 197 | + } |
| 198 | + return undefined; |
| 199 | +} |
0 commit comments