ronantakizawa/codereview-bench huggingface dataset:
Repository: discordjs/discord.js
File: packages/ws/src/ws/WebSocketShard.ts (TypeScript)

Code before:
						error: new Error(
							`${this.zLibSyncInflate.err}${this.zLibSyncInflate.msg ? `: ${this.zLibSyncInflate.msg}` : ''}`,
						),
					});
				}

				if (!flush) {
					return null;
				}

				const { result } = this.zLibSyncInflate;
				if (!result) {
					return null;
				}

				return JSON.parse(
					typeof result === 'string' ? result : this.textDecoder.decode(result),
				) as GatewayReceivePayload;
			}
		}

		this.debug([
			'Received a message we were unable to decompress',
			`isBinary: ${isBinary.toString()}`,
			`useIdentifyCompress: ${this.useIdentifyCompression.toString()}`,
			`inflate: ${Boolean(this.nativeInflate).toString()}`,
		]);

		return null;
	}

	private async onMessage(data: Data, isBinary: boolean) {
		const payload = await this.unpackMessage(data, isBinary);
		if (!payload) {
			return;
		}

		switch (payload.op) {
			case GatewayOpcodes.Dispatch: {
				if (this.#status === WebSocketShardStatus.Resuming) {
					this.replayedEvents++;
				}

				// eslint-disable-next-line sonarjs/no-nested-switch
				switch (payload.t) {
					case GatewayDispatchEvents.Ready: {
						this.#status = WebSocketShardStatus.Ready;

						const session = {
							sequence: payload.s,
							sessionId: payload.d.session_id,

Review comment (suggestion):
```suggestion
			`useIdentifyCompression: ${this.useIdentifyCompression.toString()}`,
			`nativeInflate: ${Boolean(this.nativeInflate).toString()}`,
```

Code after:

					resolve(JSON.parse(this.textDecoder.decode(result)) as GatewayReceivePayload);
				});
			});
		}

		// Deal with transport compression
		if (this.transportCompressionEnabled) {
			const flush =
				decompressable.length >= 4 &&
				decompressable.at(-4) === 0x00 &&
				decompressable.at(-3) === 0x00 &&
				decompressable.at(-2) === 0xff &&
				decompressable.at(-1) === 0xff;

			if (this.nativeInflate) {
				this.nativeInflate.write(decompressable, 'binary');

				if (!flush) {
					return null;
				}

				const [result] = await once(this.nativeInflate, 'data');
				return this.parseInflateResult(result);
			} else if (this.zLibSyncInflate) {
				const zLibSync = (await getZlibSync())!;
				this.zLibSyncInflate.push(Buffer.from(decompressable), flush ? zLibSync.Z_SYNC_FLUSH : zLibSync.Z_NO_FLUSH);

				if (this.zLibSyncInflate.err) {
					this.emit(WebSocketShardEvents.Error, {
						error: new Error(
							`${this.zLibSyncInflate.err}${this.zLibSyncInflate.msg ? `: ${this.zLibSyncInflate.msg}` : ''}`,
						),
					});
				}

				if (!flush) {
					return null;
				}

				const { result } = this.zLibSyncInflate;
				return this.parseInflateResult(result);
			}
		}

		this.debug([
			'Received a message we were unable to decompress',
			`isBinary: ${isBinary.toString()}`,
			`identifyCompressionEnabled: ${this.identifyCompressionEnabled.toString()}`,
			`inflate: ${this.transportCompressionEnabled ? CompressionMethod[this.strategy.options.compression!] : 'none'}`,
		]);
Repository: discordjs/discord.js
File: packages/discord.js/src/structures/Message.js (JavaScript)

Code before:

    if (data.poll) {
      /**
       * The poll that was sent with the message
       * @type {?Poll}
       */
      this.poll = new Poll(this.client, data.poll, this);
    } else {
      this.poll ??= null;
    }

    /**
     * The call associated with the message
     * @typedef {Object} MessageCall
     * @property {?Date} endedAt The time the call ended
     * @property {?number} endedTimestamp The timestamp the call ended
     * @property {Snowflake[]} participants The ids of the users that participated in the call
     */

    if (data.call) {
      /**
       * The call associated with the message
       * @type {?MessageCall}
       */
      this.call = {
        endedAt: data.call.ended_timestamp && new Date(data.call.ended_timestamp),
        endedTimestamp: data.call.ended_timestamp && Date.parse(data.call.ended_timestamp),
        participants: data.call.participants,
      };
    } else {
      this.call ??= null;
    }
  }

  /**
   * The channel that the message was sent in
   * @type {TextBasedChannels}
   * @readonly
   */
  get channel() {
    return this.client.channels.resolve(this.channelId);
  }

  /**
   * Whether or not this message is a partial
   * @type {boolean}
   * @readonly
   */
  get partial() {
    return typeof this.content !== 'string' || !this.author;
  }

