Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Proposal] we should provide a function to build a fork with defs extracted from @babel/types #823

Open
jedwards1211 opened this issue Aug 29, 2022 · 4 comments

Comments

@jedwards1211
Copy link
Contributor

jedwards1211 commented Aug 29, 2022

I've been thinking, sometimes Babel nodes and fields are missing from the defs in ast-types, it's naturally going to lag behind node types and fields that they add. Why not build the defs from the information in @babel/types? Here's what I have so far. Seems to work well!

import typesPlugin from 'ast-types/lib/types'
import * as t from '@babel/types'
import { mapValues } from 'lodash'
import fork from 'ast-types/fork'
import { Fork } from 'ast-types/types'
import { parse } from '@babel/parser'
import nodePathPlugin from 'ast-types/lib/node-path'

function babel(fork: Fork) {
  const types = fork.use(typesPlugin)
  const { builtInTypes, Type } = types
  const { def, or } = Type

  fork.use(nodePathPlugin)

  def('Node').field('type', builtInTypes.string)

  function tryConvertValidate(validate: any, node?: any): any {
    if (validate.type) {
      switch (validate.type) {
        case 'any':
          return {}
        case 'string':
          return builtInTypes.string
        case 'boolean':
          return builtInTypes.boolean
        case 'number':
          return builtInTypes.number
        case 'null':
          return builtInTypes.null
        case 'undefined':
          return builtInTypes.undefined
      }
    }
    if (validate.chainOf) {
      for (const elem of validate.chainOf) {
        const converted = tryConvertValidate(elem)
        if (converted) return converted
      }
    }
    if (validate.each) {
      return [convertValidate(validate.each)]
    }
    if (validate.oneOfNodeTypes) {
      return or(...validate.oneOfNodeTypes.map((type: string) => def(type)))
    }
    if (validate.oneOf) {
      return node?.optional
        ? or(...validate.oneOf, null)
        : or(...validate.oneOf)
    }
    if (validate.shapeOf) {
      return mapValues(validate.shapeOf, (value) =>
        convertValidate(value.validate, value)
      )
    }
    if (validate.oneOfNodeOrValueTypes) {
      return or(
        ...validate.oneOfNodeOrValueTypes.map((type: string) =>
          /^[A-Z]/.test(type) ? def(type) : convertValidate({ type })
        )
      )
    }
  }

  function convertValidate(validate: any, node?: any): any {
    const converted = tryConvertValidate(validate, node)
    if (!converted) {
      throw new Error(
        `couldn't determine field def for validate: ${JSON.stringify(validate)}`
      )
    }
    return converted
  }

  for (const [type, fields] of Object.entries(t.NODE_FIELDS)) {
    const d = def(type)
    const aliases: string[] | undefined = (t.ALIAS_KEYS as any)[type]
    if (aliases) {
      for (const alias of aliases) {
        def(alias)
      }
      d.bases('Node', ...aliases)
    } else {
      d.bases('Node')
    }
    for (const [field, { validate }] of Object.entries(fields)) {
      d.field(field, convertValidate(validate))
    }
  }
}

const { visit } = fork([babel])

const ast = parse(`
function test(a, b) {
  const c = a + b
}
`)

visit(ast, {
  visitExpression(path) {
    console.log(path.node.type)
    this.traverse(path)
  },
})
@jedwards1211 jedwards1211 changed the title [Proposal] we should provide a function to make a fork from @babel/types [Proposal] we should provide a function to build a fork with defs extracted from @babel/types Aug 29, 2022
@benjamn
Copy link
Owner

benjamn commented Dec 14, 2022

@jedwards1211 I think this is a great idea, and thanks for sharing concrete code!

I may be able to get this copy/pasted/tested soon, but I would also be happy to take a PR if you have the time.

@jedwards1211
Copy link
Contributor Author

@benjamn okay great! I think I should check first with the Babel maintainers if the way I'm inspecting the validate field is intended to be public API. If not I'll ask them to commit to making that metadata public API in some other way.

@jedwards1211
Copy link
Contributor Author

babel/babel#15276

@jedwards1211
Copy link
Contributor Author

Just FYI, https://github.com/codemodsquad/astx/blob/main/src/babel/babelAstTypes.ts should be considered the most up-to-date implementation of this approach, not the code example in OP

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants