cloudflare / wildebeest

Wildebeest is an ActivityPub and Mastodon-compatible server
Other
2.04k stars 399 forks source link

fix: `...` (spread vs. rest property) variable assignment #366

Closed DataDrivenMD closed 1 year ago

DataDrivenMD commented 1 year ago

👀 Please review/merge https://github.com/cloudflare/wildebeest/pull/363 before this PR

Examples of the problem:

const id = 'ActorA'
const preferredUsername = 'jorge'
const properties = { id: 'ActorB', preferredUsername: 'sven'}

const example0 = { id, preferredUsername, properties }
console.log( JSON.stringify(example0) )
// '{"id":"ActorA","preferredUsername":"jorge","properties":{"id":"ActorB","preferredUsername":"sven"}}'

const example1 = { id, preferredUsername, ...properties }
console.log( JSON.stringify(example1) )
// '{"id":"ActorB","preferredUsername":"sven"}'

const example2 = { id, ...properties, preferredUsername }
console.log( JSON.stringify(example2) )
// '{"id":"ActorB","preferredUsername":"jorge"}'

const example3 = { ...properties, id, preferredUsername }
console.log( JSON.stringify(example3) )
// '{"id":"ActorA","preferredUsername":"jorge"}'

Fix the problem

Identify bugs caused by the problem and Fix bugs caused by the problem

TL; DR:

Tests that were previously passing but are now breaking rely on assertions that validate hard-coded defaults- these values don't change when the business logic yields null or undefined values along the way, as was happening in several areas due to variable assignment and object binding using the spread and rest operators (...). Now that the correct syntax is being used and the bugs in the original code has been addressed, the proper use of ... in variable assignment should be a point of emphasis for code reviews moving forward.

References:

https://gist.github.com/yesvods/51af798dd1e7058625f4

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax#copy_an_array

cc @xtuc @dario-piotrowicz

DataDrivenMD commented 1 year ago

Sorry for the spam. Trying to revert an accidental merge commit that pulled in several upstream merge commits 🤦🏼

DataDrivenMD commented 1 year ago

Closing this PR while I sort out the mess I created for myself. Apologies again for the spam