How to use astq - 7 common examples

To help you get started, we’ve selected a few astq examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github KnisterPeter / react-to-typescript-definitions / src / typings.ts View on Github external
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);
github KnisterPeter / react-to-typescript-definitions / src / deprecated.ts View on Github external
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;
}
github KnisterPeter / react-to-typescript-definitions / src / deprecated.ts View on Github external
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;
}
github KnisterPeter / react-to-typescript-definitions / src / analyzer.ts View on Github external
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;
    }, {});
}
github KnisterPeter / react-to-typescript-definitions / src / deprecated.ts View on Github external
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;
}
github KnisterPeter / react-to-typescript-definitions / src / deprecated.ts View on Github external
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;
}
github KnisterPeter / react-to-typescript-definitions / src / deprecated.ts View on Github external
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;
}

astq

Abstract Syntax Tree (AST) Query Engine

MIT
Latest version published 9 months ago

Package Health Score

58 / 100
Full package analysis

Popular astq functions