All files index.js

96.67% Statements 29/30
88% Branches 22/25
100% Functions 7/7
96.55% Lines 28/29
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 911x 1x 1x             35x       35x       11x               1x   1x         11x 11x 11x 1x 4x                     1x     10x       6x 6x         6x       1x       11x 11x       11x 3x       24x 3x 2x   3x 3x              
module.exports = function(babel) {
  const { types: t } = babel
  const logCallee = t.memberExpression(
    t.identifier('console'),
    t.identifier('log'),
    false
  )
 
  function getComments(node) {
    return (node && node.leadingComments) || []
  }
 
  function hasSitrepComments(comments) {
    return comments.some(c => c.value.trim() === 'sitrep')
  }
 
  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]
    )
  }
 
  const dive = {
    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.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.node.argument = id
    }
  }
 
  return {
    name: 'babel-plugin-sitrep', // not required
    visitor: {
      BlockStatement(path) {
        let p = path.getFunctionParent()
        Iif (!p) {
          return
        }
 
        if (hasSitrepComments(getComments(p.node))) {
          path.traverse(dive)
        }
      },
      VariableDeclarator(path) {
        if (hasSitrepComments(getComments(path.parentPath.node))) {
          if (t.isArrowFunctionExpression(path.node.init)) {
            path.get('init').arrowFunctionToShadowed()
          }
          Eif (t.isFunction(path.node.init)) {
            path.traverse(dive)
          }
        }
      }
    }
  }
}