All files index.js

96% Statements 48/50
91.49% Branches 43/47
100% Functions 11/11
95.83% Lines 46/48
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 1611x 1x 1x           1x 21x             52x       52x       19x                 10x 3x     7x 2x     9x 5x 3x     2x       10x       10x 10x                   10x 10x   4x         16x 16x 16x 1x       4x                     1x     15x       11x 11x           11x             11x 11x     10x                           1x       21x 7x 1x   7x       31x     3x 2x   3x 3x   3x                  
module.exports = function (babel) {
  const { types: t } = babel
  const logCallee = t.memberExpression(
    t.identifier('console'),
    t.identifier('log'),
    false
  )
 
  const createGroupCallee = (end, collapsed = true) =>
    t.memberExpression(
      t.identifier('console'),
      t.identifier(`group${end ? 'End' : collapsed ? 'Collapsed' : ''}`),
      false
    )
 
  function getComments (node) {
    return (node && node.leadingComments) || []
  }
 
  function hasSitrepComments (comments, label = 'sitrep') {
    return comments.some(c => c.value.trim() === label)
  }
 
  function createLogStatement (thing) {
    return t.callExpression(
      logCallee,
      thing.name && thing.name.includes('returnValue')
        ? [t.stringLiteral('Return Value:'), thing]
        : thing.name ? [t.stringLiteral(`${thing.name}: `), thing] : [thing]
    )
  }
 
  function getName (path) {
    if (path.node.id && path.node.id.name) {
      return path.node.id.name
    }
 
    if (path.node.key && t.isIdentifier(path.node.key)) {
      return path.node.key.name
    }
 
    const variableParent = path.findParent(p => p.isVariableDeclarator())
    if (variableParent && t.isIdentifier(variableParent.node.id)) {
      return variableParent.node.id.name
    }
 
    return `function: ${path.getSource().split('\n')[0]}`
  }
 
  function functionVisitor (path, state) {
    Iif (path.isArrowFunctionExpression()) {
      return
    }
 
    let name = getName(path)
    path
      .get('body')
      .unshiftContainer(
        'body',
        t.expressionStatement(
          t.callExpression(createGroupCallee(false, state.opts.collapsed), [
            t.stringLiteral(name)
          ])
        )
      )
    let didWriteGroupEnd = false
    path.traverse({
      AssignmentExpression (path) {
        path.insertAfter(
          t.expressionStatement(createLogStatement(path.node.left))
        )
      },
      VariableDeclaration (path) {
        const decls = path.node.declarations
        decls.forEach(dec => {
          if (t.isPattern(dec.id)) {
            dec.id.properties
              .slice()
              .reverse()
              .forEach(prop => {
                path.insertAfter(
                  t.expressionStatement(
                    t.callExpression(logCallee, [
                      t.isIdentifier(prop.value)
                        ? t.stringLiteral(prop.value.name)
                        : t.stringLiteral(prop.key.name),
                      t.isIdentifier(prop.value) ? prop.value : prop.key
                    ])
                  )
                )
              })
            return
          }
 
          path.insertAfter(t.expressionStatement(createLogStatement(dec.id)))
        })
      },
      ReturnStatement (path) {
        const id = path.scope.generateUidIdentifier('returnValue')
        path.insertBefore(
          t.variableDeclaration('var', [
            t.variableDeclarator(id, path.node.argument)
          ])
        )
 
        path.insertBefore(
          t.expressionStatement(
            t.callExpression(createGroupCallee(true, state.opts.collapsed), [
              t.stringLiteral(name)
            ])
          )
        )
        didWriteGroupEnd = true
        path.node.argument = id
      }
    })
    Iif (!didWriteGroupEnd) {
      path
        .get('body')
        .pushContainer(
          'body',
          t.expressionStatement(
            t.callExpression(createGroupCallee(true, state.opts.collapsed), [
              t.stringLiteral(name)
            ])
          )
        )
    }
  }
 
  return {
    name: 'babel-plugin-sitrep',
    visitor: {
      Function (path, state) {
        if (hasSitrepComments(getComments(path.node), state.opts.label)) {
          if (path.isArrowFunctionExpression()) {
            path.arrowFunctionToShadowed()
          }
          functionVisitor(path, state)
        }
      },
      VariableDeclarator (path, state) {
        if (
          hasSitrepComments(getComments(path.parentPath.node), state.opts.label)
        ) {
          if (t.isArrowFunctionExpression(path.node.init)) {
            path.get('init').arrowFunctionToShadowed()
          }
          Eif (t.isFunction(path.node.init)) {
            path.traverse({
              Function (path) {
                functionVisitor(path, state)
              }
            })
          }
        }
      }
    }
  }
}