├── .npmignore ├── src ├── __tests__ │ ├── .babelrc │ ├── index.js │ └── __snapshots__ │ │ └── index.js.snap └── index.js ├── .gitignore ├── .travis.yml ├── package.json └── README.md /.npmignore: -------------------------------------------------------------------------------- 1 | test 2 | *.log 3 | -------------------------------------------------------------------------------- /src/__tests__/.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "plugins": ["transform-class-properties"] 3 | } 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | .DS_Store 3 | /node_modules 4 | /packages/*/node_modules 5 | *.log 6 | *.cache 7 | /.eslintcache 8 | /browser.js 9 | /browser-polyfill.js 10 | /runtime.js 11 | /coverage 12 | dist 13 | /.package.json 14 | package-lock.json 15 | yarn.lock 16 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | 3 | node_js: 4 | - "8" 5 | 6 | cache: 7 | directories: 8 | - .npm 9 | 10 | before_install: 11 | - npm install codecov 12 | 13 | after_success: 14 | - cat ./coverage/lcov.info | ./node_modules/codecov/bin/codecov 15 | 16 | cache: 17 | directories: 18 | - .npm 19 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "babel-plugin-sitrep", 3 | "version": "1.2.3", 4 | "description": "Log all assignments and the return value of a function with a simple comment", 5 | "main": "src/index.js", 6 | "scripts": { 7 | "lint": "standard src", 8 | "lint:fix": "standard --fix src", 9 | "test": "jest --coverage --no-cache --ci --runInBand", 10 | "test:watch": "jest --watch --no-cache" 11 | }, 12 | "author": "Kye Hohenberger", 13 | "license": "MIT", 14 | "eslintConfig": { 15 | "extends": "standard", 16 | "parser": "babel-eslint" 17 | }, 18 | "standard": { 19 | "parser": "babel-eslint" 20 | }, 21 | "devDependencies": { 22 | "babel-eslint": "^7.2.3", 23 | "babel-plugin-tester": "^4.0.0", 24 | "babel-plugin-transform-class-properties": "^6.24.1", 25 | "jest": "^20.0.4", 26 | "jest-cli": "^20.0.4", 27 | "prettier": "^1.6.1", 28 | "standard": "^10.0.3" 29 | }, 30 | "bugs": { 31 | "url": "https://github.com/tkh44/babel-plugin-sitrep/issues" 32 | }, 33 | "repository": { 34 | "type": "git", 35 | "url": "git+https://github.com/tkh44/babel-plugin-sitrep.git" 36 | }, 37 | "keywords": [ 38 | "console", 39 | "console.log", 40 | "babel", 41 | "plugin", 42 | "babel-plugin-sitrep", 43 | "babel log", 44 | "logging", 45 | "logger", 46 | "log" 47 | ] 48 | } 49 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # babel-plugin-sitrep 2 | 3 | > Log all assignments and the return value of a function with a simple comment 4 | 5 | [![npm version](https://badge.fury.io/js/babel-plugin-sitrep.svg)](https://badge.fury.io/js/babel-plugin-sitrep) 6 | [![Build Status](https://travis-ci.org/tkh44/babel-plugin-sitrep.svg?branch=master)](https://travis-ci.org/tkh44/babel-plugin-sitrep) 7 | [![codecov](https://codecov.io/gh/tkh44/babel-plugin-sitrep/branch/master/graph/badge.svg)](https://codecov.io/gh/tkh44/babel-plugin-sitrep) 8 | 9 | ## Example 10 | 11 | **In** 12 | 13 | ```javascript 14 | // sitrep 15 | function bar () { 16 | var a = 'foo' 17 | const b = 'bar' 18 | let c = [a, b].map(x => x) 19 | return c.join('-') 20 | } 21 | 22 | // sitrep 23 | var cb = x => x.charAt(0) 24 | 25 | // sitrep 26 | var cb = x => { 27 | x = x + 2 28 | x.charAt(0) 29 | return x 30 | } 31 | 32 | // sitrep 33 | var a = function () { 34 | return 'foo' 35 | } 36 | 37 | const obj = { 38 | // sitrep 39 | fn() { 40 | let a = 5 41 | return a + 5 42 | } 43 | } 44 | 45 | class Boom { 46 | // sitrep 47 | fire() { 48 | let a = 2 49 | return a + 5 50 | } 51 | } 52 | 53 | // sitrep prefix 54 | function bar () { 55 | var a = 'foo' 56 | return a 57 | } 58 | 59 | ``` 60 | 61 | `↓ ↓ ↓ ↓ ↓ ↓` 62 | 63 | **Out** 64 | 65 | ```javascript 66 | // sitrep 67 | function bar () { 68 | console.groupCollapsed('bar'); 69 | 70 | var a = 'foo'; 71 | console.log('a: ', a); 72 | const b = 'bar'; 73 | console.log('b: ', b); 74 | let c = [a, b].map(x => x); 75 | console.log('c: ', c); 76 | 77 | var _returnValue = c.join('-'); 78 | 79 | console.log('Return Value:', _returnValue); 80 | console.groupEnd('bar'); 81 | return _returnValue; 82 | } 83 | 84 | // sitrep 85 | var cb = function (x) { 86 | console.groupCollapsed('cb'); 87 | 88 | var _returnValue3 = x.charAt(0); 89 | 90 | console.log('Return Value:', _returnValue3); 91 | console.groupEnd('cb'); 92 | return _returnValue3; 93 | }; 94 | 95 | // sitrep 96 | var cb = function (x) { 97 | console.groupCollapsed('cb'); 98 | 99 | x = x + 2; 100 | console.log('x: ', x); 101 | x.charAt(0); 102 | var _returnValue4 = x; 103 | console.log('Return Value:', _returnValue4); 104 | console.groupEnd('cb'); 105 | return _returnValue4; 106 | }; 107 | 108 | // sitrep 109 | var a = function () { 110 | console.groupCollapsed('a'); 111 | var _returnValue5 = 'foo'; 112 | console.log('Return Value:', _returnValue5); 113 | console.groupEnd('a'); 114 | 115 | return _returnValue5; 116 | }; 117 | 118 | const obj = { 119 | // sitrep 120 | fn() { 121 | console.groupCollapsed('fn'); 122 | 123 | let a = 5; 124 | console.log('a: ', a); 125 | 126 | var _returnValue6 = a + 5; 127 | 128 | console.log('Return Value:', _returnValue6); 129 | console.groupEnd('fn'); 130 | return _returnValue6; 131 | } 132 | }; 133 | 134 | class Boom { 135 | // sitrep 136 | fire() { 137 | console.groupCollapsed('fire'); 138 | 139 | let a = 2; 140 | console.log('a: ', a); 141 | 142 | var _returnValue7 = a + 5; 143 | 144 | console.log('Return Value:', _returnValue7); 145 | console.groupEnd('fire'); 146 | return _returnValue7; 147 | } 148 | } 149 | 150 | // sitrep prefix 151 | function bar () { 152 | console.groupCollapsed('(prefix) bar'); 153 | 154 | var a = 'foo'; 155 | console.log('a: ', a); 156 | 157 | var _returnValue8 = c.join('-'); 158 | 159 | console.log('Return Value:', _returnValue8); 160 | console.groupEnd('(prefix) bar'); 161 | return _returnValue8; 162 | } 163 | ``` 164 | 165 | ## Installation 166 | 167 | ```sh 168 | npm install --save-dev babel-plugin-sitrep 169 | ``` 170 | 171 | ## Usage 172 | 173 | ### Via `.babelrc` (Recommended) 174 | 175 | **.babelrc** 176 | 177 | Without options: 178 | 179 | ```json 180 | { 181 | "plugins": ["sitrep"] 182 | } 183 | ``` 184 | 185 | ### Via CLI 186 | 187 | ```sh 188 | babel --plugins sitrep script.js 189 | ``` 190 | 191 | ### Via Node API 192 | 193 | ```javascript 194 | require("babel-core").transform("code", { 195 | plugins: ["sitrep"] 196 | }); 197 | ``` 198 | 199 | 200 | ## Options 201 | 202 | ### `label` 203 | 204 | `string`, defaults to `sitrep`. 205 | 206 | This option changes the label that enables the plugin 207 | 208 | **Example** 209 | 210 | If we set `label` to `"log-all-the-things"` 211 | 212 | **In** 213 | ```javascript 214 | // log-all-the-things 215 | function fn(a) { 216 | a = a.map(x => x) 217 | return a 218 | } 219 | ``` 220 | 221 | ```↓ ↓ ↓ ↓ ↓ ↓``` 222 | 223 | **Out** 224 | ```javascript 225 | // log-all-the-things 226 | function fn(a) { 227 | console.groupCollapsed("fn"); 228 | 229 | a = a.map(x => x); 230 | console.log("a: ", a); 231 | var _returnValue = a; 232 | console.log("Return Value:", _returnValue); 233 | console.groupEnd("fn"); 234 | return _returnValue; 235 | } 236 | ``` 237 | 238 | ### `collapsed` 239 | 240 | `boolean`, defaults to `true`. 241 | 242 | This option enables the following: 243 | 244 | - Collapse the group of console logs associated with a function 245 | -------------------------------------------------------------------------------- /src/__tests__/index.js: -------------------------------------------------------------------------------- 1 | const pluginTester = require('babel-plugin-tester') 2 | const plugin = require('../index') 3 | 4 | pluginTester({ 5 | plugin: plugin, 6 | pluginName: 'sitrep', 7 | snapshot: true, 8 | babelOptions: { 9 | babelrc: true, 10 | filename: __filename 11 | }, 12 | tests: [ 13 | { 14 | title: 'function', 15 | code: ` 16 | // sitrep 17 | function bar () { 18 | var a = 'foo' 19 | const b = 'bar' 20 | let c = [a, b].map(x => x) 21 | let d = a 22 | return c.join('-') 23 | } 24 | ` 25 | }, 26 | { 27 | title: 'arrow function expression (shorthand arrow fn)', 28 | code: ` 29 | // sitrep 30 | var cb = x => x.charAt(0) 31 | ` 32 | }, 33 | { 34 | title: 'arrow function assignment', 35 | code: ` 36 | // sitrep 37 | var cb = x => { 38 | x = x + 2 39 | x.charAt(0) 40 | return x 41 | } 42 | ` 43 | }, 44 | { 45 | title: 'function assignment', 46 | code: ` 47 | // sitrep 48 | var a = function () { 49 | return 'foo' 50 | } 51 | ` 52 | }, 53 | { 54 | title: 'object properties', 55 | code: ` 56 | const obj = { 57 | // sitrep 58 | fn() { 59 | const { a, b, c = 'foo', d: alias } = x; 60 | return a + b + c + alias; 61 | } 62 | } 63 | ` 64 | }, 65 | { 66 | title: 'object properties with let', 67 | code: ` 68 | const obj = { 69 | // sitrep 70 | fn(x) { 71 | let a, b, c, d; 72 | ({ a, b, c = 'foo', d: alias } = x); 73 | return a + b + c + alias; 74 | } 75 | } 76 | ` 77 | }, 78 | { 79 | title: 'array properties', 80 | code: ` 81 | // sitrep 82 | function fn() { 83 | const [a, b = 'foo', c, d] = x.split("_"); 84 | return a + b + c + d; 85 | } 86 | ` 87 | }, 88 | { 89 | title: 'array properties with let', 90 | code: ` 91 | // sitrep 92 | function fn() { 93 | let a, b, c, d; 94 | [a, b = 'foo', c, d] = x.split("_"); 95 | return a + b + c + d; 96 | } 97 | ` 98 | }, 99 | { 100 | title: 'class methods', 101 | code: ` 102 | class Boom { 103 | // sitrep 104 | fire() { 105 | let a = 2 106 | 107 | return a + 5 108 | } 109 | } 110 | ` 111 | }, 112 | { 113 | title: 'no function parent', 114 | code: ` 115 | // sitrep 116 | if (a) { 117 | let a = 2 118 | 119 | let b = a + 5 120 | } 121 | ` 122 | }, 123 | { 124 | title: 'arrow fn expression callback', 125 | code: ` 126 | myFn( 127 | // sitrep 128 | (x) => x 129 | ) 130 | ` 131 | }, 132 | { 133 | title: 'function declaration with multiple returns', 134 | code: ` 135 | myFn( 136 | // sitrep 137 | function(err, vars) { 138 | if (err) { 139 | return err 140 | } 141 | 142 | vars = vars.map(x => x) 143 | return vars 144 | } 145 | ) 146 | ` 147 | }, 148 | { 149 | title: 'collapsed option', 150 | code: ` 151 | // sitrep 152 | function fn(a) { 153 | a = a.map(x => x) 154 | return a 155 | } 156 | `, 157 | pluginOptions: { 158 | collapsed: false 159 | } 160 | }, 161 | { 162 | title: 'custom label', 163 | code: ` 164 | // 🔬 165 | function fn(a) { 166 | a = a.map(x => x) 167 | return a 168 | } 169 | `, 170 | pluginOptions: { 171 | label: '🔬' 172 | } 173 | }, 174 | { 175 | title: 'function has a for loop', 176 | code: ` 177 | // sitrep 178 | function test() { 179 | var sum = 0; 180 | for (var i = 0; i < 5; i++) { 181 | sum = sum + i; 182 | } 183 | return sum; 184 | } 185 | ` 186 | }, 187 | { 188 | title: 'function has a for-of loop', 189 | code: ` 190 | // sitrep 191 | function test(arr) { 192 | var sum = 0; 193 | for (const value of arr) { 194 | sum += value; 195 | } 196 | } 197 | ` 198 | }, 199 | { 200 | title: 'class property', 201 | code: ` 202 | class Foo { 203 | // sitrep 204 | handleClick = (e) =>{ 205 | const foo = e.target.innerText; 206 | // some actions based on foo 207 | e.preventDefault() 208 | } 209 | } 210 | ` 211 | }, 212 | { 213 | title: 'should not add console logs', 214 | code: ` 215 | // sitrep 216 | const shouldNotLog = 'bar'; 217 | 218 | function bar () { 219 | var a = 'foo' 220 | const b = 'bar' 221 | let c = [a, b].map(x => x) 222 | return c.join('-') 223 | } 224 | 225 | var cb = x => x.charAt(0) 226 | 227 | var cb = x => { 228 | x = x + 2 229 | x.charAt(0) 230 | return x 231 | } 232 | 233 | var a = function () { 234 | return 'foo' 235 | } 236 | 237 | const obj = { 238 | fn() { 239 | let a = 5 240 | return a + 5 241 | } 242 | } 243 | 244 | class Boom { 245 | // sitrep 246 | shouldNotLog = 'bar'; 247 | 248 | fire() { 249 | let a = 2 250 | 251 | return a + 5 252 | } 253 | 254 | handleClick = (e) => e.preventDefault() 255 | } 256 | ` 257 | }, 258 | { 259 | title: 'optional log prefix', 260 | code: ` 261 | // sitrep prefix 262 | function bar () { 263 | var a = 'foo' 264 | const b = 'bar' 265 | let c = [a, b].map(x => x) 266 | let d = a 267 | return c.join('-') 268 | } 269 | ` 270 | } 271 | ] 272 | }) 273 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | const defaultLabel = 'sitrep' 2 | 3 | module.exports = function (babel) { 4 | const { types: t } = babel 5 | const logCallee = t.memberExpression( 6 | t.identifier('console'), 7 | t.identifier('log'), 8 | false 9 | ) 10 | 11 | const createGroupCallee = (end, collapsed = true) => 12 | t.memberExpression( 13 | t.identifier('console'), 14 | t.identifier(`group${end ? 'End' : collapsed ? 'Collapsed' : ''}`), 15 | false 16 | ) 17 | 18 | function getComments (node) { 19 | return (node && node.leadingComments) || [] 20 | } 21 | 22 | function hasSitrepComments (comments, label = defaultLabel) { 23 | return comments.some(c => c.value.trim().indexOf(label) === 0) 24 | } 25 | 26 | function getSitrepCommentPrefix (comments, label = defaultLabel) { 27 | for (let i = 0; i < comments.length; i += 1) { 28 | const comment = comments[i].value.trim() 29 | if (comment.indexOf(label) === 0 && comment.length > label.length) { 30 | return comment.substr(label.length).trim() 31 | } 32 | } 33 | return undefined 34 | } 35 | 36 | function createLogStatement (thing) { 37 | return t.callExpression( 38 | logCallee, 39 | thing.name && thing.name.indexOf('returnValue') > -1 40 | ? [t.stringLiteral('Return Value:'), thing] 41 | : thing.name ? [t.stringLiteral(`${thing.name}: `), thing] : [thing] 42 | ) 43 | } 44 | 45 | function getName (path) { 46 | if (path.parentPath.isClassProperty()) { 47 | if ( 48 | path.parentPath.node.key && 49 | t.isIdentifier(path.parentPath.node.key) 50 | ) { 51 | return path.parentPath.node.key.name 52 | } 53 | } 54 | 55 | if (path.node.id && path.node.id.name) { 56 | return path.node.id.name 57 | } 58 | 59 | if (path.node.key && t.isIdentifier(path.node.key)) { 60 | return path.node.key.name 61 | } 62 | 63 | const variableParent = path.findParent(p => p.isVariableDeclarator()) 64 | if (variableParent && t.isIdentifier(variableParent.node.id)) { 65 | return variableParent.node.id.name 66 | } 67 | 68 | return `function: ${path.getSource().split('\n')[0]}` 69 | } 70 | 71 | function functionVisitor (functionPath, state, prefix) { 72 | let name = getName(functionPath) 73 | if (prefix) name = `(${prefix}) ${name}` 74 | functionPath 75 | .get('body') 76 | .unshiftContainer( 77 | 'body', 78 | t.expressionStatement( 79 | t.callExpression(createGroupCallee(false, state.opts.collapsed), [ 80 | t.stringLiteral(name) 81 | ]) 82 | ) 83 | ) 84 | let didWriteGroupEnd = false 85 | 86 | functionPath.traverse({ 87 | AssignmentExpression (path) { 88 | if (t.isPattern(path.node.left)) { 89 | return 90 | } 91 | path.insertAfter( 92 | t.expressionStatement(createLogStatement(path.node.left)) 93 | ) 94 | }, 95 | ArrayPattern (arrPatternPath) { 96 | arrPatternPath.node.elements 97 | .slice() 98 | .reverse() 99 | .forEach(element => { 100 | let id = element 101 | if (t.isAssignmentPattern(element)) { 102 | id = element.left 103 | } 104 | arrPatternPath 105 | .getStatementParent() 106 | .insertAfter( 107 | t.expressionStatement( 108 | t.callExpression(logCallee, [t.stringLiteral(id.name), id]) 109 | ) 110 | ) 111 | }) 112 | }, 113 | ObjectPattern (objPatternPath) { 114 | objPatternPath.node.properties 115 | .slice() 116 | .reverse() 117 | .forEach(prop => { 118 | objPatternPath 119 | .getStatementParent() 120 | .insertAfter( 121 | t.expressionStatement( 122 | t.callExpression(logCallee, [ 123 | t.isIdentifier(prop.value) 124 | ? t.stringLiteral(prop.value.name) 125 | : t.stringLiteral(prop.key.name), 126 | t.isIdentifier(prop.value) ? prop.value : prop.key 127 | ]) 128 | ) 129 | ) 130 | }) 131 | }, 132 | VariableDeclaration (variableDeclPath) { 133 | if (!variableDeclPath.parentPath.isBlockStatement()) { 134 | return 135 | } 136 | 137 | const decls = variableDeclPath.node.declarations 138 | decls.forEach(dec => { 139 | if (!dec.init) { 140 | return 141 | } 142 | 143 | if (t.isPattern(dec.id)) { 144 | return 145 | } 146 | 147 | variableDeclPath.insertAfter( 148 | t.expressionStatement(createLogStatement(dec.id)) 149 | ) 150 | }) 151 | }, 152 | ReturnStatement (returnPath) { 153 | const id = returnPath.scope.generateUidIdentifier('returnValue') 154 | returnPath.insertBefore( 155 | t.variableDeclaration('var', [ 156 | t.variableDeclarator(id, returnPath.node.argument) 157 | ]) 158 | ) 159 | 160 | returnPath.insertBefore( 161 | t.expressionStatement( 162 | t.callExpression(createGroupCallee(true, state.opts.collapsed), [ 163 | t.stringLiteral(name) 164 | ]) 165 | ) 166 | ) 167 | didWriteGroupEnd = true 168 | returnPath.node.argument = id 169 | } 170 | }) 171 | if (!didWriteGroupEnd) { 172 | functionPath 173 | .get('body') 174 | .pushContainer( 175 | 'body', 176 | t.expressionStatement( 177 | t.callExpression(createGroupCallee(true, state.opts.collapsed), [ 178 | t.stringLiteral(name) 179 | ]) 180 | ) 181 | ) 182 | } 183 | } 184 | 185 | return { 186 | name: 'babel-plugin-sitrep', 187 | visitor: { 188 | Class (path, state) { 189 | path.traverse({ 190 | ClassProperty (path) { 191 | const comments = getComments(path.node) 192 | if (hasSitrepComments(comments, state.opts.label)) { 193 | const prefix = getSitrepCommentPrefix(comments, state.opts.label) 194 | path.traverse({ 195 | Function (path) { 196 | functionVisitor(path, state, prefix) 197 | } 198 | }) 199 | } 200 | } 201 | }) 202 | }, 203 | Function (path, state) { 204 | const comments = getComments(path.node) 205 | if (hasSitrepComments(comments, state.opts.label)) { 206 | const prefix = getSitrepCommentPrefix(comments, state.opts.label) 207 | if (path.isArrowFunctionExpression()) { 208 | path.arrowFunctionToShadowed() 209 | } 210 | functionVisitor(path, state, prefix) 211 | } 212 | }, 213 | VariableDeclarator (path, state) { 214 | const comments = getComments(path.parentPath.node) 215 | if ( 216 | hasSitrepComments(comments, state.opts.label) 217 | ) { 218 | const prefix = getSitrepCommentPrefix(comments, state.opts.label) 219 | if (t.isArrowFunctionExpression(path.node.init)) { 220 | path.get('init').arrowFunctionToShadowed() 221 | } 222 | if (t.isFunction(path.node.init)) { 223 | path.traverse({ 224 | Function (path) { 225 | functionVisitor(path, state, prefix) 226 | } 227 | }) 228 | } 229 | } 230 | } 231 | } 232 | } 233 | } 234 | -------------------------------------------------------------------------------- /src/__tests__/__snapshots__/index.js.snap: -------------------------------------------------------------------------------- 1 | // Jest Snapshot v1, https://goo.gl/fbAQLP 2 | 3 | exports[`array properties 1`] = ` 4 | " 5 | // sitrep 6 | function fn() { 7 | const [a, b = 'foo', c, d] = x.split(\\"_\\"); 8 | return a + b + c + d; 9 | } 10 | 11 | ↓ ↓ ↓ ↓ ↓ ↓ 12 | 13 | // sitrep 14 | function fn() { 15 | console.groupCollapsed(\\"fn\\"); 16 | 17 | const [a, b = 'foo', c, d] = x.split(\\"_\\"); 18 | console.log(\\"a\\", a); 19 | console.log(\\"b\\", b); 20 | console.log(\\"c\\", c); 21 | console.log(\\"d\\", d); 22 | 23 | var _returnValue = a + b + c + d; 24 | 25 | console.log(\\"Return Value:\\", _returnValue); 26 | console.groupEnd(\\"fn\\"); 27 | return _returnValue; 28 | } 29 | " 30 | `; 31 | 32 | exports[`array properties with let 1`] = ` 33 | " 34 | // sitrep 35 | function fn() { 36 | let a, b, c, d; 37 | [a, b = 'foo', c, d] = x.split(\\"_\\"); 38 | return a + b + c + d; 39 | } 40 | 41 | ↓ ↓ ↓ ↓ ↓ ↓ 42 | 43 | // sitrep 44 | function fn() { 45 | console.groupCollapsed(\\"fn\\"); 46 | 47 | let a, b, c, d; 48 | [a, b = 'foo', c, d] = x.split(\\"_\\"); 49 | console.log(\\"a\\", a); 50 | console.log(\\"b\\", b); 51 | console.log(\\"c\\", c); 52 | console.log(\\"d\\", d); 53 | 54 | var _returnValue = a + b + c + d; 55 | 56 | console.log(\\"Return Value:\\", _returnValue); 57 | console.groupEnd(\\"fn\\"); 58 | return _returnValue; 59 | } 60 | " 61 | `; 62 | 63 | exports[`arrow fn expression callback 1`] = ` 64 | " 65 | myFn( 66 | // sitrep 67 | (x) => x 68 | ) 69 | 70 | ↓ ↓ ↓ ↓ ↓ ↓ 71 | 72 | myFn( 73 | // sitrep 74 | function (x) { 75 | console.groupCollapsed(\\"function: (x) => x\\"); 76 | var _returnValue = x; 77 | console.log(\\"Return Value:\\", _returnValue); 78 | console.groupEnd(\\"function: (x) => x\\"); 79 | return _returnValue; 80 | }); 81 | " 82 | `; 83 | 84 | exports[`arrow function assignment 1`] = ` 85 | " 86 | // sitrep 87 | var cb = x => { 88 | x = x + 2 89 | x.charAt(0) 90 | return x 91 | } 92 | 93 | ↓ ↓ ↓ ↓ ↓ ↓ 94 | 95 | // sitrep 96 | var cb = function (x) { 97 | console.groupCollapsed(\\"cb\\"); 98 | 99 | x = x + 2; 100 | console.log(\\"x: \\", x); 101 | x.charAt(0); 102 | var _returnValue = x; 103 | console.log(\\"Return Value:\\", _returnValue); 104 | console.groupEnd(\\"cb\\"); 105 | return _returnValue; 106 | }; 107 | " 108 | `; 109 | 110 | exports[`arrow function expression (shorthand arrow fn) 1`] = ` 111 | " 112 | // sitrep 113 | var cb = x => x.charAt(0) 114 | 115 | ↓ ↓ ↓ ↓ ↓ ↓ 116 | 117 | // sitrep 118 | var cb = function (x) { 119 | console.groupCollapsed(\\"cb\\"); 120 | 121 | var _returnValue = x.charAt(0); 122 | 123 | console.log(\\"Return Value:\\", _returnValue); 124 | console.groupEnd(\\"cb\\"); 125 | return _returnValue; 126 | }; 127 | " 128 | `; 129 | 130 | exports[`class methods 1`] = ` 131 | " 132 | class Boom { 133 | // sitrep 134 | fire() { 135 | let a = 2 136 | 137 | return a + 5 138 | } 139 | } 140 | 141 | ↓ ↓ ↓ ↓ ↓ ↓ 142 | 143 | class Boom { 144 | // sitrep 145 | fire() { 146 | console.groupCollapsed(\\"fire\\"); 147 | 148 | let a = 2; 149 | 150 | console.log(\\"a: \\", a); 151 | 152 | var _returnValue = a + 5; 153 | 154 | console.log(\\"Return Value:\\", _returnValue); 155 | console.groupEnd(\\"fire\\"); 156 | return _returnValue; 157 | } 158 | } 159 | " 160 | `; 161 | 162 | exports[`class property 1`] = ` 163 | " 164 | class Foo { 165 | // sitrep 166 | handleClick = (e) =>{ 167 | const foo = e.target.innerText; 168 | // some actions based on foo 169 | e.preventDefault() 170 | } 171 | } 172 | 173 | ↓ ↓ ↓ ↓ ↓ ↓ 174 | 175 | class Foo { 176 | constructor() { 177 | this.handleClick = e => { 178 | console.groupCollapsed(\\"handleClick\\"); 179 | 180 | const foo = e.target.innerText; 181 | // some actions based on foo 182 | console.log(\\"foo: \\", foo); 183 | e.preventDefault(); 184 | console.groupEnd(\\"handleClick\\"); 185 | }; 186 | } 187 | // sitrep 188 | 189 | 190 | } 191 | " 192 | `; 193 | 194 | exports[`collapsed option 1`] = ` 195 | " 196 | // sitrep 197 | function fn(a) { 198 | a = a.map(x => x) 199 | return a 200 | } 201 | 202 | ↓ ↓ ↓ ↓ ↓ ↓ 203 | 204 | // sitrep 205 | function fn(a) { 206 | console.group(\\"fn\\"); 207 | 208 | a = a.map(x => x); 209 | console.log(\\"a: \\", a); 210 | var _returnValue = a; 211 | console.log(\\"Return Value:\\", _returnValue); 212 | console.groupEnd(\\"fn\\"); 213 | return _returnValue; 214 | } 215 | " 216 | `; 217 | 218 | exports[`custom label 1`] = ` 219 | " 220 | // 🔬 221 | function fn(a) { 222 | a = a.map(x => x) 223 | return a 224 | } 225 | 226 | ↓ ↓ ↓ ↓ ↓ ↓ 227 | 228 | // 🔬 229 | function fn(a) { 230 | console.groupCollapsed(\\"fn\\"); 231 | 232 | a = a.map(x => x); 233 | console.log(\\"a: \\", a); 234 | var _returnValue = a; 235 | console.log(\\"Return Value:\\", _returnValue); 236 | console.groupEnd(\\"fn\\"); 237 | return _returnValue; 238 | } 239 | " 240 | `; 241 | 242 | exports[`function 1`] = ` 243 | " 244 | // sitrep 245 | function bar () { 246 | var a = 'foo' 247 | const b = 'bar' 248 | let c = [a, b].map(x => x) 249 | let d = a 250 | return c.join('-') 251 | } 252 | 253 | ↓ ↓ ↓ ↓ ↓ ↓ 254 | 255 | // sitrep 256 | function bar() { 257 | console.groupCollapsed('bar'); 258 | 259 | var a = 'foo'; 260 | console.log('a: ', a); 261 | const b = 'bar'; 262 | console.log('b: ', b); 263 | let c = [a, b].map(x => x); 264 | console.log('c: ', c); 265 | let d = a; 266 | console.log('d: ', d); 267 | 268 | var _returnValue = c.join('-'); 269 | 270 | console.log('Return Value:', _returnValue); 271 | console.groupEnd('bar'); 272 | return _returnValue; 273 | } 274 | " 275 | `; 276 | 277 | exports[`function assignment 1`] = ` 278 | " 279 | // sitrep 280 | var a = function () { 281 | return 'foo' 282 | } 283 | 284 | ↓ ↓ ↓ ↓ ↓ ↓ 285 | 286 | // sitrep 287 | var a = function () { 288 | console.groupCollapsed('a'); 289 | var _returnValue = 'foo'; 290 | console.log('Return Value:', _returnValue); 291 | console.groupEnd('a'); 292 | 293 | return _returnValue; 294 | }; 295 | " 296 | `; 297 | 298 | exports[`function declaration with multiple returns 1`] = ` 299 | " 300 | myFn( 301 | // sitrep 302 | function(err, vars) { 303 | if (err) { 304 | return err 305 | } 306 | 307 | vars = vars.map(x => x) 308 | return vars 309 | } 310 | ) 311 | 312 | ↓ ↓ ↓ ↓ ↓ ↓ 313 | 314 | myFn( 315 | // sitrep 316 | function (err, vars) { 317 | console.groupCollapsed(\\"function: function(err, vars) {\\"); 318 | 319 | if (err) { 320 | var _returnValue = err; 321 | console.log(\\"Return Value:\\", _returnValue); 322 | console.groupEnd(\\"function: function(err, vars) {\\"); 323 | 324 | return _returnValue; 325 | } 326 | 327 | vars = vars.map(x => x); 328 | console.log(\\"vars: \\", vars); 329 | var _returnValue2 = vars; 330 | console.log(\\"Return Value:\\", _returnValue2); 331 | console.groupEnd(\\"function: function(err, vars) {\\"); 332 | return _returnValue2; 333 | }); 334 | " 335 | `; 336 | 337 | exports[`function has a for loop 1`] = ` 338 | " 339 | // sitrep 340 | function test() { 341 | var sum = 0; 342 | for (var i = 0; i < 5; i++) { 343 | sum = sum + i; 344 | } 345 | return sum; 346 | } 347 | 348 | ↓ ↓ ↓ ↓ ↓ ↓ 349 | 350 | // sitrep 351 | function test() { 352 | console.groupCollapsed(\\"test\\"); 353 | 354 | var sum = 0; 355 | console.log(\\"sum: \\", sum); 356 | for (var i = 0; i < 5; i++) { 357 | sum = sum + i; 358 | console.log(\\"sum: \\", sum); 359 | } 360 | var _returnValue = sum; 361 | console.log(\\"Return Value:\\", _returnValue); 362 | console.groupEnd(\\"test\\"); 363 | return _returnValue; 364 | } 365 | " 366 | `; 367 | 368 | exports[`function has a for-of loop 1`] = ` 369 | " 370 | // sitrep 371 | function test(arr) { 372 | var sum = 0; 373 | for (const value of arr) { 374 | sum += value; 375 | } 376 | } 377 | 378 | ↓ ↓ ↓ ↓ ↓ ↓ 379 | 380 | // sitrep 381 | function test(arr) { 382 | console.groupCollapsed(\\"test\\"); 383 | 384 | var sum = 0; 385 | console.log(\\"sum: \\", sum); 386 | for (const value of arr) { 387 | sum += value; 388 | console.log(\\"sum: \\", sum); 389 | } 390 | console.groupEnd(\\"test\\"); 391 | } 392 | " 393 | `; 394 | 395 | exports[`no function parent 1`] = ` 396 | " 397 | // sitrep 398 | if (a) { 399 | let a = 2 400 | 401 | let b = a + 5 402 | } 403 | 404 | ↓ ↓ ↓ ↓ ↓ ↓ 405 | 406 | // sitrep 407 | if (a) { 408 | let a = 2; 409 | 410 | let b = a + 5; 411 | } 412 | " 413 | `; 414 | 415 | exports[`object properties 1`] = ` 416 | " 417 | const obj = { 418 | // sitrep 419 | fn() { 420 | const { a, b, c = 'foo', d: alias } = x; 421 | return a + b + c + alias; 422 | } 423 | } 424 | 425 | ↓ ↓ ↓ ↓ ↓ ↓ 426 | 427 | const obj = { 428 | // sitrep 429 | fn() { 430 | console.groupCollapsed('fn'); 431 | 432 | const { a, b, c = 'foo', d: alias } = x; 433 | console.log('a', a); 434 | console.log('b', b); 435 | console.log('c', c); 436 | console.log('alias', alias); 437 | 438 | var _returnValue = a + b + c + alias; 439 | 440 | console.log('Return Value:', _returnValue); 441 | console.groupEnd('fn'); 442 | return _returnValue; 443 | } 444 | }; 445 | " 446 | `; 447 | 448 | exports[`object properties with let 1`] = ` 449 | " 450 | const obj = { 451 | // sitrep 452 | fn(x) { 453 | let a, b, c, d; 454 | ({ a, b, c = 'foo', d: alias } = x); 455 | return a + b + c + alias; 456 | } 457 | } 458 | 459 | ↓ ↓ ↓ ↓ ↓ ↓ 460 | 461 | const obj = { 462 | // sitrep 463 | fn(x) { 464 | console.groupCollapsed('fn'); 465 | 466 | let a, b, c, d; 467 | ({ a, b, c = 'foo', d: alias } = x); 468 | console.log('a', a); 469 | console.log('b', b); 470 | console.log('c', c); 471 | console.log('alias', alias); 472 | 473 | var _returnValue = a + b + c + alias; 474 | 475 | console.log('Return Value:', _returnValue); 476 | console.groupEnd('fn'); 477 | return _returnValue; 478 | } 479 | }; 480 | " 481 | `; 482 | 483 | exports[`optional log prefix 1`] = ` 484 | " 485 | // sitrep prefix 486 | function bar () { 487 | var a = 'foo' 488 | const b = 'bar' 489 | let c = [a, b].map(x => x) 490 | let d = a 491 | return c.join('-') 492 | } 493 | 494 | ↓ ↓ ↓ ↓ ↓ ↓ 495 | 496 | // sitrep prefix 497 | function bar() { 498 | console.groupCollapsed('(prefix) bar'); 499 | 500 | var a = 'foo'; 501 | console.log('a: ', a); 502 | const b = 'bar'; 503 | console.log('b: ', b); 504 | let c = [a, b].map(x => x); 505 | console.log('c: ', c); 506 | let d = a; 507 | console.log('d: ', d); 508 | 509 | var _returnValue = c.join('-'); 510 | 511 | console.log('Return Value:', _returnValue); 512 | console.groupEnd('(prefix) bar'); 513 | return _returnValue; 514 | } 515 | " 516 | `; 517 | 518 | exports[`should not add console logs 1`] = ` 519 | " 520 | // sitrep 521 | const shouldNotLog = 'bar'; 522 | 523 | function bar () { 524 | var a = 'foo' 525 | const b = 'bar' 526 | let c = [a, b].map(x => x) 527 | return c.join('-') 528 | } 529 | 530 | var cb = x => x.charAt(0) 531 | 532 | var cb = x => { 533 | x = x + 2 534 | x.charAt(0) 535 | return x 536 | } 537 | 538 | var a = function () { 539 | return 'foo' 540 | } 541 | 542 | const obj = { 543 | fn() { 544 | let a = 5 545 | return a + 5 546 | } 547 | } 548 | 549 | class Boom { 550 | // sitrep 551 | shouldNotLog = 'bar'; 552 | 553 | fire() { 554 | let a = 2 555 | 556 | return a + 5 557 | } 558 | 559 | handleClick = (e) => e.preventDefault() 560 | } 561 | 562 | ↓ ↓ ↓ ↓ ↓ ↓ 563 | 564 | // sitrep 565 | const shouldNotLog = 'bar'; 566 | 567 | function bar() { 568 | var a = 'foo'; 569 | const b = 'bar'; 570 | let c = [a, b].map(x => x); 571 | return c.join('-'); 572 | } 573 | 574 | var cb = x => x.charAt(0); 575 | 576 | var cb = x => { 577 | x = x + 2; 578 | x.charAt(0); 579 | return x; 580 | }; 581 | 582 | var a = function () { 583 | return 'foo'; 584 | }; 585 | 586 | const obj = { 587 | fn() { 588 | let a = 5; 589 | return a + 5; 590 | } 591 | }; 592 | 593 | class Boom { 594 | constructor() { 595 | this.shouldNotLog = 'bar'; 596 | 597 | this.handleClick = e => e.preventDefault(); 598 | } 599 | // sitrep 600 | 601 | 602 | fire() { 603 | let a = 2; 604 | 605 | return a + 5; 606 | } 607 | 608 | } 609 | " 610 | `; 611 | --------------------------------------------------------------------------------