tunnckoCore/parse-function

View on GitHub
src/lib/plugins/initial.js

Summary

Maintainability
A
0 mins
Test Coverage
/*!
 * parse-function <https://github.com/tunnckoCore/parse-function>
 *
 * Copyright (c) 2017 Charlike Mike Reagent <open.source.charlike@gmail.com> (https://i.am.charlike.online)
 * Released under the MIT license.
 */

/* eslint-disable jsdoc/require-param-description, jsdoc/check-param-names */

import body from './body.js'
import props from './props.js'
import params from './params.js'

/**
 * > Default plugin that handles regular functions,
 * arrow functions, generator functions
 * and ES6 object method notation.
 *
 * @param  {Object} node
 * @param  {Object} result
 * @return {Object} resul
 * @private
 */
export default (app) => (node, result) => {
  const isFn = node.type.endsWith('FunctionExpression')
  const isMethod = node.type === 'ObjectExpression'

  /* istanbul ignore next */
  if (!isFn && !isMethod) {
    return
  }

  node = isMethod ? node.properties[0] : node
  node.id = isMethod ? node.key : node.id

  // babylon node.properties[0] is `ObjectMethod` that has `params` and `body`;
  // acorn node.properties[0] is `Property` that has `value`;
  if (node.type === 'Property') {
    const id = node.key
    node = node.value
    node.id = id
  }

  result = props(app)(node, result)
  result = params(app)(node, result)
  result = body(app)(node, result)

  return result
}