babel-plugin-debug-macros
Advanced tools
Comparing version 0.1.7 to 0.1.8
@@ -14,8 +14,9 @@ 'use strict'; | ||
var Builder = function () { | ||
function Builder(t, module, global) { | ||
function Builder(t, options) { | ||
_classCallCheck(this, Builder); | ||
this.t = t; | ||
this.module = module; | ||
this.global = global; | ||
this.module = options.module; | ||
this.global = options.global; | ||
this.assertPredicateIndex = options.assertPredicateIndex; | ||
this.expressions = []; | ||
@@ -33,9 +34,21 @@ } | ||
* | ||
* or | ||
* or (when `assertPredicateIndex` specified) | ||
* | ||
* ($DEBUG && $PREDICATE && console.assert(false, $MESSAGE)); | ||
* | ||
* or (`{ externalizeHelpers: { module: true } }`) | ||
* | ||
* ($DEBUG && assert($PREDICATE, $MESSAGE)); | ||
* | ||
* or | ||
* or (when `{ externalizeHelpers: { module: true }, debugTools: { source: '...', assertPredicateIndex: 0 } }` specified) | ||
* | ||
* ($DEBUG && $PREDICATE && assert(false, $MESSAGE)); | ||
* | ||
* or (when `{ externalizeHelpers: { global: '$GLOBLA_NS' }` specified) | ||
* | ||
* ($DEBUG && $GLOBAL_NS.assert($PREDICATE, $MESSAGE)); | ||
* | ||
* or (when `{ externalizeHelpers: { global: '$GLOBLA_NS' }, debugTools: { source: '...', assertPredicateIndex: 0 } }` specified) | ||
* | ||
* ($DEBUG && $PREDICATE && $GLOBAL_NS.assert(false, $MESSAGE)); | ||
*/ | ||
@@ -47,3 +60,17 @@ | ||
value: function assert(path) { | ||
this._createMacroExpression(path); | ||
var _this = this; | ||
var predicate = void 0; | ||
if (this.assertPredicateIndex !== undefined) { | ||
predicate = function predicate(expression, args) { | ||
var predicate = args[_this.assertPredicateIndex]; | ||
args[_this.assertPredicateIndex] = _this.t.identifier('false'); | ||
return predicate; | ||
}; | ||
} | ||
this._createMacroExpression(path, { | ||
predicate | ||
}); | ||
} | ||
@@ -100,3 +127,5 @@ | ||
key: '_createMacroExpression', | ||
value: function _createMacroExpression(path) { | ||
value: function _createMacroExpression(path, _options) { | ||
var options = _options || {}; | ||
var t = this.t, | ||
@@ -112,2 +141,6 @@ module = this.module, | ||
if (options.validate) { | ||
options.validate(expression, args); | ||
} | ||
if (module || global) { | ||
@@ -119,8 +152,18 @@ if (global) { | ||
} | ||
} else if (options.buildConsoleAPI) { | ||
callExpression = options.buildConsoleAPI(expression, args); | ||
} else { | ||
callExpression = this._createConsoleAPI(callee, args); | ||
callExpression = this._createConsoleAPI(options.consoleAPI || callee, args); | ||
} | ||
var identifiers = this._getIdentifiers(args); | ||
this.expressions.push([path, this._buildLogicalExpressions([], callExpression)]); | ||
var prefixedIdentifiers = []; | ||
if (options.predicate) { | ||
var predicate = options.predicate(expression, args); | ||
var negatedPredicate = t.unaryExpression('!', t.parenthesizedExpression(predicate)); | ||
prefixedIdentifiers.push(negatedPredicate); | ||
} | ||
this.expressions.push([path, this._buildLogicalExpressions(prefixedIdentifiers, callExpression)]); | ||
} | ||
@@ -147,7 +190,7 @@ | ||
* | ||
* ($DEBUG && $PREDICATE && deprecate($MESSAGE, $PREDICATE, { $ID, $URL, $UNTIL })); | ||
* ($DEBUG && $PREDICATE && deprecate($MESSAGE, false, { $ID, $URL, $UNTIL })); | ||
* | ||
* or | ||
* | ||
* ($DEBUG && $PREDICATE && $GLOBAL_NS.deprecate($MESSAGE, $PREDICATE, { $ID, $URL, $UNTIL })); | ||
* ($DEBUG && $PREDICATE && $GLOBAL_NS.deprecate($MESSAGE, false, { $ID, $URL, $UNTIL })); | ||
*/ | ||
@@ -158,39 +201,38 @@ | ||
value: function deprecate(path) { | ||
var t = this.t, | ||
module = this.module, | ||
global = this.global; | ||
var _this2 = this; | ||
var expression = path.node.expression; | ||
var callee = expression.callee; | ||
var args = expression.arguments; | ||
this._createMacroExpression(path, { | ||
predicate: function predicate(expression, args) { | ||
var _args = _slicedToArray(args, 2), | ||
predicate = _args[1]; | ||
var _args = _slicedToArray(args, 3), | ||
message = _args[0], | ||
predicate = _args[1], | ||
meta = _args[2]; | ||
args[1] = _this2.t.identifier('false'); | ||
if (meta && meta.properties && !meta.properties.some(function (prop) { | ||
return prop.key.name === 'id'; | ||
})) { | ||
throw new ReferenceError(`deprecate's meta information requires an "id" field.`); | ||
} | ||
return predicate; | ||
}, | ||
if (meta && meta.properties && !meta.properties.some(function (prop) { | ||
return prop.key.name === 'until'; | ||
})) { | ||
throw new ReferenceError(`deprecate's meta information requires an "until" field.`); | ||
} | ||
buildConsoleAPI: function buildConsoleAPI(expression, args) { | ||
var _args2 = _slicedToArray(args, 1), | ||
message = _args2[0]; | ||
var deprecate = void 0; | ||
if (module || global) { | ||
if (global) { | ||
deprecate = this._createGlobalExternalHelper(callee, args, global); | ||
} else { | ||
deprecate = expression; | ||
return _this2._createConsoleAPI(_this2.t.identifier('warn'), [message]); | ||
}, | ||
validate: function validate(expression, args) { | ||
var _args3 = _slicedToArray(args, 3), | ||
meta = _args3[2]; | ||
if (meta && meta.properties && !meta.properties.some(function (prop) { | ||
return prop.key.name === 'id'; | ||
})) { | ||
throw new ReferenceError(`deprecate's meta information requires an "id" field.`); | ||
} | ||
if (meta && meta.properties && !meta.properties.some(function (prop) { | ||
return prop.key.name === 'until'; | ||
})) { | ||
throw new ReferenceError(`deprecate's meta information requires an "until" field.`); | ||
} | ||
} | ||
} else { | ||
deprecate = this._createConsoleAPI(t.identifier('warn'), [message]); | ||
} | ||
this.expressions.push([path, this._buildLogicalExpressions([t.unaryExpression('!', t.parenthesizedExpression(predicate))], deprecate)]); | ||
}); | ||
} | ||
@@ -219,6 +261,6 @@ | ||
value: function _getIdentifiers(args) { | ||
var _this = this; | ||
var _this3 = this; | ||
return args.filter(function (arg) { | ||
return _this.t.isIdentifier(arg); | ||
return _this3.t.isIdentifier(arg); | ||
}); | ||
@@ -225,0 +267,0 @@ } |
@@ -38,7 +38,7 @@ 'use strict'; | ||
this.debugHelpers = options.externalizeHelpers || {}; | ||
var _debugHelpers = this.debugHelpers, | ||
module = _debugHelpers.module, | ||
global = _debugHelpers.global; | ||
this.builder = new _builder2.default(t, module, global); | ||
this.builder = new _builder2.default(t, { | ||
module: this.debugHelpers.module, | ||
global: this.debugHelpers.global, | ||
assertPredicateIndex: options.debugTools.assertPredicateIndex | ||
}); | ||
} | ||
@@ -45,0 +45,0 @@ |
@@ -61,7 +61,6 @@ 'use strict'; | ||
var debugToolsImport = void 0; | ||
if (debugTools) { | ||
debugToolsImport = debugTools.source; | ||
} | ||
var debugToolsImport = debugTools.source, | ||
assertPredicateIndex = debugTools.assertPredicateIndex; | ||
var envFlagsImport = void 0; | ||
@@ -92,5 +91,6 @@ var _envFlags = {}; | ||
debugTools: { | ||
debugToolsImport | ||
debugToolsImport, | ||
assertPredicateIndex | ||
} | ||
}; | ||
} |
@@ -13,3 +13,3 @@ { | ||
"name": "babel-plugin-debug-macros", | ||
"version": "0.1.7", | ||
"version": "0.1.8", | ||
"description": "Debug macros and feature flag stripping", | ||
@@ -16,0 +16,0 @@ "main": "dist/index.js", |
@@ -7,3 +7,3 @@ # Babel Debug Macros And Feature Flags | ||
The plugin takes 5 types options: `envFlags`, `features`, `debugTools`, `externalizeHelpers` and `svelte`. The `importSpecifier` is used as a hint to this plugin as to where macros are being imported and completely configurable by the host. Like Babel you can supply you're own helpers using the `externalizeHelpers` options. | ||
The plugin takes 5 types options: `envFlags`, `features`, `debugTools`, `externalizeHelpers` and `svelte`. The `importSpecifier` is used as a hint to this plugin as to where macros are being imported and completely configurable by the host. Like Babel you can supply your own helpers using the `externalizeHelpers` options. | ||
@@ -21,3 +21,5 @@ ``` | ||
debugTools: { | ||
source: 'debug-tools' | ||
source: 'debug-tools', | ||
// @optional | ||
assertPredicateIndex: 0 | ||
}, | ||
@@ -44,3 +46,3 @@ // @optional | ||
Flags and features are inlined into consuming module so that something like UglifyJS with DCE them when they are unreachable. | ||
Flags and features are inlined into the consuming module so that something like UglifyJS will DCE them when they are unreachable. | ||
@@ -100,2 +102,7 @@ ## Simple environment and fetaure flags | ||
The `assert` macro can expand in a more intelligent way with the correct | ||
configuration. When `babel-plugin-debug-macros` is provided with the | ||
`assertPredicateIndex` the predicate is injected in front of the assertion | ||
in order to avoid costly assertion message generation when not needed. | ||
```javascript | ||
@@ -109,6 +116,11 @@ import { assert } from 'debug-tools'; | ||
Expands into: | ||
With the `debugTools: { assertPredicateIndex: 0 }` configuration the following expansion is done: | ||
```js | ||
(true && !((() => { return 1 === 1;})()) && console.assert(false, 'this is a warning')); | ||
``` | ||
When `assertPredicateIndex` is not specified, the following expansion is done: | ||
```javascript | ||
(true && console.assert((() => { return 1 === 1;})(), 'this is a warning')); | ||
@@ -137,3 +149,3 @@ ``` | ||
When you externalize helpers you must provide runtime implementations for the above macros. An expansion will still occur however we will use emit references to those runtime helpers. | ||
When you externalize helpers you must provide runtime implementations for the above macros. An expansion will still occur, however we will emit references to those runtime helpers. | ||
@@ -170,5 +182,5 @@ A global expansion looks like the following: | ||
Svelte allows for consumers to opt into stripping deprecated code from your dependecies. By adding a package name and minimum version that contains no deprecations that code will be compiled away. | ||
Svelte allows for consumers to opt into stripping deprecated code from your dependecies. By adding a package name and minimum version that contains no deprecations, that code will be compiled away. | ||
For example, consider you are on `ember-source@2.10.0` and you have no deprecations all deprecated code in `ember-source` that is `<=2.10.0` will be removed. | ||
For example, consider you are on `ember-source@2.10.0` and you have no deprecations. All deprecated code in `ember-source` that is `<=2.10.0` will be removed. | ||
@@ -193,2 +205,2 @@ ``` | ||
As you may notice that we inject `DEBUG` into the code when we expand the macro. We gurantee that the binding is unique when injected and follow the local binding name if it is imported directly. | ||
As you may notice that we inject `DEBUG` into the code when we expand the macro. We guarantee that the binding is unique when injected and follow the local binding name if it is imported directly. |
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
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
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
27974
614
199
1