Skip to content

Commit

Permalink
fix(inject): isReference check bug (#804)
Browse files Browse the repository at this point in the history
* disregard the `bar` in `import { bar as foo }

* isReference test
  • Loading branch information
linsk1998 authored and guybedford committed Apr 30, 2021
1 parent bfe60d3 commit 95ffe66
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 38 deletions.
3 changes: 3 additions & 0 deletions packages/inject/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ const isReference = (node, parent) => {
// disregard the `bar` in `export { foo as bar }`
if (parent.type === 'ExportSpecifier' && node !== parent.local) return false;

// disregard the `bar` in `import { bar as foo }`
if (parent.type === 'ImportSpecifier' && node === parent.imported) { return false; }

return true;
}

Expand Down
5 changes: 5 additions & 0 deletions packages/inject/test/fixtures/is-reference/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { bar as foo } from "path";
console.log({ bar: foo });
class Foo { bar () { console.log(this) } }
export { Foo }
export { foo as bar }
86 changes: 48 additions & 38 deletions packages/inject/test/snapshots/test.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,49 @@

The actual snapshot is saved in `test.js.snap`.

Generated by [AVA](https://ava.li).
Generated by [AVA](https://avajs.dev).

## generates * imports
## inserts a default import statement

> Snapshot 1
`import * as foo from 'foo';␊
`import { default as $ } from 'jquery';␊
␊
console.log(foo.bar);␊
console.log(foo.baz);␊
$(() => {␊
console.log('ready');␊
});␊
`

## handles redundant keys
## uses the modules property

> Snapshot 1
`import { default as $inject_Buffer_isBuffer } from 'is-buffer';␊
`import { default as $ } from 'jquery';␊
␊
$inject_Buffer_isBuffer('foo');␊
$(() => {␊
console.log('ready');␊
});␊
`

## handles shadowed variables
## inserts a named import statement

> Snapshot 1
`function launch($) {␊
$(() => {␊
console.log('ready');␊
});␊
}␊
`import { Promise as Promise } from 'es6-promise';␊
␊
launch((fn) => fn());␊
Promise.all([thisThing, thatThing]).then(() => someOtherThing);␊
`

## handles shorthand properties
## overwrites keypaths

> Snapshot 1
`import { Promise as Promise } from 'es6-promise';␊
`import { default as $inject_Object_assign } from 'fixtures/keypaths/polyfills/object-assign.js';␊
␊
const polyfills = { Promise };␊
polyfills.Promise.resolve().then(() => 'it works');␊
const original = { foo: 'bar' };␊
const clone = $inject_Object_assign({}, original);␊
␊
export default clone;␊
`

## ignores existing imports
Expand All @@ -57,36 +58,46 @@ Generated by [AVA](https://ava.li).
});␊
`

## inserts a default import statement
## handles shadowed variables

> Snapshot 1
`import { default as $ } from 'jquery';␊
`function launch($) {␊
$(() => {␊
console.log('ready');␊
});␊
}␊
␊
$(() => {␊
console.log('ready');␊
});␊
launch((fn) => fn());␊
`

## inserts a named import statement
## handles shorthand properties

> Snapshot 1
`import { Promise as Promise } from 'es6-promise';␊
␊
Promise.all([thisThing, thatThing]).then(() => someOtherThing);␊
const polyfills = { Promise };␊
polyfills.Promise.resolve().then(() => 'it works');␊
`

## overwrites keypaths
## handles redundant keys

> Snapshot 1
`import { default as $inject_Object_assign } from 'fixtures/keypaths/polyfills/object-assign.js';␊
`import { default as $inject_Buffer_isBuffer } from 'is-buffer';␊
␊
const original = { foo: 'bar' };␊
const clone = $inject_Object_assign({}, original);␊
$inject_Buffer_isBuffer('foo');␊
`

## generates * imports

> Snapshot 1
`import * as foo from 'foo';␊
␊
export default clone;␊
console.log(foo.bar);␊
console.log(foo.baz);␊
`

## transpiles non-JS files but handles failures to parse
Expand All @@ -99,13 +110,12 @@ Generated by [AVA](https://ava.li).
assert.equal(foo, path.join('..', 'baz'));␊
`

## uses the modules property
## ignores check isReference is false

> Snapshot 1
`import { default as $ } from 'jquery';␊
␊
$(() => {␊
console.log('ready');␊
});␊
`
`import { bar as foo } from "path";␊
console.log({ bar: foo });␊
class Foo { bar () { console.log(this) } }␊
export { Foo }␊
export { foo as bar }`
Binary file modified packages/inject/test/snapshots/test.js.snap
Binary file not shown.
4 changes: 4 additions & 0 deletions packages/inject/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,7 @@ test('generates * imports', (t) => {
test('transpiles non-JS files but handles failures to parse', (t) => {
compare(t, 'non-js', { relative: ['path', 'relative'] });
});

test('ignores check isReference is false', (t) => {
compare(t, 'is-reference', { bar:'path' });
});

0 comments on commit 95ffe66

Please sign in to comment.