Skip to content

Commit

Permalink
feat(assert-function-name/auto-import): gracefully handle conflicts b…
Browse files Browse the repository at this point in the history
…y renaming existing bindings
  • Loading branch information
jeysal committed Apr 13, 2018
1 parent dc7c529 commit 4bea5c8
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/generate-assert-identifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ const addImport = (
source: string,
name: string,
) => {
if (name) {
// clear possible binding conflicts
scope.rename(name);
}

const program = scope.getProgramParent().path;

// generate default or specified import from source
Expand Down
17 changes: 17 additions & 0 deletions test/__snapshots__/assert-function-name.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`does not rename non-conflicting bindings of the same name 1`] = `
"import check from \\"power-assert\\";
{
let check;
}
expect: check(1 === 1);"
`;

exports[`generates import with specified name when autoImport is set 1`] = `
"import check from \\"power-assert\\";
expect: check(1 === 1);"
`;

exports[`generates specified assert function 1`] = `"expect: check(1 === 1);"`;

exports[`renames conflicting bindings when autoImporting 1`] = `
"import check from \\"power-assert\\";
let _check;
expect: check(1 === 1);"
`;
42 changes: 42 additions & 0 deletions test/assert-function-name.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,45 @@ test('generates import with specified name when autoImport is set', () => {
});
expect(code).toMatchSnapshot();
});

test('renames conflicting bindings when autoImporting', () => {
const { code } = transform(
`let check;
expect: 1 === 1;`,
{
plugins: [
[
plugin,
{
...minimalConfig,
assertFunctionName: 'check',
autoImport: true,
} as Config,
],
],
},
);
expect(code).toMatchSnapshot();
});

test('does not rename non-conflicting bindings of the same name', () => {
const { code } = transform(
`{
let check;
}
expect: 1 === 1;`,
{
plugins: [
[
plugin,
{
...minimalConfig,
assertFunctionName: 'check',
autoImport: true,
} as Config,
],
],
},
);
expect(code).toMatchSnapshot();
});

0 comments on commit 4bea5c8

Please sign in to comment.