Review comment (suggestion):
This should work, but test it first!

```suggestion
        get endedAt() {
        	return this.endedTimestamp && new Date(this.endedTimestamp)
        },
```

Code after:

    if (data.poll) {
      /**
       * The poll that was sent with the message
       * @type {?Poll}
       */
      this.poll = new Poll(this.client, data.poll, this);
    } else {
      this.poll ??= null;
    }

    /**
     * A call associated with a message
     * @typedef {Object} MessageCall
     * @property {Readonly<?Date>} endedAt The time the call ended
     * @property {?number} endedTimestamp The timestamp the call ended
     * @property {Snowflake[]} participants The ids of the users that participated in the call
     */

    if (data.call) {
      /**
       * The call associated with the message
       * @type {?MessageCall}
       */
      this.call = {
        endedTimestamp: data.call.ended_timestamp ? Date.parse(data.call.ended_timestamp) : null,
        participants: data.call.participants,
        get endedAt() {
          return this.endedTimestamp && new Date(this.endedTimestamp);
        },
      };
    } else {
      this.call ??= null;
    }
  }

  /**
   * The channel that the message was sent in
   * @type {TextBasedChannels}
   * @readonly
   */
  get channel() {
    return this.client.channels.resolve(this.channelId);
  }

  /**
   * Whether or not this message is a partial
   * @type {boolean}
   * @readonly
   */
  get partial() {
Repository: discordjs/discord.js
File: packages/discord.js/src/structures/Message.js (JavaScript)

Code before:

    if (data.poll) {
      /**
       * The poll that was sent with the message
       * @type {?Poll}
       */
      this.poll = new Poll(this.client, data.poll, this);
    } else {
      this.poll ??= null;
    }

    /**
     * The call associated with the message
     * @typedef {Object} MessageCall
     * @property {?Readonly<Date>} endedAt The time the call ended
     * @property {?number} endedTimestamp The timestamp the call ended
     * @property {Snowflake[]} participants The ids of the users that participated in the call
     */

    if (data.call) {
      /**
       * The call associated with the message
       * @type {?MessageCall}
       */
      this.call = {
        endedTimestamp: data.call.ended_timestamp && Date.parse(data.call.ended_timestamp),
        participants: data.call.participants,
        get endedAt() {
          return this.endedTimestamp && new Date(this.endedTimestamp);
        },
      };
    } else {
      this.call ??= null;
    }
  }

  /**
   * The channel that the message was sent in
   * @type {TextBasedChannels}
   * @readonly
   */
  get channel() {
    return this.client.channels.resolve(this.channelId);
  }

  /**
   * Whether or not this message is a partial
   * @type {boolean}
   * @readonly
   */
  get partial() {

Review comment (suggestion):
According to the docs, this might also missing

```suggestion
        endedTimestamp: data.call.ended_timestamp ? Date.parse(data.call.ended_timestamp) : null,
```

Code after:

    if (data.poll) {
      /**
       * The poll that was sent with the message
       * @type {?Poll}
       */
      this.poll = new Poll(this.client, data.poll, this);
    } else {
      this.poll ??= null;
    }

    /**
     * A call associated with a message
     * @typedef {Object} MessageCall
     * @property {Readonly<?Date>} endedAt The time the call ended
     * @property {?number} endedTimestamp The timestamp the call ended
     * @property {Snowflake[]} participants The ids of the users that participated in the call
     */

    if (data.call) {
      /**
       * The call associated with the message
       * @type {?MessageCall}
       */
      this.call = {
        endedTimestamp: data.call.ended_timestamp ? Date.parse(data.call.ended_timestamp) : null,
        participants: data.call.participants,
        get endedAt() {
          return this.endedTimestamp && new Date(this.endedTimestamp);
        },
      };
    } else {
      this.call ??= null;
    }
  }

  /**
   * The channel that the message was sent in
   * @type {TextBasedChannels}
   * @readonly
   */
  get channel() {
    return this.client.channels.resolve(this.channelId);
  }

  /**
   * Whether or not this message is a partial
   * @type {boolean}
   * @readonly
   */
  get partial() {
Repository: discordjs/discord.js
File: packages/discord.js/src/structures/Message.js (JavaScript)

Code before:
      /**
       * Partial data of the interaction that this message is a reply to
       * @type {?MessageInteraction}
       */
      this.interaction = {
        id: data.interaction.id,
        type: data.interaction.type,
        commandName: data.interaction.name,
        user: this.client.users._add(data.interaction.user),
      };
    } else {
      this.interaction ??= null;
    }

    if (data.poll) {
      /**
       * The poll that was sent with the message
       * @type {?Poll}
       */
      this.poll = new Poll(this.client, data.poll, this);
    } else {
      this.poll ??= null;
    }

    /**
     * The call associated with the message
     * @typedef {Object} MessageCall
     * @property {?Readonly<Date>} endedAt The time the call ended
     * @property {?number} endedTimestamp The timestamp the call ended
     * @property {Snowflake[]} participants The ids of the users that participated in the call
     */

    if (data.call) {
      /**
       * The call associated with the message
       * @type {?MessageCall}
       */
      this.call = {
        endedTimestamp: data.call.ended_timestamp && Date.parse(data.call.ended_timestamp),
        participants: data.call.participants,
        get endedAt() {
          return this.endedTimestamp && new Date(this.endedTimestamp);
        },
      };
    } else {
      this.call ??= null;
    }
  }

  /**
   * The channel that the message was sent in

Review comment (suggestion):
```suggestion
     * A call associated with a message
```

Code after:
      /**
       * Partial data of the interaction that this message is a reply to
       * @type {?MessageInteraction}
       */
      this.interaction = {
        id: data.interaction.id,
        type: data.interaction.type,
        commandName: data.interaction.name,
        user: this.client.users._add(data.interaction.user),
      };
    } else {
      this.interaction ??= null;
    }

    if (data.poll) {
      /**
       * The poll that was sent with the message
       * @type {?Poll}
       */
      this.poll = new Poll(this.client, data.poll, this);
    } else {
      this.poll ??= null;
    }

    /**
     * A call associated with a message
     * @typedef {Object} MessageCall
     * @property {Readonly<?Date>} endedAt The time the call ended
     * @property {?number} endedTimestamp The timestamp the call ended
     * @property {Snowflake[]} participants The ids of the users that participated in the call
     */

    if (data.call) {
      /**
       * The call associated with the message
       * @type {?MessageCall}
       */
      this.call = {
        endedTimestamp: data.call.ended_timestamp ? Date.parse(data.call.ended_timestamp) : null,
        participants: data.call.participants,
        get endedAt() {
          return this.endedTimestamp && new Date(this.endedTimestamp);
        },
      };
    } else {
      this.call ??= null;
    }
  }

  /**
   * The channel that the message was sent in
Repository: discordjs/discord.js
File: packages/discord.js/src/structures/Message.js (JavaScript)

Code before:
       * @type {?MessageInteraction}
       */
      this.interaction = {
        id: data.interaction.id,
        type: data.interaction.type,
        commandName: data.interaction.name,
        user: this.client.users._add(data.interaction.user),
      };
    } else {
      this.interaction ??= null;
    }

    if (data.poll) {
      /**
       * The poll that was sent with the message
       * @type {?Poll}
       */
      this.poll = new Poll(this.client, data.poll, this);
    } else {
      this.poll ??= null;
    }

    /**
     * The call associated with the message
     * @typedef {Object} MessageCall
     * @property {?Readonly<Date>} endedAt The time the call ended
     * @property {?number} endedTimestamp The timestamp the call ended
     * @property {Snowflake[]} participants The ids of the users that participated in the call
     */

    if (data.call) {
      /**
       * The call associated with the message
       * @type {?MessageCall}
       */
      this.call = {
        endedTimestamp: data.call.ended_timestamp && Date.parse(data.call.ended_timestamp),
        participants: data.call.participants,
        get endedAt() {
          return this.endedTimestamp && new Date(this.endedTimestamp);
        },
      };
    } else {
      this.call ??= null;
    }
  }

  /**
   * The channel that the message was sent in
   * @type {TextBasedChannels}
   * @readonly

Review comment (suggestion):
```suggestion
     * @property {Readonly<?Date>} endedAt The time the call ended
```

Code after:
       * @type {?MessageInteraction}
       */
      this.interaction = {
        id: data.interaction.id,
        type: data.interaction.type,
        commandName: data.interaction.name,
        user: this.client.users._add(data.interaction.user),
      };
    } else {
      this.interaction ??= null;
    }

    if (data.poll) {
      /**
       * The poll that was sent with the message
       * @type {?Poll}
       */
      this.poll = new Poll(this.client, data.poll, this);
    } else {
      this.poll ??= null;
    }

    /**
     * A call associated with a message
     * @typedef {Object} MessageCall
     * @property {Readonly<?Date>} endedAt The time the call ended
     * @property {?number} endedTimestamp The timestamp the call ended
     * @property {Snowflake[]} participants The ids of the users that participated in the call
     */

    if (data.call) {
      /**
       * The call associated with the message
       * @type {?MessageCall}
       */
      this.call = {
        endedTimestamp: data.call.ended_timestamp ? Date.parse(data.call.ended_timestamp) : null,
        participants: data.call.participants,
        get endedAt() {
          return this.endedTimestamp && new Date(this.endedTimestamp);
        },
      };
    } else {
      this.call ??= null;
    }
  }

  /**
   * The channel that the message was sent in
   * @type {TextBasedChannels}
   * @readonly
Repository: discordjs/discord.js
File: packages/brokers/src/brokers/redis/BaseRedis.ts (TypeScript)

Code before:
						if (idx < 0) {
							continue;
						}

						const data = packet[idx + 1];
						if (!data) {
							continue;
						}

						this.emitEvent(id, this.options.group, event.toString('utf8'), this.options.decode(data));
					}
				}
			} catch (error) {
				this.emit('error', error);
				break;
			}
		}

		this.listening = false;
	}

	/**
	 * Destroys the broker, closing all connections
	 */
	public async destroy() {
		await this.unsubscribe(Array.from(this.subscribedEvents));
		this.streamReadClient.disconnect();
		this.redisClient.disconnect();
	}

	/**
	 * Handles an incoming Redis event
	 */
	protected abstract emitEvent(id: Buffer, group: string, event: string, data: unknown): unknown;
}


