Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function createTypings(moduleName: string|null, programAst: any, options: IOptions,
reactImport: string): string {
// #609: configure eol character
dom.config.outputEol = options.eol || '\r\n';
const astq = new ASTQ();
const ast = {
ast: programAst,
query(query: string): any[] {
return astq.query(programAst, query);
},
querySubtree(subtree: any, query: string): any[] {
return astq.query(subtree, query);
}
};
const reactComponentName = getReactComponentName(ast);
const importedPropTypes: ImportedPropTypes = {
propTypesName: getPropTypesName(ast),
propTypes: getImportedPropTypes(ast)
};
const importedTypes = getInstanceOfPropTypes(ast, importedPropTypes);
const importStatements = getImportStatements(ast, importedTypes, options.instanceOfResolver);
function getEs7StyleClassPropTypes(ast: any, classname: string,
instanceOfResolver?: InstanceOfResolver): IPropTypes|undefined {
const astq = new ASTQ();
const propTypesNodes = astq.query(ast, `
//ClassDeclaration[/:id Identifier[@name == '${classname}']]
//ClassProperty[/:key Identifier[@name == 'propTypes']]
`);
if (propTypesNodes.length > 0) {
return parsePropTypes(propTypesNodes[0].value, instanceOfResolver);
}
return undefined;
}
function getComponentNameByPropTypeAssignment(ast: any): string|undefined {
const astq = new ASTQ();
const componentNames = astq.query(ast, `
//AssignmentExpression
/:left MemberExpression[
/:object Identifier &&
/:property Identifier[@name == 'propTypes']
]
`);
if (componentNames.length > 0) {
return componentNames[0].object.name;
}
return undefined;
}
export function parsePropTypes(node: any, instanceOfResolver?: InstanceOfResolver): IPropTypes {
const astq = new ASTQ();
return astq
.query(node, `/ObjectProperty`)
.reduce((propTypes: IPropTypes, propertyNode: IASTNode) => {
const prop: IProp = getTypeFromPropType(propertyNode.value, instanceOfResolver);
prop.documentation = getOptionalDocumentation(propertyNode);
propTypes[propertyNode.key.name] = prop;
return propTypes;
}, {});
}
function getPropTypesFromAssignment(ast: any, componentName: string,
instanceOfResolver?: InstanceOfResolver): IPropTypes|undefined {
const astq = new ASTQ();
const propTypesNodes = astq.query(ast, `
//AssignmentExpression[
/:left MemberExpression[
/:object Identifier[@name == '${componentName}'] &&
/:property Identifier[@name == 'propTypes']
]
] /:right *
`);
if (propTypesNodes.length > 0) {
return parsePropTypes(propTypesNodes[0], instanceOfResolver);
}
return undefined;
}
function getClassName(ast: any): string|undefined {
const astq = new ASTQ();
const classDeclarationNodes = astq.query(ast, `
//ClassDeclaration[
/:id Identifier[@name]
]
`);
if (classDeclarationNodes.length > 0) {
return classDeclarationNodes[0].id.name;
}
return undefined;
}
function getClassExportType(ast: any, classname: string): ExportType|undefined {
const astq = new ASTQ();
const exportTypeNodes = astq.query(ast, `
//ExportNamedDeclaration [
/ClassDeclaration [ /:id Identifier[@name=='${classname}'] ]
],
//ExportDefaultDeclaration [
/ClassDeclaration [ /:id Identifier[@name=='${classname}'] ]
]
`);
if (exportTypeNodes.length > 0) {
return exportTypeNodes[0].type === 'ExportDefaultDeclaration' ? ExportType.default : ExportType.named;
}
return undefined;
}