From 16dc9fc046ea018198fcc42ac42d6c5da4bad058 Mon Sep 17 00:00:00 2001 From: Jose Luis Represa Date: Mon, 15 Jan 2018 16:32:12 +0100 Subject: [PATCH 1/3] use CallExpression --- src/index.js | 2 +- src/lib/utils/builder.js | 2 +- src/lib/utils/macros.js | 6 ++---- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/index.js b/src/index.js index 8965d23..53a2f19 100644 --- a/src/index.js +++ b/src/index.js @@ -45,7 +45,7 @@ function macros(babel) { } }, - ExpressionStatement(path) { + CallExpression(path) { this.macroBuilder.build(path); } } diff --git a/src/lib/utils/builder.js b/src/lib/utils/builder.js index 6d612d2..a519d61 100644 --- a/src/lib/utils/builder.js +++ b/src/lib/utils/builder.js @@ -85,7 +85,7 @@ module.exports = class Builder { let options = _options || {}; let t = this.t; - let expression = path.node.expression; + let expression = path.node; let callee = expression.callee; let args = expression.arguments; diff --git a/src/lib/utils/macros.js b/src/lib/utils/macros.js index 2a6288a..b05aa83 100644 --- a/src/lib/utils/macros.js +++ b/src/lib/utils/macros.js @@ -132,10 +132,8 @@ module.exports = class Macros { * Builds the expressions that the CallExpression will expand into. */ build(path) { - let expression = path.node.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; + if (this.localDebugBindings.some((b) => b.node.name === path.node.callee.name)) { + let imported = path.scope.getBinding(path.node.callee.name).path.node.imported.name; this.builder[`${imported}`](path); } } From 7c4f776389a723069f8fec0cfc902ee1ca961747 Mon Sep 17 00:00:00 2001 From: Jose Luis Represa Date: Tue, 16 Jan 2018 10:01:20 +0100 Subject: [PATCH 2/3] unit tests --- fixtures/debug-expansion-arrow/expectation.js | 25 +++++++++++ fixtures/debug-expansion-arrow/sample.js | 26 +++++++++++ .../debug-expansion-return/expectation.js | 41 +++++++++++++++++ fixtures/debug-expansion-return/sample.js | 42 ++++++++++++++++++ fixtures/warn-expansion/sample.js | 2 +- src/tests/debug-tools-test.js | 44 +++++++++++++++++++ 6 files changed, 179 insertions(+), 1 deletion(-) create mode 100644 fixtures/debug-expansion-arrow/expectation.js create mode 100644 fixtures/debug-expansion-arrow/sample.js create mode 100644 fixtures/debug-expansion-return/expectation.js create mode 100644 fixtures/debug-expansion-return/sample.js 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/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, From 7a75a64f84597d2fd988c398a4cf476e08264efb Mon Sep 17 00:00:00 2001 From: Jose Luis Represa Date: Tue, 16 Jan 2018 10:02:26 +0100 Subject: [PATCH 3/3] add ArrowFunctionExpression and ReturnStatement --- src/index.js | 20 ++++++++++++++++++-- src/lib/utils/builder.js | 19 +++++++++---------- src/lib/utils/macros.js | 8 ++++---- 3 files changed, 31 insertions(+), 16 deletions(-) diff --git a/src/index.js b/src/index.js index 53a2f19..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; @@ -45,8 +53,16 @@ function macros(babel) { } }, - CallExpression(path) { - this.macroBuilder.build(path); + ExpressionStatement(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 a519d61..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; 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 b05aa83..e8a28a9 100644 --- a/src/lib/utils/macros.js +++ b/src/lib/utils/macros.js @@ -131,10 +131,10 @@ module.exports = class Macros { /** * Builds the expressions that the CallExpression will expand into. */ - build(path) { - if (this.localDebugBindings.some((b) => b.node.name === path.node.callee.name)) { - let imported = path.scope.getBinding(path.node.callee.name).path.node.imported.name; - this.builder[`${imported}`](path); + 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, expression); } }