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: don't mutate user provided array #10014
base: main
Are you sure you want to change the base?
fix: don't mutate user provided array #10014
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎ 2 Ignored Deployments
|
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #10014 +/- ##
=======================================
Coverage 58.45% 58.45%
=======================================
Files 241 241
Lines 17130 17130
Branches 1241 1241
=======================================
Hits 10014 10014
Misses 7071 7071
Partials 45 45
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
Is it possible to write tests for this? |
const embed = new EmbedBuilder();
const fields1 = [{ name: '1', value: '1' }, { name: '2', value: '2' }, { name: '3', value: '3' }];
const fields2 = [{ name: '4', value: '4' }, { name: '5', value: '5' }, { name: '6', value: '6' }];
embed.addFields(fields1);
embed.addFields(fields2);
console.log(fields1);
/**
[
{ name: '1', value: '1' },
{ name: '2', value: '2' },
{ name: '3', value: '3' },
{ name: '4', value: '4' },
{ name: '5', value: '5' },
{ name: '6', value: '6' }
]
**/
console.log(fields2);
/**
[
{ name: '4', value: '4' },
{ name: '5', value: '5' },
{ name: '6', value: '6' }
]
**/ |
This is an incomplete PR i'd say - it needs to also clone the array in the other case |
all the methods uses |
calling |
ok |
I asked for tests because I couldn't reproduce this via |
it happens when u use |
Do you mind sharing a code sample—not sure I'm getting it. |
I just tried and then checked my original code and yeah it was |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
More often than not, the output of this function is already going to be cloned (.map()
, spread, etc.). Perhaps we should instead fix the Embed.addFields()
method, which seems to be the only place where the value is not being cloned.
However, if you choose to keep it as it is, we should remove this clone:
this.data.default_values = normalizedValues.slice(); |
expect(normalizeArray([1, 2, 3])).toEqual([1, 2, 3]); | ||
}); | ||
|
||
test('always returns a clone', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a test case for rest too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rest doesnt gets cloned in normalizeArray as the original input wasn't an array but rest params
Can i get a core members feedback on which one to prefer here? |
|
||
test('always returns a clone', () => { | ||
const arr = [1, 2, 3]; | ||
expect(normalizeArray([arr])).not.toBe(arr); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should check toEqual(arr)
to make sure the output is the same as the input followed by the not.toBe(arr)
to signal that the arrays are equal but they're not the same instance.
Please describe the changes this PR makes and why it should be merged:
Calling
embed.setFields
twice will mutate the array passed in the first call. This PR fixes it.I was going to change it in
spliceFields
only but thought this issue might be present in other builders too so doing it innormalizeArray
makes more senseStatus and versioning classification: