babel-plugin-trace
Advanced tools
Comparing version 1.0.0 to 1.1.0
201
lib/index.js
@@ -7,9 +7,11 @@ 'use strict'; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
exports.default = function (_ref11) { | ||
var t = _ref11.types; | ||
var template = _ref11.template; | ||
var t = _ref11.types, | ||
template = _ref11.template; | ||
function _ref(_id) { | ||
if (!Plugin(_id)) { | ||
throw new TypeError('Function return value violates contract.\n\nExpected:\n{ visitor: Visitors\n}\n\nGot:\n' + _inspect(_id)); | ||
throw new TypeError('Function return value violates contract.\n\nExpected:\nPlugin\n\nGot:\n' + _inspect(_id)); | ||
} | ||
@@ -21,3 +23,3 @@ | ||
if (!PluginParams(arguments[0])) { | ||
throw new TypeError('Value of argument 0 violates contract.\n\nExpected:\n{ types: Object;\n template(source: string): Template;\n}\n\nGot:\n' + _inspect({ types: t, template: template })); | ||
throw new TypeError('Value of argument 0 violates contract.\n\nExpected:\nPluginParams\n\nGot:\n' + _inspect(arguments[0])); | ||
} | ||
@@ -63,3 +65,3 @@ | ||
if (!Template(_id3)) { | ||
throw new TypeError('Function "expression" return value violates contract.\n\nExpected:\n({ [key: string]: Node\n}) => { type: string;\n node?: void;\n}\n\nGot:\n' + _inspect(_id3)); | ||
throw new TypeError('Function "expression" return value violates contract.\n\nExpected:\nTemplate\n\nGot:\n' + _inspect(_id3)); | ||
} | ||
@@ -77,3 +79,3 @@ | ||
if (!Template(fn)) { | ||
throw new TypeError('Value of variable "fn" violates contract.\n\nExpected:\n({ [key: string]: Node\n}) => { type: string;\n node?: void;\n}\n\nGot:\n' + _inspect(fn)); | ||
throw new TypeError('Value of variable "fn" violates contract.\n\nExpected:\nTemplate\n\nGot:\n' + _inspect(fn)); | ||
} | ||
@@ -84,3 +86,3 @@ | ||
if (!Node(_id4)) { | ||
throw new TypeError('Function return value violates contract.\n\nExpected:\n{ type: string;\n node?: void;\n}\n\nGot:\n' + _inspect(_id4)); | ||
throw new TypeError('Function return value violates contract.\n\nExpected:\nNode\n\nGot:\n' + _inspect(_id4)); | ||
} | ||
@@ -92,3 +94,3 @@ | ||
if (!TemplateIds(ids)) { | ||
throw new TypeError('Value of argument "ids" violates contract.\n\nExpected:\n{ [key: string]: Node\n}\n\nGot:\n' + _inspect(ids)); | ||
throw new TypeError('Value of argument "ids" violates contract.\n\nExpected:\nTemplateIds\n\nGot:\n' + _inspect(ids)); | ||
} | ||
@@ -99,3 +101,3 @@ | ||
if (!Node(node)) { | ||
throw new TypeError('Value of variable "node" violates contract.\n\nExpected:\n{ type: string;\n node?: void;\n}\n\nGot:\n' + _inspect(node)); | ||
throw new TypeError('Value of variable "node" violates contract.\n\nExpected:\nNode\n\nGot:\n' + _inspect(node)); | ||
} | ||
@@ -113,3 +115,3 @@ | ||
if (!PluginOptions(_id5)) { | ||
throw new TypeError('Function "normalizeOpts" return value violates contract.\n\nExpected:\n{ aliases?: { [key: string]: string | Template\n };\n strip?: bool | string | { [key: string]: bool\n };\n}\n\nGot:\n' + _inspect(_id5)); | ||
throw new TypeError('Function "normalizeOpts" return value violates contract.\n\nExpected:\nPluginOptions\n\nGot:\n' + _inspect(_id5)); | ||
} | ||
@@ -121,3 +123,3 @@ | ||
if (!PluginOptions(opts)) { | ||
throw new TypeError('Value of argument "opts" violates contract.\n\nExpected:\n{ aliases?: { [key: string]: string | Template\n };\n strip?: bool | string | { [key: string]: bool\n };\n}\n\nGot:\n' + _inspect(opts)); | ||
throw new TypeError('Value of argument "opts" violates contract.\n\nExpected:\nPluginOptions\n\nGot:\n' + _inspect(opts)); | ||
} | ||
@@ -137,25 +139,23 @@ | ||
if (typeof opts.aliases[key] === 'string' && opts.aliases[key]) { | ||
(function () { | ||
var expr = expression(opts.aliases[key]); | ||
var expr = expression(opts.aliases[key]); | ||
if (!(typeof expr === 'function')) { | ||
throw new TypeError('Value of variable "expr" violates contract.\n\nExpected:\n({ prefix: Literal;\n indent: Literal;\n parentName: Literal;\n filename: Literal;\n dirname: Literal;\n basename: Literal;\n extname: Literal;\n content: Node;\n}) => { type: string;\n node?: void;\n}\n\nGot:\n' + _inspect(expr)); | ||
} | ||
if (!(typeof expr === 'function')) { | ||
throw new TypeError('Value of variable "expr" violates contract.\n\nExpected:\n(Message) => Node\n\nGot:\n' + _inspect(expr)); | ||
} | ||
opts.aliases[key] = function (message) { | ||
function _ref6(_id6) { | ||
if (!Node(_id6)) { | ||
throw new TypeError('Function return value violates contract.\n\nExpected:\n{ type: string;\n node?: void;\n}\n\nGot:\n' + _inspect(_id6)); | ||
} | ||
return _id6; | ||
opts.aliases[key] = function (message) { | ||
function _ref6(_id6) { | ||
if (!Node(_id6)) { | ||
throw new TypeError('Function return value violates contract.\n\nExpected:\nNode\n\nGot:\n' + _inspect(_id6)); | ||
} | ||
if (!Message(message)) { | ||
throw new TypeError('Value of argument "message" violates contract.\n\nExpected:\n{ prefix: Literal;\n indent: Literal;\n parentName: Literal;\n filename: Literal;\n dirname: Literal;\n basename: Literal;\n extname: Literal;\n content: Node;\n}\n\nGot:\n' + _inspect(message)); | ||
} | ||
return _id6; | ||
} | ||
return _ref6(expr(message)); | ||
}; | ||
})(); | ||
if (!Message(message)) { | ||
throw new TypeError('Value of argument "message" violates contract.\n\nExpected:\nMessage\n\nGot:\n' + _inspect(message)); | ||
} | ||
return _ref6(expr(message)); | ||
}; | ||
} | ||
@@ -174,3 +174,3 @@ }); | ||
if (!Node(_id7)) { | ||
throw new TypeError('Function "defaultLog" return value violates contract.\n\nExpected:\n{ type: string;\n node?: void;\n}\n\nGot:\n' + _inspect(_id7)); | ||
throw new TypeError('Function "defaultLog" return value violates contract.\n\nExpected:\nNode\n\nGot:\n' + _inspect(_id7)); | ||
} | ||
@@ -182,7 +182,7 @@ | ||
if (!Message(message)) { | ||
throw new TypeError('Value of argument "message" violates contract.\n\nExpected:\n{ prefix: Literal;\n indent: Literal;\n parentName: Literal;\n filename: Literal;\n dirname: Literal;\n basename: Literal;\n extname: Literal;\n content: Node;\n}\n\nGot:\n' + _inspect(message)); | ||
throw new TypeError('Value of argument "message" violates contract.\n\nExpected:\nMessage\n\nGot:\n' + _inspect(message)); | ||
} | ||
if (!Metadata(metadata)) { | ||
throw new TypeError('Value of argument "metadata" violates contract.\n\nExpected:\n{ indent: number;\n prefix: string;\n parentName: string;\n filename: string;\n dirname: string;\n basename: string;\n extname: string;\n hasStartMessage: bool;\n isStartMessage: bool;\n}\n\nGot:\n' + _inspect(metadata)); | ||
throw new TypeError('Value of argument "metadata" violates contract.\n\nExpected:\nMetadata\n\nGot:\n' + _inspect(metadata)); | ||
} | ||
@@ -201,5 +201,5 @@ | ||
} else { | ||
return _ref7(expression('console.log(prefix, content)')({ | ||
prefix: t.stringLiteral(prefix), | ||
content: message.content | ||
return _ref7(expression('console.log(PREFIX, CONTENT)')({ | ||
PREFIX: t.stringLiteral(prefix), | ||
CONTENT: message.content | ||
})); | ||
@@ -244,3 +244,3 @@ } | ||
if (!Metadata(_id9)) { | ||
throw new TypeError('Function "collectMetadata" return value violates contract.\n\nExpected:\n{ indent: number;\n prefix: string;\n parentName: string;\n filename: string;\n dirname: string;\n basename: string;\n extname: string;\n hasStartMessage: bool;\n isStartMessage: bool;\n}\n\nGot:\n' + _inspect(_id9)); | ||
throw new TypeError('Function "collectMetadata" return value violates contract.\n\nExpected:\nMetadata\n\nGot:\n' + _inspect(_id9)); | ||
} | ||
@@ -252,7 +252,7 @@ | ||
if (!NodePath(path)) { | ||
throw new TypeError('Value of argument "path" violates contract.\n\nExpected:\n{ type: string;\n node: Node;\n scope: Scope;\n}\n\nGot:\n' + _inspect(path)); | ||
throw new TypeError('Value of argument "path" violates contract.\n\nExpected:\nNodePath\n\nGot:\n' + _inspect(path)); | ||
} | ||
if (!PluginOptions(opts)) { | ||
throw new TypeError('Value of argument "opts" violates contract.\n\nExpected:\n{ aliases?: { [key: string]: string | Template\n };\n strip?: bool | string | { [key: string]: bool\n };\n}\n\nGot:\n' + _inspect(opts)); | ||
throw new TypeError('Value of argument "opts" violates contract.\n\nExpected:\nPluginOptions\n\nGot:\n' + _inspect(opts)); | ||
} | ||
@@ -294,3 +294,3 @@ | ||
var indent = 0; | ||
var parent = undefined; | ||
var parent = void 0; | ||
@@ -309,3 +309,3 @@ if (!(parent == null || NodePath(parent))) { | ||
if (!NodePath(item)) { | ||
throw new TypeError('Value of argument "item" violates contract.\n\nExpected:\n{ type: string;\n node: Node;\n scope: Scope;\n}\n\nGot:\n' + _inspect(item)); | ||
throw new TypeError('Value of argument "item" violates contract.\n\nExpected:\nNodePath\n\nGot:\n' + _inspect(item)); | ||
} | ||
@@ -362,3 +362,3 @@ | ||
if (!NodePath(_child)) { | ||
throw new TypeError('Value of variable "child" violates contract.\n\nExpected:\n{ type: string;\n node: Node;\n scope: Scope;\n}\n\nGot:\n' + _inspect(_child)); | ||
throw new TypeError('Value of variable "child" violates contract.\n\nExpected:\nNodePath\n\nGot:\n' + _inspect(_child)); | ||
} | ||
@@ -376,3 +376,3 @@ | ||
if (!NodePath(label)) { | ||
throw new TypeError('Value of variable "label" violates contract.\n\nExpected:\n{ type: string;\n node: Node;\n scope: Scope;\n}\n\nGot:\n' + _inspect(label)); | ||
throw new TypeError('Value of variable "label" violates contract.\n\nExpected:\nNodePath\n\nGot:\n' + _inspect(label)); | ||
} | ||
@@ -417,7 +417,7 @@ | ||
if (!Metadata(metadata)) { | ||
throw new TypeError('Value of argument "metadata" violates contract.\n\nExpected:\n{ indent: number;\n prefix: string;\n parentName: string;\n filename: string;\n dirname: string;\n basename: string;\n extname: string;\n hasStartMessage: bool;\n isStartMessage: bool;\n}\n\nGot:\n' + _inspect(metadata)); | ||
throw new TypeError('Value of argument "metadata" violates contract.\n\nExpected:\nMetadata\n\nGot:\n' + _inspect(metadata)); | ||
} | ||
if (!PluginOptions(opts)) { | ||
throw new TypeError('Value of argument "opts" violates contract.\n\nExpected:\n{ aliases?: { [key: string]: string | Template\n };\n strip?: bool | string | { [key: string]: bool\n };\n}\n\nGot:\n' + _inspect(opts)); | ||
throw new TypeError('Value of argument "opts" violates contract.\n\nExpected:\nPluginOptions\n\nGot:\n' + _inspect(opts)); | ||
} | ||
@@ -445,3 +445,3 @@ | ||
if (!Metadata(metadata)) { | ||
throw new TypeError('Value of argument "metadata" violates contract.\n\nExpected:\n{ indent: number;\n prefix: string;\n parentName: string;\n filename: string;\n dirname: string;\n basename: string;\n extname: string;\n hasStartMessage: bool;\n isStartMessage: bool;\n}\n\nGot:\n' + _inspect(metadata)); | ||
throw new TypeError('Value of argument "metadata" violates contract.\n\nExpected:\nMetadata\n\nGot:\n' + _inspect(metadata)); | ||
} | ||
@@ -472,3 +472,3 @@ | ||
if (!NodePath(program)) { | ||
throw new TypeError('Value of argument "program" violates contract.\n\nExpected:\n{ type: string;\n node: Node;\n scope: Scope;\n}\n\nGot:\n' + _inspect(program)); | ||
throw new TypeError('Value of argument "program" violates contract.\n\nExpected:\nNodePath\n\nGot:\n' + _inspect(program)); | ||
} | ||
@@ -479,3 +479,3 @@ | ||
if (!NodePath(path)) { | ||
throw new TypeError('Value of argument "path" violates contract.\n\nExpected:\n{ type: string;\n node: Node;\n scope: Scope;\n}\n\nGot:\n' + _inspect(path)); | ||
throw new TypeError('Value of argument "path" violates contract.\n\nExpected:\nNodePath\n\nGot:\n' + _inspect(path)); | ||
} | ||
@@ -486,3 +486,3 @@ | ||
if (!NodePath(label)) { | ||
throw new TypeError('Value of variable "label" violates contract.\n\nExpected:\n{ type: string;\n node: Node;\n scope: Scope;\n}\n\nGot:\n' + _inspect(label)); | ||
throw new TypeError('Value of variable "label" violates contract.\n\nExpected:\nNodePath\n\nGot:\n' + _inspect(label)); | ||
} | ||
@@ -498,3 +498,3 @@ | ||
if (!Metadata(metadata)) { | ||
throw new TypeError('Value of variable "metadata" violates contract.\n\nExpected:\n{ indent: number;\n prefix: string;\n parentName: string;\n filename: string;\n dirname: string;\n basename: string;\n extname: string;\n hasStartMessage: bool;\n isStartMessage: bool;\n}\n\nGot:\n' + _inspect(metadata)); | ||
throw new TypeError('Value of variable "metadata" violates contract.\n\nExpected:\nMetadata\n\nGot:\n' + _inspect(metadata)); | ||
} | ||
@@ -510,3 +510,3 @@ | ||
if (!NodePath(item)) { | ||
throw new TypeError('Value of argument "item" violates contract.\n\nExpected:\n{ type: string;\n node: Node;\n scope: Scope;\n}\n\nGot:\n' + _inspect(item)); | ||
throw new TypeError('Value of argument "item" violates contract.\n\nExpected:\nNodePath\n\nGot:\n' + _inspect(item)); | ||
} | ||
@@ -518,3 +518,3 @@ | ||
if (!NodePath(statement)) { | ||
throw new TypeError('Value of argument "statement" violates contract.\n\nExpected:\n{ type: string;\n node: Node;\n scope: Scope;\n}\n\nGot:\n' + _inspect(statement)); | ||
throw new TypeError('Value of argument "statement" violates contract.\n\nExpected:\nNodePath\n\nGot:\n' + _inspect(statement)); | ||
} | ||
@@ -539,3 +539,3 @@ | ||
if (!Message(message)) { | ||
throw new TypeError('Value of variable "message" violates contract.\n\nExpected:\n{ prefix: Literal;\n indent: Literal;\n parentName: Literal;\n filename: Literal;\n dirname: Literal;\n basename: Literal;\n extname: Literal;\n content: Node;\n}\n\nGot:\n' + _inspect(message)); | ||
throw new TypeError('Value of variable "message" violates contract.\n\nExpected:\nMessage\n\nGot:\n' + _inspect(message)); | ||
} | ||
@@ -567,5 +567,3 @@ | ||
function _typeof(obj) { return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; } | ||
var Plugin = (function () { | ||
var Plugin = function () { | ||
function Plugin(input) { | ||
@@ -582,5 +580,5 @@ return input != null && Visitors(input.visitor); | ||
return Plugin; | ||
})(); | ||
}(); | ||
var PluginParams = (function () { | ||
var PluginParams = function () { | ||
function PluginParams(input) { | ||
@@ -597,5 +595,5 @@ return input != null && input.types instanceof Object && typeof input.template === 'function'; | ||
return PluginParams; | ||
})(); | ||
}(); | ||
var PluginOptions = (function () { | ||
var PluginOptions = function () { | ||
function PluginOptions(input) { | ||
@@ -618,5 +616,5 @@ return input != null && (input.aliases === undefined || input.aliases != null && _typeof(input.aliases) === 'object' && Object.keys(input.aliases).every(function (key) { | ||
return PluginOptions; | ||
})(); | ||
}(); | ||
var Visitors = (function () { | ||
var Visitors = function () { | ||
function Visitors(input) { | ||
@@ -636,5 +634,5 @@ return input != null && (typeof input === 'undefined' ? 'undefined' : _typeof(input)) === 'object' && Object.keys(input).every(function (key) { | ||
return Visitors; | ||
})(); | ||
}(); | ||
var Template = (function () { | ||
var Template = function () { | ||
function Template(input) { | ||
@@ -651,5 +649,5 @@ return typeof input === 'function'; | ||
return Template; | ||
})(); | ||
}(); | ||
var TemplateIds = (function () { | ||
var TemplateIds = function () { | ||
function TemplateIds(input) { | ||
@@ -669,5 +667,5 @@ return input != null && (typeof input === 'undefined' ? 'undefined' : _typeof(input)) === 'object' && Object.keys(input).every(function (key) { | ||
return TemplateIds; | ||
})(); | ||
}(); | ||
var Visitor = (function () { | ||
var Visitor = function () { | ||
function Visitor(input) { | ||
@@ -684,5 +682,5 @@ return typeof input === 'function'; | ||
return Visitor; | ||
})(); | ||
}(); | ||
var Node = (function () { | ||
var Node = function () { | ||
function Node(input) { | ||
@@ -699,5 +697,5 @@ return input != null && typeof input.type === 'string' && (input.node === undefined || input.node == null); | ||
return Node; | ||
})(); | ||
}(); | ||
var Literal = (function () { | ||
var Literal = function () { | ||
function Literal(input) { | ||
@@ -714,5 +712,5 @@ return input != null && (input.type === 'StringLiteral' || input.type === 'BooleanLiteral' || input.type === 'NumericLiteral' || input.type === 'NullLiteral' || input.type === 'RegExpLiteral'); | ||
return Literal; | ||
})(); | ||
}(); | ||
var Identifier = (function () { | ||
var Identifier = function () { | ||
function Identifier(input) { | ||
@@ -729,5 +727,5 @@ return input != null && typeof input.type === 'string' && typeof input.name === 'string'; | ||
return Identifier; | ||
})(); | ||
}(); | ||
var Scope = (function () { | ||
var Scope = function () { | ||
function Scope(input) { | ||
@@ -744,5 +742,5 @@ return input != null && (typeof input === 'undefined' ? 'undefined' : _typeof(input)) === 'object'; | ||
return Scope; | ||
})(); | ||
}(); | ||
var NodePath = (function () { | ||
var NodePath = function () { | ||
function NodePath(input) { | ||
@@ -759,5 +757,5 @@ return input != null && typeof input.type === 'string' && Node(input.node) && Scope(input.scope); | ||
return NodePath; | ||
})(); | ||
}(); | ||
var Metadata = (function () { | ||
var Metadata = function () { | ||
function Metadata(input) { | ||
@@ -774,5 +772,5 @@ return input != null && typeof input.indent === 'number' && typeof input.prefix === 'string' && typeof input.parentName === 'string' && typeof input.filename === 'string' && typeof input.dirname === 'string' && typeof input.basename === 'string' && typeof input.extname === 'string' && typeof input.hasStartMessage === 'boolean' && typeof input.isStartMessage === 'boolean'; | ||
return Metadata; | ||
})(); | ||
}(); | ||
var Message = (function () { | ||
var Message = function () { | ||
function Message(input) { | ||
@@ -789,3 +787,3 @@ return input != null && Literal(input.prefix) && Literal(input.indent) && Literal(input.parentName) && Literal(input.filename) && Literal(input.dirname) && Literal(input.basename) && Literal(input.extname) && Node(input.content); | ||
return Message; | ||
})(); | ||
}(); | ||
@@ -799,3 +797,12 @@ var $handled = Symbol('handled'); | ||
function _inspect(input) { | ||
function _inspect(input, depth) { | ||
var maxDepth = 4; | ||
var maxKeys = 15; | ||
if (depth === undefined) { | ||
depth = 0; | ||
} | ||
depth += 1; | ||
if (input === null) { | ||
@@ -809,10 +816,14 @@ return 'null'; | ||
if (input.length > 0) { | ||
var first = _inspect(input[0]); | ||
if (depth > maxDepth) return '[...]'; | ||
var first = _inspect(input[0], depth); | ||
if (input.every(function (item) { | ||
return _inspect(item) === first; | ||
return _inspect(item, depth) === first; | ||
})) { | ||
return first.trim() + '[]'; | ||
} else { | ||
return '[' + input.map(_inspect).join(', ') + ']'; | ||
return '[' + input.slice(0, maxKeys).map(function (item) { | ||
return _inspect(item, depth); | ||
}).join(', ') + (input.length >= maxKeys ? ', ...' : '') + ']'; | ||
} | ||
@@ -833,12 +844,20 @@ } else { | ||
var entries = keys.map(function (key) { | ||
return (/^([A-Z_$][A-Z0-9_$]*)$/i.test(key) ? key : JSON.stringify(key)) + ': ' + _inspect(input[key]) + ';'; | ||
}).join('\n '); | ||
if (depth > maxDepth) return '{...}'; | ||
var _indent = ' '.repeat(depth - 1); | ||
var entries = keys.slice(0, maxKeys).map(function (key) { | ||
return (/^([A-Z_$][A-Z0-9_$]*)$/i.test(key) ? key : JSON.stringify(key)) + ': ' + _inspect(input[key], depth) + ';'; | ||
}).join('\n ' + _indent); | ||
if (keys.length >= maxKeys) { | ||
entries += '\n ' + _indent + '...'; | ||
} | ||
if (input.constructor && input.constructor.name && input.constructor.name !== 'Object') { | ||
return input.constructor.name + ' {\n ' + entries + '\n}'; | ||
return input.constructor.name + ' {\n ' + _indent + entries + '\n' + _indent + '}'; | ||
} else { | ||
return '{ ' + entries + '\n}'; | ||
return '{\n ' + _indent + entries + '\n' + _indent + '}'; | ||
} | ||
} | ||
} |
{ | ||
"name": "babel-plugin-trace", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Super convenient syntax for execution tracing, logging and debugging JavaScript applications via a babel plugin.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -1,2 +0,2 @@ | ||
# Babel Contracts | ||
# Babel Plugin: Trace | ||
@@ -9,41 +9,10 @@ This is a [Babel](https://babeljs.io/) plugin which adds a straightforward, declarative syntax for adding debug logging to JavaScript applications. | ||
It's common to insert `console.log()` statements to help keep track of the internal state of functions when writing tricky pieces of code. | ||
It's common to insert `console.log()` statements to help keep track of the internal state of functions when writing tricky pieces of code. During development this is very useful, but it creates a lot of noise in the console, and when development of that particular piece of code is complete, the developer is likely to delete the `console.log()` calls. If we're lucky, they might leave comments in their place. | ||
A simplified version looks like this: | ||
This is a tragedy - that logging information is extremely useful, not only is it helpful when fixing bugs, it's a great assistance for new developers (including yourself, 6 months from now) when getting to know a codebase. | ||
```js | ||
// login.js | ||
This plugin repurposes JavaScript LabeledStatements like `log:` and `trace:` to provide a logging / tracing syntax which can be selectively enabled or disabled at the folder, file, or function level at build time. Normally, these labels are only used as targets for labeled `break` and `continue` statements. | ||
async function authenticate (username, password) { | ||
console.log('authenticating user', username); | ||
const user = await db.select().from('users').where({username: username}); | ||
if (!user) { | ||
console.log('no such user'); | ||
return false; | ||
} | ||
else if (!user.checkPassword(password)) { | ||
console.log('invalid password'); | ||
return false; | ||
} | ||
else if (!user.isActive) { | ||
console.log('user is not active'); | ||
return false; | ||
} | ||
console.log('logging user', username, 'into the site'); | ||
return true; | ||
} | ||
``` | ||
When disabled in production the logging statements are completely dropped out, incurring no overhead. The syntax looks like this: | ||
During development this is very useful, but it creates a lot of noise in the console, and when development of that particular piece of code is complete, | ||
the developer is likely to delete the `console.log()` calls. If we're lucky, they might leave comments in their place. | ||
But this is a tragedy - that logging information is extremely useful, not only is it helpful when fixing bugs, it's a great assistance for new developers | ||
(including yourself, 6 months from now) when getting to know a codebase. | ||
This plugin repurposes JavaScript `LabeledStatements` to provide a logging / tracing syntax which can be selectively enabled or disabled at the folder, file, or function level at build time. | ||
When disabled in production it incurs no overhead. | ||
The syntax looks like this: | ||
```js | ||
@@ -85,4 +54,5 @@ // login.js | ||
# Installation | ||
# Installation & Configuration | ||
Install via [npm](https://npmjs.org/package/babel-plugin-trace). | ||
@@ -95,9 +65,9 @@ ```sh | ||
{ | ||
"plugins": [["trace", { | ||
"env": { | ||
"production": { | ||
"strip": true | ||
"plugins": [ | ||
["trace", { | ||
"env": { | ||
"production": { "strip": true } | ||
} | ||
} | ||
}]] | ||
}] | ||
] | ||
} | ||
@@ -108,14 +78,26 @@ ``` | ||
Alternatively, you may wish to disable tracing all of the time, and enable it for certain files or functions only. | ||
To disable tracing all of the time, use this in your `.babelrc`: | ||
Alternatively, you may wish to disable all tracing by default, enabling it only for certain files or functions using environment variables: | ||
```json | ||
{ | ||
"plugins": [["trace", { | ||
"strip": true | ||
}]] | ||
"plugins": [ | ||
["trace", { "strip": true }] | ||
] | ||
} | ||
``` | ||
### Enable by filename | ||
# Environment Variables | ||
### `TRACE_LEVEL` - Enable only specific logging levels | ||
Log only `warn` statements. | ||
``` | ||
TRACE_LEVEL=warn babel -d ./lib ./src | ||
``` | ||
Log `trace` and `warn` statements. | ||
``` | ||
TRACE_LEVEL=trace,warn babel -d ./lib ./src | ||
``` | ||
### `TRACE_FILE` - Enable by filename | ||
Enable logging for any file with `login.js` in the path. | ||
@@ -131,3 +113,3 @@ ``` | ||
### Enable for specific functions | ||
### `TRACE_CONTEXT` - Enable for specific functions | ||
Enable logging for any function called `login()` or `logout()`. | ||
@@ -143,15 +125,3 @@ ``` | ||
### Enable only specific logging levels | ||
Log only `warn` statements. | ||
``` | ||
TRACE_LEVEL=warn babel -d ./lib ./src | ||
``` | ||
Log `trace` and `warn` statements. | ||
``` | ||
TRACE_LEVEL=trace,warn babel -d ./lib ./src | ||
``` | ||
# License | ||
@@ -158,0 +128,0 @@ |
@@ -152,5 +152,5 @@ import fspath from "path"; | ||
else { | ||
return expression(`console.log(prefix, content)`)({ | ||
prefix: t.stringLiteral(prefix), | ||
content: message.content | ||
return expression(`console.log(PREFIX, CONTENT)`)({ | ||
PREFIX: t.stringLiteral(prefix), | ||
CONTENT: message.content | ||
}); | ||
@@ -157,0 +157,0 @@ } |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
1146
47442
15
125
2