Skip to content

Commit

Permalink
fix(auto-import): avoid unnecessary duplicate imports
Browse files Browse the repository at this point in the history
  • Loading branch information
jeysal committed Mar 20, 2018
1 parent 98a3639 commit 308ffcb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/assertify-statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default (
// register added import with scope so it is found for the next assertion
// for some reason, this needs to happen after we have referenced the import
// with our call expression, otherwise the import will be removed
(scope as any).crawl();
(scope.getProgramParent() as any).crawl();

if (config.powerAssert) {
// Now let espower generate nice power assertions for this assertion
Expand Down
10 changes: 10 additions & 0 deletions test/__snapshots__/auto-import.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ expect: _assert(1 === 1);
expect: _assert(2 === 2);"
`;

exports[`reuses the same import for multiple assertions in nested scopes 1`] = `
"import _assert from \\"power-assert\\";
(() => {
expect: _assert(1 === 1);
expect: _assert(2 === 2);
})();"
`;

exports[`uses an existing default import 1`] = `
"import fancyAssert from 'power-assert';
Expand Down
13 changes: 13 additions & 0 deletions test/auto-import.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ test('reuses the same import for multiple assertions', () => {
expect(code).toMatchSnapshot();
});

test('reuses the same import for multiple assertions in nested scopes', () => {
const { code } = transform(
`(() => {
expect: 1 === 1;
expect: 2 === 2;
})()`,
{
plugins: [[plugin, { powerAssert: false } as Config]],
},
);
expect(code).toMatchSnapshot();
});

test('does not break preset-env module transform and generates code runnable in node', () => {
const { code } = transform(`expect: 1 === 2;`, {
plugins: [[plugin, { powerAssert: false } as Config]],
Expand Down

0 comments on commit 308ffcb

Please sign in to comment.