How to use flow-parser - 10 common examples

To help you get started, we’ve selected a few flow-parser 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 evcohen / jsx-ast-utils / __tests__ / helper.js View on Github external
function parse(code) {
  if (parserName === undefined) {
    throw new Error('No parser specified');
  }
  if (parserName === 'babel') {
    try {
      return babelParser.parse(code, { plugins, sourceFilename: 'test.js' });
    } catch (_) {
      // eslint-disable-next-line no-console
      console.warn(`Failed to parse with ${fallbackToBabylon ? 'babylon' : 'Babel'} parser.`);
    }
  }
  if (parserName === 'flow') {
    try {
      return flowParser.parse(code, { plugins });
    } catch (_) {
      // eslint-disable-next-line no-console
      console.warn('Failed to parse with the Flow parser');
    }
  }
  throw new Error(`The parser ${parserName} is not yet supported for testing.`);
}
github reactide / reactide / importPath.js View on Github external
function constructSingleLevel(jsxPath) {
  console.log('this is jsx path l 191', jsxPath);
  let reactObj = {};
  // grabs content from file and creates an AST Object
  const fileContent = fs.readFileSync(jsxPath, { encoding: 'utf-8' });
  let jsonObj = flowParser.parse(fileContent);
  // checks for Components in imports section  
  let imports = grabImportNameAndPath(jsonObj);
  let componentTags = grabChildComponents(imports, fileContent);
  // checks if component is Stateful and grabs state;
  let state = grabState(jsonObj);
  // iterates through components array and creates object with Component name, props and path;
  if (componentTags !== null){
    componentTags.forEach(elem => {
      let ast = flowParser.parse(elem).body[0].expression
      reactObj = Object.assign(reactObj, constructComponentProps(ast));
    });
    imports = imports.filter(comp => {
        comp.props = reactObj[comp.name]
        return Object.keys(reactObj).includes(comp.name);
    });
  } else {
github facebook / react-native / website / jsdocs / jsdocs.js View on Github external
function parseSource(source) {
  var lines = source.split('\n');
  var ast = flowParser.parse(source, {
    loc: true,
    comment: true,
    range: true,
    sourceType: 'nonStrictModule',
  });

  /**
   * This sets up genericTransform so that it can be queried above.
   */
  var _state = {
    g: {
      source: source
    }
  };
  if (genericVisitor.test(ast, [], _state)) {
    // HACK: Mark that this file has typechecks on the comments object.
github reactide / reactide / release-builds / Reactide-darwin-x64 / Reactide.app / Contents / Resources / app / importPath.js View on Github external
function constructSingleLevel(jsxPath) {
  let reactObj = {};
  // fileContent stores the file at the jsxPath
  const fileContent = fs.readFileSync(jsxPath, { encoding: 'utf-8' });
  //jsonObj uses flowParser to turn the file into an AST
  let jsonObj = flowParser.parse(fileContent);
  // checks for Components in imports section  
  let imports = grabImportNameAndPath(jsonObj);
  let componentTags = grabChildComponents(imports, fileContent);
  // checks if component is Stateful and grabs state;
  let state = grabState(jsonObj);
  // iterates through components array and creates object with Component name, props and path;
  if (componentTags !== null){
    componentTags.forEach(elem => {
      let ast = flowParser.parse(elem).body[0].expression
      reactObj = Object.assign(reactObj, constructComponentProps(ast));
    });
    imports = imports.filter(comp => {
        comp.props = reactObj[comp.name]
        return Object.keys(reactObj).includes(comp.name);
    });
  } else {
github pichillilorenzo / deps-report / lib / find-dependencies.js View on Github external
files: dependencies.length,
      dependencies
    }

    // Apply a first filter to exclude some files:
    // Don't consider js files where there is no import/require of inputFile
    if ( data.indexOf('require(') == -1 && data.indexOf('import ') == -1  )
      continue 

    if (inputFile.trim().endsWith(".ts")) {
      isTS = true
      ast = typescript.createSourceFile(inputFile, data)
      imports = util.typescriptTraverseAST('kind', [typescript.SyntaxKind.VariableDeclaration, typescript.SyntaxKind.ImportDeclaration], ast)
    }
    else {
      ast = flowParser.parse(data)
      imports = util.flowTraverseAST('type', ['VariableDeclarator', 'ImportDeclaration'], ast)
    }

    for (let imp /*: Object*/ of imports) {
      let dependency = {
        importPath: '',
        importAbsolutePath: '',
        isCircularDependency: null,
        isNodeModule: false,
        specifiers: []
      }

      if (isTS) {
        if ( util.isRequireStatement(imp, true) ) {
          dependency.importPath = imp.initializer.arguments[0].text
          if (options.parent.specifiers && imp.name) {
github brodybits / prettierx / scripts / sync-flow-tests.js View on Github external
function tryParse(file, content) {
  const ast = flowParser.parse(content, {
    esproposal_class_instance_fields: true,
    esproposal_class_static_fields: true,
    esproposal_export_star_as: true
  });

  if (ast.errors.length > 0) {
    const line = ast.errors[0].loc.start.line;
    const column = ast.errors[0].loc.start.column;
    const message = ast.errors[0].message;
    return `${file}:${line}:${column}: ${message}`;
  }

  return null;
}
github benjamn / ast-types / test / flow.ts View on Github external
parse(code: string) {
        return flowParser.parse(code, {
          types: true
        });
      }
    };
github acornjs / acorn / test / bench / common.js View on Github external
'Flow'() {
    const { parse } = req('flow-parser');
    return {
      version: isWorker ? '' : require('flow-parser/package.json').version,
      parse
    };
  },
github JoinColony / colonyJS / packages / colony-js-client / scripts / docgen.js View on Github external
const generateMarkdown = ({ file, templateFile, output }) => {
  const ast = parser.parse(fs.readFileSync(file).toString());

  const callers = [];
  const senders = [];
  const multisig = [];
  const events = [];

  types.visit(ast, {
    visitQualifiedTypeIdentifier(p) {
      if (p.value.id.name === 'Caller') {
        const { params } = p.parent.value.typeParameters;

        callers.push({
          name: getName(p),
          description: getDescription(ast, p),
          args: mapObjectProps(ast, params[0]),
          returns: mapObjectProps(ast, params[1]),
github imranolas / graphql-ast-types / generate / index.js View on Github external
const loadAST = () => {
  const astFilePath = resolve(__dirname, '../node_modules/graphql/language/ast.js.flow');
  const astFile = readFileSync(astFilePath).toString();
  const ast = parse(astFile);

  return ast;
};

flow-parser

JavaScript parser written in OCaml. Produces ESTree AST

MIT
Latest version published 1 day ago

Package Health Score

93 / 100
Full package analysis