Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use call expression #56

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions fixtures/debug-expansion-arrow/expectation.js
Original file line number Diff line number Diff line change
@@ -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'));
};
26 changes: 26 additions & 0 deletions fixtures/debug-expansion-arrow/sample.js
Original file line number Diff line number Diff line change
@@ -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');
};
41 changes: 41 additions & 0 deletions fixtures/debug-expansion-return/expectation.js
Original file line number Diff line number Diff line change
@@ -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'));
}
42 changes: 42 additions & 0 deletions fixtures/debug-expansion-return/sample.js
Original file line number Diff line number Diff line change
@@ -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');
}
2 changes: 1 addition & 1 deletion fixtures/warn-expansion/sample.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DEBUG } from '@ember/env-flags';
import { warn } from '@ember/debug-tools';

warn('This is a warning');
warn('This is a warning');
18 changes: 17 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 });
}
}
};
Expand Down
19 changes: 9 additions & 10 deletions src/lib/utils/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ module.exports = class Builder {
*
* ($DEBUG && $GLOBAL_NS.assert($PREDICATE, $MESSAGE));
*/
assert(path) {
assert(path, expression) {
let predicate;
if (this.assertPredicateIndex !== undefined) {
predicate = (expression, args) => {
return args[this.assertPredicateIndex];
};
}

this._createMacroExpression(path, {
this._createMacroExpression(path, expression, {
predicate
});
}
Expand All @@ -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);
}

/**
Expand All @@ -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;

Expand Down Expand Up @@ -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) => {
Expand Down
6 changes: 2 additions & 4 deletions src/lib/utils/macros.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
44 changes: 44 additions & 0 deletions src/tests/debug-tools-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down