Skip to content

Fix replacement of renamed/aliased imports (e.g. import { assert as somethingElse } from '@ember/debug') #82

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

Open
wants to merge 8 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
3 changes: 2 additions & 1 deletion fixtures/assert-expansion/expectation6.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
(true && !((() => true)()) && console.assert((() => true)(), 'This is an assertion'));
(true && !(false) && console.assert(false, 'This is an assertion 2'));
(true && !(false) && console.assert(false, 'This is an assertion 2'));
(true && !(false) && console.assert(false, 'renamed assertion'));
3 changes: 2 additions & 1 deletion fixtures/assert-expansion/expectation7.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
(true && !((() => true)()) && console.assert((() => true)(), 'This is an assertion'));
(true && !(false) && console.assert(false, 'This is an assertion 2'));
(true && !(false) && console.assert(false, 'This is an assertion 2'));
(true && !(false) && console.assert(false, 'renamed assertion'));
4 changes: 3 additions & 1 deletion fixtures/assert-expansion/sample.js
Original file line number Diff line number Diff line change
@@ -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');
assert(false, 'This is an assertion 2');
debugAssert(false, 'renamed assertion');
13 changes: 12 additions & 1 deletion fixtures/ember-cli-babel-config/expectation6.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,15 @@ if (true /* DEBUG */) {
id: 'donzo',
until: '4.0.0',
url: 'http://example.com'
}));
}));

// 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'
}));
13 changes: 12 additions & 1 deletion fixtures/ember-cli-babel-config/expectation7.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,15 @@ if (true
id: 'donzo',
until: '4.0.0',
url: 'http://example.com'
}));
}));

// 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
16 changes: 15 additions & 1 deletion fixtures/ember-cli-babel-config/sample.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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'
});


21 changes: 16 additions & 5 deletions src/utils/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,23 @@ module.exports = class Builder {
*
* ($DEBUG && $GLOBAL_NS.assert($PREDICATE, $MESSAGE));
*/
assert(path) {
assert(path, options) {
let predicate;
if (this.assertPredicateIndex !== undefined) {
predicate = (expression, args) => {
return args[this.assertPredicateIndex];
};
}

this._createMacroExpression(path, {
predicate,
});
this._createMacroExpression(
path,
Object.assign(
{
predicate,
},
options
)
);
}

/**
Expand Down Expand Up @@ -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 = [];
Expand Down
27 changes: 21 additions & 6 deletions src/utils/macros.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
}
Expand All @@ -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,
});
}
}

Expand All @@ -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();
}
});
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/create-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
});
},

Expand Down