<commit_before>import React from 'react'
import addons from '@kadira/storybook-addons'
import reactElementToJSXString from 'react-element-to-jsx-string'

export default {
  addWithJSX(kind, story, opts = {}) {
    const defaultOpts = {
      skip: 0,
      showFunctions: true,
    }
    const channel = addons.getChannel()
    const result = this.add(kind, story)
    const options = Object.assign({}, defaultOpts, opts)
    let code = story()

    for (let i = 0; i < options.skip; i++) {
      if (typeof code === 'undefined') {
        console.warn('Cannot skip undefined element')
        return
      }

      if (React.Children.count(code) > 1) {
        console.warn('Trying to skip an array of elements')
        return
      }

      if (typeof code.props.children === 'undefined') {
        console.warn('Not enough children to skip elements.')

        if (typeof code.type === 'function' && code.type.name === '') code = code.type(code.props)
      } else {
        if (typeof code.props.children === 'function') {
          code = code.props.children()
        } else {
          code = code.props.children
        }
      }
    }

    if (typeof code === 'undefined') return console.warn('Too many skip or undefined component')

    while (typeof code.type === 'function' && code.type.name === '')
      code = code.type(code.props)

    const ooo = typeof options.displayName === 'string'
      ? Object.assign({}, options, { displayName: () => options.displayName })
      : options

    const compiledCode = React.Children.map(code, c => reactElementToJSXString(c, ooo)).join('\n')

    channel.emit('kadira/jsx/add_jsx', result.kind, kind, compiledCode)
    return result
  },
}<commit_msg>Handles context to render after decorator are applied<commit_after>import React from 'react'
import addons from '@kadira/storybook-addons'
import reactElementToJSXString from 'react-element-to-jsx-string'

const renderJsx = (code, options) => {
  for (let i = 0; i < options.skip; i++) {
    if (typeof code === 'undefined') {
      console.warn('Cannot skip undefined element')
      return
    }

    if (React.Children.count(code) > 1) {
      console.warn('Trying to skip an array of elements')
      return
    }

    if (typeof code.props.children === 'undefined') {
      console.warn('Not enough children to skip elements.')

      if (typeof code.type === 'function' && code.type.name === '') code = code.type(code.props)
    } else {
      if (typeof code.props.children === 'function') {
        code = code.props.children()
      } else {
        code = code.props.children
      }
    }
  }

  if (typeof code === 'undefined') return console.warn('Too many skip or undefined component')

  while (typeof code.type === 'function' && code.type.name === '')
    code = code.type(code.props)

  const ooo = typeof options.displayName === 'string'
    ? Object.assign({}, options, { displayName: () => options.displayName })
    : options

  return React.Children.map(code, c => reactElementToJSXString(c, ooo)).join('\n')
}

export default {
  addWithJSX(kind, storyFn, opts = {}) {
    const defaultOpts = {
      skip: 0,
      showFunctions: true,
    }
    const options = Object.assign({}, defaultOpts, opts)
    const channel = addons.getChannel()

    const result = this.add(kind, context => {
      const story = storyFn(context)

      const jsx = renderJsx(story, options)
      channel.emit('kadira/jsx/add_jsx', result.kind, kind, jsx)

      return story
    })

    return result
  },
}
{'fraction_non_alphanumeric': 0.14180164819551008, 'fraction_numerical': 0.0017050298380221654, 'mean_word_length': 2.797195253505933, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6761157', 'n_tokens_mistral': 1069, 'n_tokens_neox': 1051, 'n_words': 280}