diff --git a/fixtures/debug-expansion-arrow/expectation.js b/fixtures/debug-expansion-arrow/expectation.js new file mode 100644 index 0000000..7a8f8e4 --- /dev/null +++ b/fixtures/debug-expansion-arrow/expectation.js @@ -0,0 +1,25 @@ + + +(() => (true && !('assert') && console.assert('assert')))(); +const assert1 = () => (true && !('assert') && console.assert('assert')); +const assert2 = () => { + return (true && !('assert') && console.assert('assert')); +}; + +(() => (true && console.warn('warn')))(); +const warn1 = () => (true && console.warn('warn')); +const warn2 = () => { + return (true && console.warn('warn')); +}; + +(() => (true && console.log('log')))(); +const log1 = () => (true && console.log('log')); +const log2 = () => { + return (true && console.log('log')); +}; + +(() => (true && !(false) && console.warn('deprecate')))(); +const deprecate1 = () => (true && !(false) && console.warn('deprecate')); +const deprecate2 = () => { + return (true && !(false) && console.warn('deprecate')); +}; \ No newline at end of file diff --git a/fixtures/debug-expansion-arrow/sample.js b/fixtures/debug-expansion-arrow/sample.js new file mode 100644 index 0000000..03bdee7 --- /dev/null +++ b/fixtures/debug-expansion-arrow/sample.js @@ -0,0 +1,26 @@ +import { DEBUG } from '@ember/env-flags'; +import { assert, warn, log, deprecate } from '@ember/debug-tools'; + +(() => assert('assert'))(); +const assert1 = () => assert('assert'); +const assert2 = () => { + return assert('assert'); +}; + +(() => warn('warn'))(); +const warn1 = () => warn('warn'); +const warn2 = () => { + return warn('warn'); +}; + +(() => log('log'))(); +const log1 = () => log('log'); +const log2 = () => { + return log('log'); +}; + +(() => deprecate('deprecate'))(); +const deprecate1 = () => deprecate('deprecate'); +const deprecate2 = () => { + return deprecate('deprecate'); +}; diff --git a/fixtures/debug-expansion-return/expectation.js b/fixtures/debug-expansion-return/expectation.js new file mode 100644 index 0000000..ad7a469 --- /dev/null +++ b/fixtures/debug-expansion-return/expectation.js @@ -0,0 +1,41 @@ + + +(function () { + return (true && !('assert') && console.assert('assert')); +})(); +const assert1 = function () { + return (true && !('assert') && console.assert('assert')); +}; +function assert2() { + return (true && !('assert') && console.assert('assert')); +} + +(function () { + return (true && console.warn('warn')); +})(); +const warn1 = function () { + return (true && console.warn('warn')); +}; +function warn2() { + return (true && console.warn('warn')); +} + +(function () { + return (true && console.log('log')); +})(); +const log1 = function () { + return (true && console.log('log')); +}; +function log2() { + return (true && console.log('log')); +} + +(function () { + return (true && !(false) && console.warn('deprecate')); +})(); +const deprecate1 = function () { + return (true && !(false) && console.warn('deprecate')); +}; +function deprecate2() { + return (true && !(false) && console.warn('deprecate')); +} \ No newline at end of file diff --git a/fixtures/debug-expansion-return/sample.js b/fixtures/debug-expansion-return/sample.js new file mode 100644 index 0000000..f59affc --- /dev/null +++ b/fixtures/debug-expansion-return/sample.js @@ -0,0 +1,42 @@ +import { DEBUG } from '@ember/env-flags'; +import { assert, warn, log, deprecate } from '@ember/debug-tools'; + +(function() { + return assert('assert'); +})(); +const assert1 = function() { + return assert('assert'); +}; +function assert2() { + return assert('assert'); +} + +(function(){ + return warn('warn'); +})(); +const warn1 = function() { + return warn('warn'); +}; +function warn2() { + return warn('warn'); +} + +(function(){ + return log('log'); +})(); +const log1 = function() { + return log('log'); +}; +function log2() { + return log('log'); +} + +(function(){ + return deprecate('deprecate'); +})(); +const deprecate1 = function() { + return deprecate('deprecate'); +}; +function deprecate2() { + return deprecate('deprecate'); +} diff --git a/fixtures/warn-expansion/sample.js b/fixtures/warn-expansion/sample.js index 0b634d4..37979f8 100644 --- a/fixtures/warn-expansion/sample.js +++ b/fixtures/warn-expansion/sample.js @@ -1,4 +1,4 @@ import { DEBUG } from '@ember/env-flags'; import { warn } from '@ember/debug-tools'; -warn('This is a warning'); \ No newline at end of file +warn('This is a warning'); diff --git a/src/index.js b/src/index.js index 8965d23..814ab4d 100644 --- a/src/index.js +++ b/src/index.js @@ -3,6 +3,14 @@ const Macros = require('./lib/utils/macros'); const normalizeOptions = require('./lib/utils/normalize-options').normalizeOptions; +const updateCallExpression = { + CallExpression(path) { + if (this.parent.node === path.parent) { + this.plugin.macroBuilder.build(path, path.node); + } + } +}; + function macros(babel) { const t = babel.types; @@ -46,7 +54,15 @@ function macros(babel) { }, ExpressionStatement(path) { - this.macroBuilder.build(path); + this.macroBuilder.build(path, path.node.expression); + }, + + ArrowFunctionExpression(path) { + path.traverse(updateCallExpression, { parent: path, plugin: this }); + }, + + ReturnStatement(path) { + path.traverse(updateCallExpression, { parent: path, plugin: this }); } } }; diff --git a/src/lib/utils/builder.js b/src/lib/utils/builder.js index 6d612d2..2ca2c85 100644 --- a/src/lib/utils/builder.js +++ b/src/lib/utils/builder.js @@ -26,7 +26,7 @@ module.exports = class Builder { * * ($DEBUG && $GLOBAL_NS.assert($PREDICATE, $MESSAGE)); */ - assert(path) { + assert(path, expression) { let predicate; if (this.assertPredicateIndex !== undefined) { predicate = (expression, args) => { @@ -34,7 +34,7 @@ module.exports = class Builder { }; } - this._createMacroExpression(path, { + this._createMacroExpression(path, expression, { predicate }); } @@ -56,8 +56,8 @@ module.exports = class Builder { * * ($DEBUG && $GLOBAL_NS.warn($MESSAGE)); */ - warn(path) { - this._createMacroExpression(path); + warn(path, expression) { + this._createMacroExpression(path, expression); } /** @@ -77,15 +77,14 @@ module.exports = class Builder { * * ($DEBUG && $GLOBAL_NS.log($MESSAGE)); */ - log(path) { - this._createMacroExpression(path); + log(path, expression) { + this._createMacroExpression(path, expression); } - _createMacroExpression(path, _options) { + _createMacroExpression(path, expression, _options) { let options = _options || {}; let t = this.t; - let expression = path.node.expression; let callee = expression.callee; let args = expression.arguments; @@ -141,8 +140,8 @@ module.exports = class Builder { * * ($DEBUG && $PREDICATE && $GLOBAL_NS.deprecate($MESSAGE, $PREDICATE, { $ID, $URL, $UNTIL })); */ - deprecate(path) { - this._createMacroExpression(path, { + deprecate(path, expression) { + this._createMacroExpression(path, expression, { predicate: (expression, args) => args[1], buildConsoleAPI: (expression, args) => { diff --git a/src/lib/utils/macros.js b/src/lib/utils/macros.js index 2a6288a..e8a28a9 100644 --- a/src/lib/utils/macros.js +++ b/src/lib/utils/macros.js @@ -131,12 +131,10 @@ module.exports = class Macros { /** * Builds the expressions that the CallExpression will expand into. */ - build(path) { - let expression = path.node.expression; - + build(path, expression) { if (this.builder.t.isCallExpression(expression) && this.localDebugBindings.some((b) => b.node.name === expression.callee.name)) { let imported = path.scope.getBinding(expression.callee.name).path.node.imported.name; - this.builder[`${imported}`](path); + this.builder[`${imported}`](path, expression); } } diff --git a/src/tests/debug-tools-test.js b/src/tests/debug-tools-test.js index 5cce474..3f4e72e 100644 --- a/src/tests/debug-tools-test.js +++ b/src/tests/debug-tools-test.js @@ -67,6 +67,50 @@ describe('Debug Macros', function(){ h.generateTest('log-expansion'); }); +describe('Debug Macros with return statement', function(){ + let h = transformTestHelper({ + presets, + plugins: [ + [DebugToolsPlugin, { + debugTools: { + source: '@ember/debug-tools', + assertPredicateIndex: 0 + }, + envFlags: { + source: '@ember/env-flags', + flags: { + DEBUG: true + } + } + }] + ] + }); + + h.generateTest('debug-expansion-return'); +}); + +describe('Debug Macros with arrow expression', function(){ + let h = transformTestHelper({ + presets, + plugins: [ + [DebugToolsPlugin, { + debugTools: { + source: '@ember/debug-tools', + assertPredicateIndex: 0 + }, + envFlags: { + source: '@ember/env-flags', + flags: { + DEBUG: true + } + } + }] + ] + }); + + h.generateTest('debug-expansion-arrow'); +}); + describe('foreign debug imports', function() { let h = transformTestHelper({ presets,