Review comment (suggestion):
```suggestion
		await this.unsubscribe(...this.subscribedEvents);
```

think we don't really use `Array.from`.

Code after:
						if (idx < 0) {
							continue;
						}

						const data = packet[idx + 1];
						if (!data) {
							continue;
						}

						this.emitEvent(id, this.options.group, event.toString('utf8'), this.options.decode(data));
					}
				}
			} catch (error) {
				this.emit('error', error);
				break;
			}
		}

		this.listening = false;
	}

	/**
	 * Destroys the broker, closing all connections
	 */
	public async destroy() {
		await this.unsubscribe([...this.subscribedEvents]);
		this.streamReadClient.disconnect();
		this.redisClient.disconnect();
	}

	/**
	 * Handles an incoming Redis event
	 */
	protected abstract emitEvent(id: Buffer, group: string, event: string, data: unknown): unknown;
}
Repository: discordjs/discord.js
File: packages/discord.js/src/structures/VoiceChannelEffect.js (JavaScript)

Code before:
    /**
     * The animation id of the effect.
     * @type {?number}
     */
    this.animationId = data.animation_id ?? null;

    /**
     * The id of the soundboard sound for soundboard effects.
     * @type {?(Snowflake|number)}
     */
    this.soundId = data.sound_id ?? null;

    /**
     * The volume of the soundboard sound [0-1] for soundboard effects.
     * @type {?number}
     */
    this.soundVolume = data.sound_volume ?? null;
  }

  /**
   * The channel the effect was sent in.
   * @type {?VoiceChannel}
   * @readonly
   */
  get channel() {
    return this.guild.channels.cahe.get(this.channelId) ?? null;
  }
}

