How to use the babel-traverse.default.cheap function in babel-traverse

To help you get started, we’ve selected a few babel-traverse 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 capaj / vscode-exports-autocomplete / lib / get-token-at-position.js View on Github external
module.exports = (tree, position) => {
  const babylonPos = positionUtils.vsCodeToBabylon(position)
  let currentToken
  let currentTokenLength = tree.program.end
  try {
    traverse.cheap(tree.program, (node) => {
      const {start, end} = node.loc
      if (start.line === babylonPos.line && start.column < babylonPos.column && end.column >= babylonPos.column) {
        const nodeLength = node.end - node.start
        if (nodeLength < currentTokenLength) {
          currentToken = node
          currentTokenLength = nodeLength
        }
      }
    })
  } catch (err) {
    // current text in the editor might not be parseable, but this is not an error we'd need to throw outside of this function
  }
  return currentToken
}
github capaj / vscode-exports-autocomplete / lib / should-complete-identifiers.js View on Github external
module.exports = (tree, documentOffset) => {
  let currentNode
  let paramsNodes = []
  let attributeNamesNodes = []
  let propertyNodes = []
  try {
    traverse.cheap(tree.program, (node, parent) => {
      const { start, end, params, attributes, property, properties } = node
      if (start <= documentOffset && end >= documentOffset) {
        if (params) {
          paramsNodes = paramsNodes.concat(params)
        }
        if (attributes) {
          attributeNamesNodes = attributeNamesNodes.concat(
            attributes.map(({ name }) => name)
          )
        }
        if (property) {
          propertyNodes.push(property)
        }
        if (properties) {
          propertyNodes = propertyNodes.concat(properties.map(({ key }) => key))
        }