Skip to content

Commit

Permalink
Merge pull request #156 from mdeanjones/master
Browse files Browse the repository at this point in the history
  • Loading branch information
rwjblue authored Feb 11, 2021
2 parents d6bfa5a + d1df8e7 commit 3f7f816
Show file tree
Hide file tree
Showing 4 changed files with 277 additions and 2 deletions.
41 changes: 41 additions & 0 deletions __tests__/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,3 +457,44 @@ let MyController = (_dec = Ember._action, (_class = class MyController extends E
export { MyController as default };`);
});
});

describe('when used with babel-plugin-istanbul', () => {
// babel-plugin-istanbul won't run on <= Node 6
const majorVersion = parseInt(process.version.match(/^v(\d+)\./)[1], 10);
const runOrSkip = majorVersion > 6 ? it : it.skip;

runOrSkip('works with mixins', () => {
let source = `
import EmberObject from '@ember/object';
import Evented from '@ember/object/evented';
const TestObject = EmberObject.extend(Evented);
export default TestObject;
`;

let actual = babel7.transformSync(source, {
filename: 'istanbul-should-cover.js',
plugins: [require('babel-plugin-istanbul'), Plugin],
}).code;

expect(actual).toContain('Ember.Object.extend(Ember.Evented)');
});

runOrSkip('works with classes that extend from mixins', () => {
let source = `
import EmberObject from '@ember/object';
import Evented from '@ember/object/evented';
export default class TestObject extends EmberObject.extend(Evented) {};
`;

let actual = babel7.transformSync(source, {
filename: 'istanbul-should-cover.js',
plugins: [require('babel-plugin-istanbul'), Plugin],
}).code;

expect(actual).toContain(
'export default class TestObject extends (Ember.Object.extend(Ember.Evented)) {}'
);
});
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"babel-core": "^6.25.0",
"eslint": "^7.19.0",
"eslint-config-prettier": "^7.2.0",
"babel-plugin-istanbul": "^6.0.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^3.3.1",
"jest": "^24.9.0",
Expand Down
12 changes: 10 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ module.exports = function (babel) {
])
);
} else {
// Replace the occurences of the imported name with the global name.
let binding = path.scope.getBinding(local.name);
let referencePaths = binding.referencePaths;

Expand Down Expand Up @@ -211,10 +212,17 @@ module.exports = function (babel) {
});
}

// Replace the occurences of the imported name with the global name.
// Replace the occurrences of the imported name with the global name.
referencePaths.forEach((referencePath) => {
if (!isTypescriptNode(referencePath.parentPath)) {
referencePath.replaceWith(getMemberExpressionFor(global));
const memberExpression = getMemberExpressionFor(global);

try {
referencePath.replaceWith(memberExpression);
} catch (e) {
referencePath.scope.crawl();
referencePath.replaceWith(memberExpression);
}
}
});
}
Expand Down
Loading

0 comments on commit 3f7f816

Please sign in to comment.