lib/unwrap.js
function unwrapAst (ast, prefix, suffix) {
  let fake_code = `${prefix}\nlet ${LONG_AND_WEIRED_STRING}\n${suffix}`
  let fake_ast = babylon.parse(fake_code, {
    sourceType: 'module'
  })

  let selected_node
  let selected_index
  traverse(fake_ast, {
    enter: function (node, parent) {
      // removes the loc info of fake ast
      if (node.loc) {
        node.loc = null
      }

      if (node.type !== 'BlockStatement') {
        return
      }

      // find the index of LONG_AND_WEIRED_STRING
      let index = node.body.findIndex(n => {
        if (node.type !== 'VariableDeclaration') {
          return
        }

        if (
          access(n, [
            'declarations', 0,
            'id',
            'name'
          ]) === LONG_AND_WEIRED_STRING
        ) {
          return true
        }
      })

      if (!~index) {
        return
      }

      selected_node = node
      selected_index = index
    }
  })

  if (selected_node) {
    selected_node.body.splice(selected_index, 1)
    selected_node.body.push(...ast.program.body)
  }

  return fake_ast
}
function unwrapAst (ast, prefix, suffix) {
  let fake_code = `${prefix}\nlet ${LONG_AND_WEIRED_STRING}\n${suffix}`
  let fake_ast = babylon.parse(fake_code, {
    sourceType: 'module'
  })

  let selected_node
  let selected_index
  traverse(fake_ast, {
    enter: function (node, parent) {
      // removes the loc info of fake ast
      if (node.loc) {
        node.loc = null
      }

      if (node.type !== 'BlockStatement') {
        return
      }

      // find the index of LONG_AND_WEIRED_STRING
      let index = node.body.findIndex(n => {
        if (node.type !== 'VariableDeclaration') {
          return
        }

        if (
          access(n, [
            'declarations', 0,
            'id',
            'name'
          ]) === LONG_AND_WEIRED_STRING
        ) {
          return true
        }
      })

      if (!~index) {
        return
      }

      selected_node = node
      selected_index = index
    }
  })

  if (selected_node) {
    selected_node.body.splice(selected_index, 1)
    selected_node.body.push(...ast.program.body)
  }

  return fake_ast
}
function unwrapAst ( ast , prefix , suffix ) { let fake_code = ` ${ prefix } \n ${ LONG_AND_WEIRED_STRING } \n ${ suffix } ` let fake_ast = babylon . parse ( fake_code , { sourceType : 'module' } ) let selected_node let selected_index traverse ( fake_ast , { enter : function ( node , parent ) { // removes the loc info of fake ast if ( node . loc ) { node . loc = null } if ( node . type !== 'BlockStatement' ) { return } // find the index of LONG_AND_WEIRED_STRING let index = node . body . findIndex ( n => { if ( node . type !== 'VariableDeclaration' ) { return } if ( access ( n , [ 'declarations' , 0 , 'id' , 'name' ] ) === LONG_AND_WEIRED_STRING ) { return true } } ) if ( ! ~ index ) { return } selected_node = node selected_index = index } } ) if ( selected_node ) { selected_node . body . splice ( selected_index , 1 ) selected_node . body . push ( ... ast . program . body ) } return fake_ast }
Based on the situation that neuron wrappings are always functions with no statements within the bodies or statements at the beginning of function bodies
Based on the situation that neuron wrappings are always functions with no statements within the bodies or statements at the beginning of function bodies