Skip to content

Commit

Permalink
Fix: doesn't detect references in destructed object value
Browse files Browse the repository at this point in the history
  • Loading branch information
eight04 committed Oct 14, 2022
1 parent 77178b3 commit a8a8ddc
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 12 deletions.
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
2 changes: 2 additions & 0 deletions packages/inject/test/fixtures/shorthand-assignment/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const { Promise = "fallback" } = foo;

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

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

41 changes: 30 additions & 11 deletions packages/inject/test/snapshots/test.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,6 @@ Generated by [AVA](https://avajs.dev).
});␊
`

## escapes backslashes in module name

> Snapshot 1
`import { default as $ } from 'slash\\\\back';␊
␊
$(() => {␊
console.log('ready');␊
});␊
`

## inserts a named import statement

> Snapshot 1
Expand Down Expand Up @@ -103,6 +92,36 @@ Generated by [AVA](https://avajs.dev).
polyfills.Promise.resolve().then(() => 'it works');␊
`

## handles shorthand properties (as assignment)

> Snapshot 1
`const { Promise = "fallback" } = foo;␊
␊
`

## handles shorthand properties in function

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

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

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

## 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 a8a8ddc

Please sign in to comment.