diff --git a/fixtures/assert-expansion/expectation6.js b/fixtures/assert-expansion/expectation6.js index 3aab006..345bb9c 100644 --- a/fixtures/assert-expansion/expectation6.js +++ b/fixtures/assert-expansion/expectation6.js @@ -1,2 +1,3 @@ (true && !((() => true)()) && console.assert((() => true)(), 'This is an assertion')); -(true && !(false) && console.assert(false, 'This is an assertion 2')); \ No newline at end of file +(true && !(false) && console.assert(false, 'This is an assertion 2')); +(true && !(false) && console.assert(false, 'renamed assertion')); diff --git a/fixtures/assert-expansion/expectation7.js b/fixtures/assert-expansion/expectation7.js index 3aab006..345bb9c 100644 --- a/fixtures/assert-expansion/expectation7.js +++ b/fixtures/assert-expansion/expectation7.js @@ -1,2 +1,3 @@ (true && !((() => true)()) && console.assert((() => true)(), 'This is an assertion')); -(true && !(false) && console.assert(false, 'This is an assertion 2')); \ No newline at end of file +(true && !(false) && console.assert(false, 'This is an assertion 2')); +(true && !(false) && console.assert(false, 'renamed assertion')); diff --git a/fixtures/assert-expansion/sample.js b/fixtures/assert-expansion/sample.js index c417594..423de62 100644 --- a/fixtures/assert-expansion/sample.js +++ b/fixtures/assert-expansion/sample.js @@ -1,5 +1,7 @@ import { DEBUG } from '@ember/env-flags'; import { assert } from '@ember/debug-tools'; +import { assert as debugAssert } from '@ember/debug-tools'; assert((() => true )(), 'This is an assertion'); -assert(false, 'This is an assertion 2'); \ No newline at end of file +assert(false, 'This is an assertion 2'); +debugAssert(false, 'renamed assertion'); diff --git a/fixtures/ember-cli-babel-config/expectation6.js b/fixtures/ember-cli-babel-config/expectation6.js index 8995e5d..47bd063 100644 --- a/fixtures/ember-cli-babel-config/expectation6.js +++ b/fixtures/ember-cli-babel-config/expectation6.js @@ -11,4 +11,15 @@ if (true /* DEBUG */) { id: 'donzo', until: '4.0.0', url: 'http://example.com' -})); \ No newline at end of file +})); + +// renamed imports + +(true && Ember.warn('This is a warning')); +(true && !(foo) && Ember.assert('Hahahaha', foo)); +(true && !(false) && Ember.assert('without predicate')); +(true && !(true) && Ember.deprecate('This thing is donzo', true, { + id: 'donzo', + until: '4.0.0', + url: 'http://example.com' +})); diff --git a/fixtures/ember-cli-babel-config/expectation7.js b/fixtures/ember-cli-babel-config/expectation7.js index 8b757cd..71ab2f9 100644 --- a/fixtures/ember-cli-babel-config/expectation7.js +++ b/fixtures/ember-cli-babel-config/expectation7.js @@ -11,4 +11,15 @@ if (true id: 'donzo', until: '4.0.0', url: 'http://example.com' -})); \ No newline at end of file +})); + +// renamed imports + +(true && Ember.warn('This is a warning')); +(true && !(foo) && Ember.assert('Hahahaha', foo)); +(true && !(false) && Ember.assert('without predicate')); +(true && !(true) && Ember.deprecate('This thing is donzo', true, { + id: 'donzo', + until: '4.0.0', + url: 'http://example.com' +})); // renamed imports diff --git a/fixtures/ember-cli-babel-config/sample.js b/fixtures/ember-cli-babel-config/sample.js index c291e51..7b6a14b 100644 --- a/fixtures/ember-cli-babel-config/sample.js +++ b/fixtures/ember-cli-babel-config/sample.js @@ -1,4 +1,4 @@ -import { warn, assert, deprecate } from '@ember/debug'; +import { warn, assert, deprecate, warn as debugWarn, assert as debugAssert, deprecate as debugDeprecate } from '@ember/debug'; import { DEBUG } from '@glimmer/env'; if (DEBUG) { @@ -15,3 +15,17 @@ deprecate('This thing is donzo', true, { until: '4.0.0', url: 'http://example.com' }); + +// renamed imports +debugWarn('This is a warning'); + +debugAssert('Hahahaha', foo); +debugAssert('without predicate'); + +debugDeprecate('This thing is donzo', true, { + id: 'donzo', + until: '4.0.0', + url: 'http://example.com' +}); + + diff --git a/src/utils/builder.js b/src/utils/builder.js index ea61084..4018132 100644 --- a/src/utils/builder.js +++ b/src/utils/builder.js @@ -27,7 +27,7 @@ module.exports = class Builder { * * ($DEBUG && $GLOBAL_NS.assert($PREDICATE, $MESSAGE)); */ - assert(path) { + assert(path, options) { let predicate; if (this.assertPredicateIndex !== undefined) { predicate = (expression, args) => { @@ -35,9 +35,15 @@ module.exports = class Builder { }; } - this._createMacroExpression(path, { - predicate, - }); + this._createMacroExpression( + path, + Object.assign( + { + predicate, + }, + options + ) + ); } /** @@ -102,7 +108,12 @@ module.exports = class Builder { } else if (options.buildConsoleAPI) { callExpression = options.buildConsoleAPI(expression, args); } else { - callExpression = this._createConsoleAPI(options.consoleAPI || callee, args); + callExpression = this._createConsoleAPI( + options.consoleAPI || + (options.importedName && t.identifier(options.importedName)) || + callee, + args + ); } let prefixedIdentifiers = []; diff --git a/src/utils/macros.js b/src/utils/macros.js index 46bdddc..9b929b9 100644 --- a/src/utils/macros.js +++ b/src/utils/macros.js @@ -34,7 +34,7 @@ module.exports = class Macros { collectDebugToolsSpecifiers(specifiers) { specifiers.forEach(specifier => { if (specifier.node.imported && SUPPORTED_MACROS.indexOf(specifier.node.imported.name) > -1) { - this.localDebugBindings.push(specifier.get('local')); + this.localDebugBindings.push(specifier); } }); } @@ -47,10 +47,15 @@ module.exports = class Macros { if ( this.builder.t.isCallExpression(expression) && - this.localDebugBindings.some(b => b.node.name === expression.callee.name) + this.localDebugBindings.some(b => b.get('local').node.name === expression.callee.name) ) { - let imported = path.scope.getBinding(expression.callee.name).path.node.imported.name; - this.builder[`${imported}`](path); + let specifier = path.scope.getBinding(expression.callee.name).path.node; + let imported = specifier.imported.name; + // The builder needs to be made aware of the the local name of the ImportSpecifier + this.builder[`${imported}`](path, { + localName: specifier.local.name, + importedName: imported, + }); } } @@ -62,12 +67,22 @@ module.exports = class Macros { // import declaration in question seems to have already been removed return; } + let specifiers = importPath.get('specifiers'); if (specifiers.length === this.localDebugBindings.length) { - this.localDebugBindings[0].parentPath.parentPath.remove(); + importPath.remove(); } else { - this.localDebugBindings.forEach(binding => binding.parentPath.remove()); + this.localDebugBindings.forEach(binding => { + let specifier = binding.get('local').parentPath; + let importPath = specifier.parentPath; + + if (importPath.get('specifiers').length === 1) { + importPath.remove(); + } else { + specifier.remove(); + } + }); } } } diff --git a/tests/create-tests.js b/tests/create-tests.js index dbf09f0..7590e1f 100644 --- a/tests/create-tests.js +++ b/tests/create-tests.js @@ -453,7 +453,7 @@ function createTests(options) { `./fixtures/${fixtureName}/expectation${babelVersion}.js`, 'utf-8' ); - expect(transform(sample, options).code).toEqual(expectation); + expect(transform(sample, options).code.trim()).toEqual(expectation.trim()); }); },