Skip to content

Commit

Permalink
fix(inject): detect references inside object destructuring (#1241)
Browse files Browse the repository at this point in the history
* Fix: doesn't detect references in destructed object value

* Fix: pass the linetr

* Update snapshot
  • Loading branch information
eight04 committed Oct 15, 2022
1 parent 77178b3 commit b64cd9c
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/inject/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export default function inject(options) {

// special case – shorthand properties. because node.key === node.value,
// we can't differentiate once we've descended into the node
if (node.type === 'Property' && node.shorthand) {
if (node.type === 'Property' && node.shorthand && node.value.type === 'Identifier') {
const { name } = node.key;
handleReference(node, name, name);
this.skip();
Expand Down
3 changes: 3 additions & 0 deletions packages/inject/test/fixtures/shorthand-assignment/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const { Promise = "fallback" } = foo;
console.log(Promise);

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function foo({bar = Promise}) {
console.log(bar);
}
foo();

5 changes: 5 additions & 0 deletions packages/inject/test/fixtures/shorthand-func/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function foo({Promise}) {
console.log(Promise);
}
foo();

33 changes: 33 additions & 0 deletions packages/inject/test/snapshots/test.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,39 @@ Generated by [AVA](https://avajs.dev).
polyfills.Promise.resolve().then(() => 'it works');␊
`

## handles shorthand properties (as assignment)

> Snapshot 1
`const { Promise = "fallback" } = foo;␊
console.log(Promise);␊
␊
`

## handles shorthand properties in function

> Snapshot 1
`function foo({Promise}) {␊
console.log(Promise);␊
}␊
foo();␊
␊
`

## handles shorthand properties in function (as fallback value)

> Snapshot 1
`import { Promise as Promise } from 'es6-promise';␊
␊
function foo({bar = Promise}) {␊
console.log(bar);␊
}␊
foo();␊
␊
`

## handles redundant keys

> Snapshot 1
Expand Down
Binary file modified packages/inject/test/snapshots/test.js.snap
Binary file not shown.
12 changes: 12 additions & 0 deletions packages/inject/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ test('handles shorthand properties', (t) => {
compare(t, 'shorthand', { Promise: ['es6-promise', 'Promise'] });
});

test('handles shorthand properties (as assignment)', (t) => {
compare(t, 'shorthand-assignment', { Promise: ['es6-promise', 'Promise'] });
});

test('handles shorthand properties in function', (t) => {
compare(t, 'shorthand-func', { Promise: ['es6-promise', 'Promise'] });
});

test('handles shorthand properties in function (as fallback value)', (t) => {
compare(t, 'shorthand-func-fallback', { Promise: ['es6-promise', 'Promise'] });
});

test('handles redundant keys', (t) => {
compare(t, 'redundant-keys', {
Buffer: 'Buffer',
Expand Down

0 comments on commit b64cd9c

Please sign in to comment.