All files index.js

100% Statements 28/28
91.3% Branches 21/23
100% Functions 7/7
100% Lines 27/27
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 931x 1x 1x             40x       40x       11x               1x   1x         11x 11x 11x 1x       4x                     1x     10x       6x 6x         6x       1x       14x 3x   3x           26x 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
            .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.node.argument = id
    }
  }
 
  return {
    name: 'babel-plugin-sitrep',
    visitor: {
      Function (path) {
        if (hasSitrepComments(getComments(path.node))) {
          path.traverse({
            BlockStatement (blockStatementPath) {
              blockStatementPath.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)
          }
        }
      }
    }
  }
}