babel-plugin-captains-log
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -1,2 +0,2 @@ | ||
"use strict"; | ||
'use strict'; | ||
@@ -7,58 +7,172 @@ Object.defineProperty(exports, "__esModule", { | ||
var _typeof2 = require('babel-runtime/helpers/typeof'); | ||
var _typeof3 = _interopRequireDefault(_typeof2); | ||
var _toConsumableArray2 = require('babel-runtime/helpers/toConsumableArray'); | ||
var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2); | ||
var _keys = require('babel-runtime/core-js/object/keys'); | ||
var _keys2 = _interopRequireDefault(_keys); | ||
var _set = require('babel-runtime/core-js/set'); | ||
var _set2 = _interopRequireDefault(_set); | ||
exports.default = function (_ref) { | ||
var t = _ref.types; | ||
var buildScope = function buildScope(path) { | ||
var scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : []; | ||
var name = 'babel-plugin-captains-log'; | ||
var callExpressions = new _set2.default(); | ||
var evaluatedExpressions = new _set2.default(); | ||
return { | ||
name: name, | ||
visitor: { | ||
Identifier: function Identifier(path, _ref2) { | ||
var _ref2$opts = _ref2.opts, | ||
opts = _ref2$opts === undefined ? {} : _ref2$opts, | ||
file = _ref2.file; | ||
var functionParent = path.getFunctionParent(); | ||
if (!functionParent) { | ||
return scope; | ||
} | ||
if (functionParent.isFunctionDeclaration()) { | ||
scope.push(functionParent.node.id.name); | ||
} else if (t.isClassProperty(functionParent) || t.isClassMethod(functionParent)) { | ||
scope.push(functionParent.node.key.name); | ||
var classBody = functionParent.findParent(function (path) { | ||
return path.isClassDeclaration() || path.isClassExpression(); | ||
}); | ||
if (classBody) { | ||
scope.push(classBody.node.id.name); | ||
return buildScope(classBody, scope); | ||
} | ||
} else if (functionParent.isArrowFunctionExpression()) { | ||
var arrFuncParent = functionParent.findParent(function (path) { | ||
return path; | ||
}); | ||
if (t.isVariableDeclarator(arrFuncParent)) { | ||
scope.push(arrFuncParent.node.id.name); | ||
} else if (t.isClassProperty(arrFuncParent) || t.isClassMethod(arrFuncParent)) { | ||
scope.push(arrFuncParent.node.key.name); | ||
var _classBody = arrFuncParent.findParent(function (path) { | ||
return path.isClassDeclaration() || path.isClassExpression(); | ||
}); | ||
if (_classBody) { | ||
scope.push(_classBody.node.id.name); | ||
return buildScope(_classBody, scope); | ||
if (matchesIgnorePattern(opts.ignorePatterns, file)) { | ||
return; | ||
} | ||
} else if (t.isCallExpression(arrFuncParent)) { | ||
var callee = arrFuncParent.node.callee; | ||
if (!looksLike(path.node, { name: 'console' })) { | ||
return; | ||
} | ||
// find somewhere we can move this so that it only needs to be called once. | ||
var settings = (0, _pluginOptions.buildOptions)(opts || {}); | ||
var parentCallExp = path.findParent(t.isCallExpression); | ||
if (isTrackingConsoleCallStatement(path, parentCallExp, settings)) { | ||
callExpressions.add(parentCallExp); | ||
} | ||
}, | ||
scope.push("[" + callee.object.name + "." + callee.property.name + "]"); | ||
Program: { | ||
exit: function exit(_, _ref3) { | ||
var file = _ref3.file, | ||
opts = _ref3.opts; | ||
var settings = (0, _pluginOptions.buildOptions)(opts || {}); | ||
callExpressions.forEach(function (callExp) { | ||
if (!callExp || evaluatedExpressions.has(callExp)) { | ||
return; | ||
} | ||
var options = settings[getConsoleCallMethodName(callExp)]; | ||
var args = callExp.get('arguments'); | ||
if (options.injectVariableName) { | ||
args = injectVariableNames(args); | ||
} | ||
if (options.injectScope) { | ||
var scope = findCallScope(callExp); | ||
args = prependArguments(args, scope); | ||
} | ||
if (options.injectFileName) { | ||
var filename = void 0; | ||
if (file) { | ||
filename = file.opts.filename; | ||
} | ||
var start = callExp.node.loc.start; | ||
var lineCol = '(' + start.line + ':' + start.column + ')'; | ||
args = prependArguments(args, '' + filename + lineCol); | ||
} | ||
callExp.set('arguments', args); | ||
}); | ||
} | ||
} | ||
} | ||
return buildScope(functionParent, scope); | ||
}; | ||
function matchesIgnorePattern() { | ||
var ignorePatterns = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ['node_modules']; | ||
var file = arguments[1]; | ||
return { | ||
name: "babel-plugin-captains-log", // not required | ||
visitor: { | ||
MemberExpression: function MemberExpression(path) { | ||
if (path.get("object").isIdentifier({ name: "console" })) { | ||
var scope = buildScope(path); | ||
path.parent.arguments.unshift(t.stringLiteral(scope.reverse().join(".") + ":")); | ||
} | ||
return ignorePatterns.some(function (pattern) { | ||
return file.opts.filename.includes(pattern); | ||
}); | ||
} | ||
function getConsoleCallMethodName(callExpression) { | ||
return callExpression.get('callee.property').node.name; | ||
} | ||
function isTrackingConsoleCallStatement(path, parentCallExp, settings) { | ||
return parentCallExp && parentCallExp.node.callee === path.parent && (0, _keys2.default)(settings).includes(getConsoleCallMethodName(parentCallExp)); | ||
} | ||
function injectVariableNames() { | ||
var args = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; | ||
return args.reduce(function (acc, arg) { | ||
if (!t.isLiteral(arg)) { | ||
return [].concat((0, _toConsumableArray3.default)(acc), [t.stringLiteral(arg.getSource()), arg.node]); | ||
} | ||
return [].concat((0, _toConsumableArray3.default)(acc), [arg.node]); | ||
}, []); | ||
} | ||
function findCallScope(path) { | ||
var scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : []; | ||
var parentFunc = path.findParent(function (path) { | ||
return (0, _keys2.default)(scopeHandlers).includes(path.type); | ||
}); | ||
if (parentFunc) { | ||
return findCallScope(parentFunc, [scopeHandlers[parentFunc.type](parentFunc)].concat((0, _toConsumableArray3.default)(scope))); | ||
} | ||
}; | ||
}; | ||
return scope.length ? scope.join('.') + ':' : ''; | ||
} | ||
function prependArguments() { | ||
var args = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; | ||
var value = arguments[1]; | ||
if (value) { | ||
return [t.stringLiteral(value)].concat((0, _toConsumableArray3.default)(args)); | ||
} | ||
return args; | ||
} | ||
}; | ||
var _pluginOptions = require('./utils/pluginOptions'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var idNameSelector = function idNameSelector(path) { | ||
return path.node.id.name; | ||
}; | ||
var keyNameSelector = function keyNameSelector(path) { | ||
return path.node.key.name; | ||
}; | ||
var scopeHandlers = { | ||
FunctionDeclaration: idNameSelector, | ||
VariableDeclarator: idNameSelector, | ||
ObjectProperty: keyNameSelector, | ||
ObjectMethod: keyNameSelector, | ||
ClassMethod: keyNameSelector, | ||
ClassExpression: idNameSelector, | ||
ClassDeclaration: idNameSelector, | ||
AssignmentExpression: function AssignmentExpression(path) { | ||
return path.node.left.name; | ||
} | ||
}; | ||
function looksLike(a, b) { | ||
return a && b && (0, _keys2.default)(b).every(function (bKey) { | ||
var bVal = b[bKey]; | ||
var aVal = a[bKey]; | ||
if (typeof bVal === 'function') { | ||
return bVal(aVal); | ||
} | ||
return isPrimitive(bVal) ? bVal === aVal : looksLike(aVal, bVal); | ||
}); | ||
} | ||
function isPrimitive(val) { | ||
return val == null || /^[sbn]/.test(typeof val === 'undefined' ? 'undefined' : (0, _typeof3.default)(val)); | ||
} |
{ | ||
"name": "babel-plugin-captains-log", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"files": [ | ||
@@ -11,7 +11,8 @@ "dist" | ||
"prebuild": "rimraf dist", | ||
"build": "babel --copy-files --out-dir dist --ignore *.spec.js,__mocks__,__snapshots__ src", | ||
"build": "babel --copy-files --out-dir dist --ignore *.spec.js,__snapshots__,__test__ src", | ||
"report-coverage": "codecov", | ||
"contribute": "all-contributors", | ||
"release": "semantic-release pre && npm publish && semantic-release post", | ||
"semantic-release": "semantic-release pre && npm publish && semantic-release post" | ||
"semantic-release": "semantic-release pre && npm publish && semantic-release post", | ||
"lint": "eslint ." | ||
}, | ||
@@ -37,6 +38,10 @@ "keywords": [ | ||
"babel-cli": "^6.24.1", | ||
"babel-eslint": "^7.2.3", | ||
"babel-jest": "^19.0.0", | ||
"babel-plugin-tester": "^3.1.0", | ||
"babel-plugin-transform-runtime": "^6.23.0", | ||
"babel-preset-env": "^1.3.3", | ||
"babel-preset-stage-2": "^6.24.1", | ||
"codecov": "^2.1.0", | ||
"eslint": "^3.19.0", | ||
"fs": "^0.0.1-security", | ||
@@ -48,3 +53,10 @@ "global": "^4.3.2", | ||
"semantic-release": "^6.3.2" | ||
}, | ||
"jest": { | ||
"coveragePathIgnorePatterns": [ | ||
"/node_modules", | ||
"__fixtures__", | ||
"__snapshots__" | ||
] | ||
} | ||
} | ||
} |
127
README.md
# Captain's Log (☠️) | ||
[![version](https://img.shields.io/npm/v/babel-plugin-captains-log.svg?style=flat-square)](http://npm.im/babel-plugin-captains-log) | ||
[![downloads](https://img.shields.io/npm/dm/babel-plugin-captains-log.svg?style=flat-square)](http://npm-stat.com/charts.html?package=babel-plugin-captains-log) | ||
[![Travis Build Status](https://img.shields.io/travis/kwelch/babel-plugin-captains-log.svg?style=flat-square)](https://travis-ci.org/kwelch/babel-plugin-captains-log) | ||
[![codecov](https://codecov.io/gh/kwelch/babel-plugin-captains-log/branch/master/graph/badge.svg)](https://codecov.io/gh/kwelch/babel-plugin-captains-log) | ||
[![codecov](https://codecov.io/gh/kwelch/babel-plugin-captains-log/branch/master/graph/badge.svg?style=flat-square)](https://codecov.io/gh/kwelch/babel-plugin-captains-log) | ||
[![MIT License](https://img.shields.io/npm/l/kwelch.svg?style=flat-square)](http://opensource.org/licenses/MIT) | ||
[![Roadmap](https://img.shields.io/badge/%F0%9F%93%94-roadmap-CD9523.svg?style=flat-square)]([roadmap]) | ||
[![All Contributors](https://img.shields.io/badge/all_contributors-1-orange.svg?style=flat-square)](#contributors) | ||
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release) | ||
[![All Contributors](https://img.shields.io/badge/all_contributors-2-orange.svg?style=flat-square)](#contributors) | ||
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg?style=flat-square)](https://github.com/semantic-release/semantic-release) | ||
@@ -21,31 +21,20 @@ [![Watch on GitHub](https://img.shields.io/github/watchers/kwelch/babel-plugin-captains-log.svg?style=social)](https://github.com/kwelch/babel-plugin-captains-log/watchers) | ||
Current version only injects the console statments scope. | ||
Default bahavior: | ||
- prepend console statement file & location | ||
- add inject variable name into console statements | ||
**Transforms** | ||
``` | ||
function add(a, b) { | ||
console.log(a, b); | ||
```diff | ||
function add(a = 1, b = 2) { | ||
console.log(a); // outputs: 1 | ||
return a + b; | ||
} | ||
const subtract = (a, b) => { | ||
console.log(a, b); | ||
return a - b; | ||
}; | ||
``` | ||
**Into** | ||
``` | ||
function add(a, b) { | ||
console.log("add:", a, b); | ||
↓ ↓ ↓ ↓ ↓ ↓ | ||
function add(a = 1, b = 2) { | ||
console.log("simple.js(2:2)", "a", a); // outputs: "simple.js(2:2)" "a" 1 | ||
return a + b; | ||
} | ||
const subtract = (a, b) => { | ||
console.log("subtract:", a, b); | ||
return a - b; | ||
}; | ||
``` | ||
See the [Roadmap][roadmap] for a future features and oppurtunities to contribute. | ||
See the [Issues][issues] for a future features and oppurtunities to contribute. | ||
@@ -69,14 +58,80 @@ ## Requirements | ||
``` | ||
Settings and options are below. | ||
## Options | ||
## Roadmap | ||
### Methods | ||
This option provides control over which console statments are adjusted. Methods is set within your `.babelrc` as an array. | ||
- [ ] Handle alias for console/ desctructored methods | ||
- [ ] Arrow functions within classes (stage-2 integration) | ||
- [ ] Add config for methods | ||
- [ ] Add ability to label variables in console statments | ||
- [ ] Add ability to timestamp console statements | ||
- [ ] Add ability to auto add console statements to methods | ||
**Default**: `["debug", "error", "exception", "info", "log", "warn"]` | ||
``` | ||
{ | ||
plugins: [ | ||
["captains-log", { | ||
"methods": ['debug', 'info'] | ||
}] | ||
] | ||
} | ||
``` | ||
### Ignore Patterns | ||
This option provides control over which files are adjusted. Ignore Patterns is set within your `.babelrc` as an array of strings. | ||
**Default**: `["node_modules"]` | ||
``` | ||
{ | ||
plugins: [ | ||
["captains-log", { | ||
"ignorePatterns": ["node_modules", ".spec.js"] | ||
}] | ||
] | ||
} | ||
``` | ||
### Flags | ||
Flags are values set for all methods and are used to turn that feature on or off. Flags are not merged with defaults to allow for maximum control. | ||
#### Variable Name Labels | ||
**Default**: `true` | ||
``` | ||
{ | ||
plugins: [ | ||
["captains-log", { | ||
"injectVariableName": true | ||
}] | ||
] | ||
} | ||
``` | ||
#### File Location Data | ||
**Default**: `true` | ||
``` | ||
{ | ||
plugins: [ | ||
["captains-log", { | ||
"injectFileName": true | ||
}] | ||
] | ||
} | ||
``` | ||
#### Inject Scope _(Experimental)_ | ||
_This has a few issues with other plugins particularly react-hot-loader, as it changes method names. Also, it was written for recursion which adds too much noise to the console statement which is against this libraries purpose_ | ||
**Default**: `false` | ||
``` | ||
{ | ||
plugins: [ | ||
["captains-log", { | ||
"injectScope": true | ||
}] | ||
] | ||
} | ||
``` | ||
## License | ||
@@ -91,4 +146,4 @@ | ||
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section --> | ||
| [<img src="https://avatars0.githubusercontent.com/u/1295580?v=3" width="100px;"/><br /><sub>Kyle Welch</sub>](http://www.krwelch.com)<br />[💻](https://github.com/kwelch/babel-plugin-captains-log/commits?author=kwelch "Code") [📖](https://github.com/kwelch/babel-plugin-captains-log/commits?author=kwelch "Documentation") [⚠️](https://github.com/kwelch/babel-plugin-captains-log/commits?author=kwelch "Tests") | | ||
| :---: | | ||
| [<img src="https://avatars0.githubusercontent.com/u/1295580?v=3" width="100px;"/><br /><sub>Kyle Welch</sub>](http://www.krwelch.com)<br />[💻](https://github.com/kwelch/babel-plugin-captains-log/commits?author=kwelch "Code") [📖](https://github.com/kwelch/babel-plugin-captains-log/commits?author=kwelch "Documentation") [⚠️](https://github.com/kwelch/babel-plugin-captains-log/commits?author=kwelch "Tests") | [<img src="https://avatars1.githubusercontent.com/u/9456433?v=4" width="100px;"/><br /><sub>Maksim</sub>](https://github.com/mqklin)<br />[🐛](https://github.com/kwelch/babel-plugin-captains-log/issues?q=author%3Amqklin "Bug reports") | | ||
| :---: | :---: | | ||
<!-- ALL-CONTRIBUTORS-LIST:END --> | ||
@@ -99,2 +154,2 @@ | ||
[roadmap]: https://github.com/kwelch/babel-plugin-captains-log#roadmap | ||
[issues]: https://github.com/kwelch/babel-plugin-captains-log/issues |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16470
209
152
16
7
1