module.exports = VoiceChannelEffect;


Review comment (performance):
```suggestion
    return this.guild.channels.cache.get(this.channelId) ?? null;
```

Code after:
    /**
     * The animation id of the effect.
     * @type {?number}
     */
    this.animationId = data.animation_id ?? null;

    /**
     * The id of the soundboard sound for soundboard effects.
     * @type {?(Snowflake|number)}
     */
    this.soundId = data.sound_id ?? null;

    /**
     * The volume of the soundboard sound [0-1] for soundboard effects.
     * @type {?number}
     */
    this.soundVolume = data.sound_volume ?? null;
  }

  /**
   * The channel the effect was sent in.
   * @type {?VoiceChannel}
   * @readonly
   */
  get channel() {
    return this.guild.channels.cache.get(this.channelId) ?? null;
  }
}

module.exports = VoiceChannelEffect;
Repository: discordjs/discord.js
File: packages/discord.js/src/structures/Poll.js (JavaScript)

Code before:
     * @typedef {Object} PollQuestionMedia
     * @property {string} text The text of this question
     */

    /**
     * The media for this poll's question
     * @type {PollQuestionMedia}
     */
    this.question = {
      text: data.question.text,
    };

    /**
     * The answers of this poll
     * @type {Collection<number, PollAnswer | PartialPollAnswer>}
     */
    this.answers = data.answers.reduce(
      (acc, answer) => acc.set(answer.answer_id, new PollAnswer(this.client, answer, this)),
      new Collection(),
    );

    /**
     * The timestamp when this poll expires
     * @type {?number}
     */
    this.expiresTimestamp = Date.parse(data.expiry) ?? null;

    /**
     * Whether this poll allows multiple answers
     * @type {boolean}
     */
    this.allowMultiselect = data.allow_multiselect;

    /**
     * The layout type of this poll
     * @type {PollLayoutType}
     */
    this.layoutType = data.layout_type;

    /**
     * Whether or not this poll is a partial
     * @name Poll#_partial
     * @type {boolean}
     * @private
     */
    Object.defineProperty(this, '_partial', { value: data.partial ?? false });

    this._patch(data);
  }

  _patch(data) {

Review comment (bug):
This change here is to prevent a hard crash once polls without expiry dates roll out, and it is also marked as nullish on the API documentation [here](https://discord.com/developers/docs/resources/poll#poll-object-poll-object-structure)

Code after:
  _patch(data) {
    if (data.results) {
      /**
       * Whether this poll's results have been precisely counted
       * @type {boolean}
       */
      this.resultsFinalized = data.results.is_finalized;

      for (const answerResult of data.results.answer_counts) {
        const answer = this.answers.get(answerResult.id);
        answer?._patch(answerResult);
      }
    } else {
      this.resultsFinalized ??= false;
    }

    if ('allow_multiselect' in data) {
      /**
       * Whether this poll allows multiple answers
       * @type {boolean}
       */
      this.allowMultiselect = data.allow_multiselect;
    } else {
      this.allowMultiselect ??= null;
    }

    if ('layout_type' in data) {
      /**
       * The layout type of this poll
       * @type {PollLayoutType}
       */
      this.layoutType = data.layout_type;
    } else {
      this.layoutType ??= null;
    }

    if ('expiry' in data) {
      /**
       * The timestamp when this poll expires
       * @type {?number}
       */
      this.expiresTimestamp = data.expiry && Date.parse(data.expiry);
    } else {
      this.expiresTimestamp ??= null;
    }

    if (data.question) {
      /**
       * The media for a poll's question
       * @typedef {Object} PollQuestionMedia
       * @property {?string} text The text of this question
Repository: discordjs/discord.js
File: packages/discord.js/src/structures/Poll.js (JavaScript)

Code before:
     */
    this.answers = data.answers.reduce(
      (acc, answer) => acc.set(answer.answer_id, new PollAnswer(this.client, answer, this)),
      new Collection(),
    );

    /**
     * The timestamp when this poll expires
     * @type {?number}
     */
    this.expiresTimestamp = Date.parse(data.expiry) ?? null;

    /**
     * Whether this poll allows multiple answers
     * @type {boolean}
     */
    this.allowMultiselect = data.allow_multiselect;

    /**
     * The layout type of this poll
     * @type {PollLayoutType}
     */
    this.layoutType = data.layout_type;

    /**
     * Whether or not this poll is a partial
     * @name Poll#_partial
     * @type {boolean}
     * @private
     */
    Object.defineProperty(this, '_partial', { value: data.partial ?? false });

    this._patch(data);
  }

  _patch(data) {
    if (data.results) {
      /**
       * Whether this poll's results have been precisely counted
       * @type {boolean}
       */
      this.resultsFinalized = data.results.is_finalized;

      for (const answerResult of data.results.answer_counts) {
        const answer = this.answers.get(answerResult.id);
        answer?._patch(answerResult);
      }
    } else if (data.question) {
      this.question = {
        text: data.question.text,
      };

Review comment (suggestion):
```suggestion
     * Whether this poll is a partial
```

Code after:

    if ('allow_multiselect' in data) {
      /**
       * Whether this poll allows multiple answers
       * @type {boolean}
       */
      this.allowMultiselect = data.allow_multiselect;
    } else {
      this.allowMultiselect ??= null;
    }

    if ('layout_type' in data) {
      /**
       * The layout type of this poll
       * @type {PollLayoutType}
       */
      this.layoutType = data.layout_type;
    } else {
      this.layoutType ??= null;
    }

    if ('expiry' in data) {
      /**
       * The timestamp when this poll expires
       * @type {?number}
       */
      this.expiresTimestamp = data.expiry && Date.parse(data.expiry);
    } else {
      this.expiresTimestamp ??= null;
    }

    if (data.question) {
      /**
       * The media for a poll's question
       * @typedef {Object} PollQuestionMedia
       * @property {?string} text The text of this question
       */

      /**
       * The media for this poll's question
       * @type {PollQuestionMedia}
       */
      this.question = {
        text: data.question.text,
      };
    } else {
      this.question ??= {
        text: null,
      };
    }