Skip to content

Commit 8988de9

Browse files
author
Robert Jackson
committed
Ensure import is removed when other tooling uses the same module.
In the default configuration of ember-cli-babel both `babel-plugin-debug-macros` and `babel-plugin-ember-modules-api-polyfill` handle specific specifiers out of `@ember/debug`. Prior to this change this package would not properly remove the import declaration (even though it had no specifiers left after both plugins had ran), because the `isImportRemovable` flag is eagerly cached. After this change, the import is removed if the only remaining specifiers are the ones that debug-macros is removing.
1 parent 11b5d05 commit 8988de9

File tree

4 files changed

+72
-5
lines changed

4 files changed

+72
-5
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var inspect = Ember.inspect;
2+
3+
4+
inspect('string thing');
5+
(true && !(false) && Ember.assert(false, 'This is an assertion 2'));
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { assert, inspect } from '@ember/debug-tools';
2+
3+
inspect('string thing');
4+
assert(false, 'This is an assertion 2');

src/lib/utils/macros.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ const SUPPORTED_MACROS = ['assert', 'deprecate', 'warn', 'log'];
77
export default class Macros {
88
constructor(t, options) {
99
this.localDebugBindings = [];
10-
this.isImportRemovable = false;
1110
this.envFlagBindings = [];
1211
this.hasEnvFlags = false;
1312
this.envFlagsSource = options.envFlags.envFlagsImport;
@@ -122,9 +121,6 @@ export default class Macros {
122121
collectDebugToolsSpecifiers(specifiers) {
123122
this.importedDebugTools = true;
124123
this._collectImportBindings(specifiers, this.localDebugBindings);
125-
if (specifiers.length === this.localDebugBindings.length) {
126-
this.isImportRemovable = true;
127-
}
128124
}
129125

130126
collectEnvFlagSpecifiers(specifiers) {
@@ -197,7 +193,11 @@ export default class Macros {
197193

198194
if (!debugHelpers.module) {
199195
if (this.localDebugBindings.length > 0) {
200-
if (this.isImportRemovable) {
196+
this.localDebugBindings[0].parentPath.parentPath
197+
let importPath = this.localDebugBindings[0].findParent(p => p.isImportDeclaration());
198+
let specifiers = importPath.get('specifiers');
199+
200+
if (specifiers.length === this.localDebugBindings.length) {
201201
this.localDebugBindings[0].parentPath.parentPath.remove();
202202
} else {
203203
this.localDebugBindings.forEach((binding) => binding.parentPath.remove());

src/tests/debug-tools-test.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,64 @@ let cases = {
7777
]
7878
},
7979

80+
'foreign debug imports': {
81+
transformOptions: {
82+
presets,
83+
plugins: [
84+
[DebugToolsPlugin, {
85+
externalizeHelpers: {
86+
global: 'Ember'
87+
},
88+
debugTools: {
89+
source: '@ember/debug-tools',
90+
assertPredicateIndex: 0
91+
},
92+
envFlags: {
93+
source: '@ember/env-flags',
94+
flags: {
95+
DEBUG: true
96+
}
97+
}
98+
}],
99+
100+
[function(babel) {
101+
let t = babel.types;
102+
103+
return {
104+
name: 'import-remover',
105+
visitor: {
106+
ImportSpecifier(path) {
107+
let importedName = path.node.imported.name;
108+
if (importedName === 'inspect') {
109+
let importDeclarationPath = path.findParent(p => p.isImportDeclaration());
110+
let binding = path.scope.getBinding(importedName);
111+
let references = binding.referencePaths;
112+
113+
let replacements = [];
114+
for (let reference of references) {
115+
replacements.push(t.variableDeclaration('var', [
116+
t.variableDeclarator(
117+
t.identifier(path.node.local.name),
118+
t.memberExpression(t.identifier('Ember'), t.identifier(importedName))
119+
),
120+
]));
121+
}
122+
123+
path.remove();
124+
importDeclarationPath.insertAfter(replacements);
125+
}
126+
}
127+
}
128+
}
129+
}]
130+
]
131+
},
132+
fixtures: [
133+
'shared-debug-module',
134+
],
135+
errors: [ ]
136+
},
137+
80138
'Global External Test Helpers': {
81139
transformOptions: {
82140
presets,

0 commit comments

Comments
 (0)