Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(inject): detect references inside object destructuring #1241

Merged
merged 3 commits into from
Oct 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My worry is that with this change, if the shorthand property is an assignment pattern but the left side should be replaced, it would cause the same issue we are guarding against.

i.e. what would happen here

export const test = ({process = {}}) => {
   console.log(process);
 };

(Admittedly I did not run the code...)
Maybe we should rather add special handling for assignment patterns in shorthands than restrict the current special handling to the identifier case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be guarded by if (scope.contains(...)) since process is a local variable in this case.

I guess we can add more test cases e.g.

{
  const {$} = foo;
}
{
  const foo = {$};
}
{
  const foo = ({$}) => {};
}
{
  const foo = ({bar = $}) => {};
}

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