Skip to content

A few JS utilities for generating ASTs for a directory of files.

Notifications You must be signed in to change notification settings

ds-pack/ast-crawler

Repository files navigation

AST Crawler

Two small utilities for generating an AST Map (mapping of file paths to ast for their contents) and for visiting each of those entries in the AST Map.

Installation:

yarn add @matthamlin/ast-crawler

Usage:

import { generateASTMap, visit, find } from '@matthamlin/ast-crawler'

// Implementing a local cache for parsers
import { parse } from '@babel/parser'
import path from 'path'

let parsers = {}

function createParser(filepath) {
  let extension = path.extname(filepath)
  if (parsers[extension]) {
    return parsers[extension]
  }
  if (extension === '.tsx' || extension === '.ts') {
    parsers['.ts'] = parsers['.tsx'] = (contents) => {
      return parse(contents, {
        plugins: ['typescript'],
        sourceType: 'module',
      })
    }
    return parsers['.ts']
  } else {
    parsers[extension] = (contents) => {
      return parse(contents, {
        plugins: ['flow'],
        sourceType: 'module',
      })
    }
    return parsers[extension]
  }
}

let astMap = generateASTMap({
  files: find({ root: path.join('./src'), extensions: ['.js', '.ts', '.tsx'] }),
  createParser,
})

function visitor({ ast, path: filePath }) {
  // do whatever you want here with the ast and the filepath
}

visit({
  visitor,
  astMap,
})

Tools:

  • Typescript
  • Babel
  • Jest

TODO:

  • Refine AST type

About

A few JS utilities for generating ASTs for a directory of files.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published