ronantakizawa/codereview-bench huggingface dataset:
Repository: discordjs/discord.js
File: packages/discord.js/src/structures/PollAnswer.js (JavaScript)

Code before:
     * The manager of the voters that voted for this answer
     * @type {PollAnswerVoterManager}
     */
    this.voters = new PollAnswerVoterManager(this, []);

    /**
     * The raw emoji of this answer
     * @name PollAnswer#_emoji
     * @type {?APIPartialEmoji}
     * @private
     */
    Object.defineProperty(this, '_emoji', { value: data.poll_media?.emoji ?? null });

    this._patch(data);
  }

  _patch(data) {
    // This `count` field comes from `poll.results.answer_counts`
    if ('count' in data) {
      /**
       * The amount of votes this answer has
       * @type {number}
       */
      this.voteCount = data.count;
    } else if (data.poll_media?.text) {
      this.text = data.poll_media?.text ?? null;
    } else if (data.poll_media?.emoji) {
      Object.defineProperty(this, '_emoji', { value: data.poll_media?.emoji ?? null });
    } else {
      this.voteCount ??= 0;
    }
  }

  /**
   * The emoji of this answer
   * @type {?(GuildEmoji|Emoji)}
   */
  get emoji() {
    if (!this._emoji || (!this._emoji.id && !this._emoji.name)) return null;
    return this.client.emojis.resolve(this._emoji.id) ?? new Emoji(this.client, this._emoji);
  }

  /**
   * Whether or not this poll answer is a partial.
   * @type {boolean}
   * @readonly
   */
  get partial() {
    return this.poll.partial || (this.text === null && this.emoji === null);
  }


Review comment (suggestion):
```suggestion
      this.text = data.poll_media.text;
```

Code after:
    /**
     * The raw emoji of this answer
     * @name PollAnswer#_emoji
     * @type {?APIPartialEmoji}
     * @private
     */
    Object.defineProperty(this, '_emoji', { value: null });

    this._patch(data);
  }

  _patch(data) {
    // This `count` field comes from `poll.results.answer_counts`
    if ('count' in data) {
      /**
       * The amount of votes this answer has
       * @type {number}
       */
      this.voteCount = data.count;
    } else {
      this.voteCount ??= this.voters.cache.size;
    }

    /**
     * The text of this answer
     * @type {?string}
     */
    this.text ??= data.poll_media?.text ?? null;

    if (data.poll_media?.emoji) {
      Object.defineProperty(this, '_emoji', { value: data.poll_media.emoji });
    }
  }

  /**
   * The emoji of this answer
   * @type {?(GuildEmoji|Emoji)}
   */
  get emoji() {
    if (!this._emoji || (!this._emoji.id && !this._emoji.name)) return null;
    return resolveGuildEmoji(this.client, this._emoji.id) ?? new Emoji(this.client, this._emoji);
  }

  /**
   * Whether this poll answer is a partial.
   * @type {boolean}
   * @readonly
   */
  get partial() {
    return this.poll.partial || (this.text === null && this.emoji === null);
  }
Repository: discordjs/discord.js
File: packages/discord.js/src/structures/PollAnswer.js (JavaScript)

Code before:
     */
    this.voters = new PollAnswerVoterManager(this, []);

    /**
     * The raw emoji of this answer
     * @name PollAnswer#_emoji
     * @type {?APIPartialEmoji}
     * @private
     */
    Object.defineProperty(this, '_emoji', { value: data.poll_media?.emoji ?? null });

    this._patch(data);
  }

  _patch(data) {
    // This `count` field comes from `poll.results.answer_counts`
    if ('count' in data) {
      /**
       * The amount of votes this answer has
       * @type {number}
       */
      this.voteCount = data.count;
    } else if (data.poll_media?.text) {
      this.text = data.poll_media?.text ?? null;
    } else if (data.poll_media?.emoji) {
      Object.defineProperty(this, '_emoji', { value: data.poll_media?.emoji ?? null });
    } else {
      this.voteCount ??= 0;
    }
  }

  /**
   * The emoji of this answer
   * @type {?(GuildEmoji|Emoji)}
   */
  get emoji() {
    if (!this._emoji || (!this._emoji.id && !this._emoji.name)) return null;
    return this.client.emojis.resolve(this._emoji.id) ?? new Emoji(this.client, this._emoji);
  }

  /**
   * Whether or not this poll answer is a partial.
   * @type {boolean}
   * @readonly
   */
  get partial() {
    return this.poll.partial || (this.text === null && this.emoji === null);
  }

  /**
   * Options used for fetching voters of a poll answer.

Review comment (suggestion):
```suggestion
      Object.defineProperty(this, '_emoji', { value: data.poll_media.emoji });
```

Code after:
     * @name PollAnswer#_emoji
     * @type {?APIPartialEmoji}
     * @private
     */
    Object.defineProperty(this, '_emoji', { value: null });

    this._patch(data);
  }

  _patch(data) {
    // This `count` field comes from `poll.results.answer_counts`
    if ('count' in data) {
      /**
       * The amount of votes this answer has
       * @type {number}
       */
      this.voteCount = data.count;
    } else {
      this.voteCount ??= this.voters.cache.size;
    }

    /**
     * The text of this answer
     * @type {?string}
     */
    this.text ??= data.poll_media?.text ?? null;

    if (data.poll_media?.emoji) {
      Object.defineProperty(this, '_emoji', { value: data.poll_media.emoji });
    }
  }

  /**
   * The emoji of this answer
   * @type {?(GuildEmoji|Emoji)}
   */
  get emoji() {
    if (!this._emoji || (!this._emoji.id && !this._emoji.name)) return null;
    return resolveGuildEmoji(this.client, this._emoji.id) ?? new Emoji(this.client, this._emoji);
  }

  /**
   * Whether this poll answer is a partial.
   * @type {boolean}
   * @readonly
   */
  get partial() {
    return this.poll.partial || (this.text === null && this.emoji === null);
  }

  /**
Repository: discordjs/discord.js
File: packages/discord.js/src/structures/PollAnswer.js (JavaScript)

Code before:
    if ('count' in data) {
      /**
       * The amount of votes this answer has
       * @type {number}
       */
      this.voteCount = data.count;
    } else if (data.poll_media?.text) {
      this.text = data.poll_media?.text ?? null;
    } else if (data.poll_media?.emoji) {
      Object.defineProperty(this, '_emoji', { value: data.poll_media?.emoji ?? null });
    } else {
      this.voteCount ??= 0;
    }
  }

  /**
   * The emoji of this answer
   * @type {?(GuildEmoji|Emoji)}
   */
  get emoji() {
    if (!this._emoji || (!this._emoji.id && !this._emoji.name)) return null;
    return this.client.emojis.resolve(this._emoji.id) ?? new Emoji(this.client, this._emoji);
  }

  /**
   * Whether or not this poll answer is a partial.
   * @type {boolean}
   * @readonly
   */
  get partial() {
    return this.poll.partial || (this.text === null && this.emoji === null);
  }

  /**
   * Options used for fetching voters of a poll answer.
   * @typedef {Object} BaseFetchPollAnswerVotersOptions
   * @property {number} [limit] The maximum number of voters to fetch
   * @property {Snowflake} [after] The user id to fetch voters after
   */

  /**
   * Fetches the users that voted for this answer.
   * @param {BaseFetchPollAnswerVotersOptions} [options={}] The options for fetching voters
   * @returns {Promise<Collection<Snowflake, User>>}
   */
  fetchVoters({ after, limit } = {}) {
    return this.voters.fetch({ after, limit });
  }
}

exports.PollAnswer = PollAnswer;

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

Code after:
      this.voteCount = data.count;
    } else {
      this.voteCount ??= this.voters.cache.size;
    }

    /**
     * The text of this answer
     * @type {?string}
     */
    this.text ??= data.poll_media?.text ?? null;

    if (data.poll_media?.emoji) {
      Object.defineProperty(this, '_emoji', { value: data.poll_media.emoji });
    }
  }

  /**
   * The emoji of this answer
   * @type {?(GuildEmoji|Emoji)}
   */
  get emoji() {
    if (!this._emoji || (!this._emoji.id && !this._emoji.name)) return null;
    return resolveGuildEmoji(this.client, this._emoji.id) ?? new Emoji(this.client, this._emoji);
  }

  /**
   * Whether this poll answer is a partial.
   * @type {boolean}
   * @readonly
   */
  get partial() {
    return this.poll.partial || (this.text === null && this.emoji === null);
  }

  /**
   * Options used for fetching voters of a poll answer.
   * @typedef {Object} BaseFetchPollAnswerVotersOptions
   * @property {number} [limit] The maximum number of voters to fetch
   * @property {Snowflake} [after] The user id to fetch voters after
   */

  /**
   * Fetches the users that voted for this answer.
   * @param {BaseFetchPollAnswerVotersOptions} [options={}] The options for fetching voters
   * @returns {Promise<Collection<Snowflake, User>>}
   * @deprecated Use {@link PollAnswerVoterManager#fetch} instead
   */
  fetchVoters({ after, limit } = {}) {
    return this.voters.fetch({ after, limit });
  }
}
Repository: discordjs/discord.js
File: packages/discord.js/src/managers/PollAnswerVoterManager.js (JavaScript)

Code before:
class PollAnswerVoterManager extends CachedManager {
  constructor(answer, iterable) {
    super(answer.client, User, iterable);

    /**
     * The poll answer that this manager belongs to
     * @type {PollAnswer}
     */
    this.answer = answer;
  }

  /**
   * The cache of this manager
   * @type {Collection<Snowflake, User>}
   * @name PollAnswerVoterManager#cache
   */

  /**
   * Options used to fetch voters who voted for this poll answer.
   * @typedef {Object} FetchPollAnswerVotersOptions
   * @property {number} [limit] The maximum amount of users to fetch
   * @property {Snowflake} [after] The user id to fetch voters after
   */

  /**
   * Fetches all the voters that voted on this poll answer. Resolves with a collection of users, mapped by their ids.
   * @param {FetchPollAnswerVotersOptions} [options] Options for fetching the users
   * @returns {Promise<Collection<Snowflake, User>>}
   */
  async fetch({ after, limit }) {
    const poll = this.answer.poll;
    const query = makeURLSearchParams({ limit, after });
    const data = await this.client.rest.get(Routes.pollAnswerVoters(poll.channelId, poll.messageId, this.answer.id), {
      query,
    });

    const users = new Collection();
    for (const rawUser of data) {
      const user = this.client.users._add(rawUser);
      this.cache.set(user.id, user);
      users.set(user.id, user);
    }

    this.answer.voteCount = users.size;

    return users;
  }
}

module.exports = PollAnswerVoterManager;


Review comment (suggestion):
```suggestion
   * Fetches the users that voted on this poll answer. Resolves with a collection of users, mapped by their ids.
```

Code after:
class PollAnswerVoterManager extends CachedManager {
  constructor(answer) {
    super(answer.client, User);

    /**
     * The poll answer that this manager belongs to
     * @type {PollAnswer}
     */
    this.answer = answer;
  }

  /**
   * The cache of this manager
   * @type {Collection<Snowflake, User>}
   * @name PollAnswerVoterManager#cache
   */

  /**
   * Fetches the users that voted on this poll answer. Resolves with a collection of users, mapped by their ids.
   * @param {BaseFetchPollAnswerVotersOptions} [options={}] Options for fetching the users
   * @returns {Promise<Collection<Snowflake, User>>}
   */
  async fetch({ after, limit } = {}) {
    const poll = this.answer.poll;
    const query = makeURLSearchParams({ limit, after });
    const data = await this.client.rest.get(Routes.pollAnswerVoters(poll.channelId, poll.messageId, this.answer.id), {
      query,
    });

    return data.users.reduce((coll, rawUser) => {
      const user = this.client.users._add(rawUser);
      this.cache.set(user.id, user);
      return coll.set(user.id, user);
    }, new Collection());
  }
}

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

Code before:
    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,
      };
    } else if (data.answers) {
      for (const answer of data.answers) {
        const existing = this.answers.get(answer.answer_id);
        if (existing) {
          existing._patch(answer);
        } else {
          this.answers.set(answer.answer_id, new PollAnswer(this.client, answer, this));
        }
      }
    } else if (data.partial) {
      Object.defineProperty(this, '_partial', { value: data.partial });
    } else {
      this.resultsFinalized ??= false;
    }
  }

  /**
   * The date when this poll expires
   * @type {Date}
   * @readonly
   */
  get expiresAt() {

Review comment (suggestion):
I don't believe these should be `else if`s, but rather regular `if`s after the previous if-else that was here

Code after:
      /**
       * 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,
      };
    }

    /**
     * The answers of this poll
     * @type {Collection<number, PollAnswer|PartialPollAnswer>}
     */
    this.answers ??= new Collection();

    if (data.answers) {
      for (const answer of data.answers) {
        const existing = this.answers.get(answer.answer_id);
        if (existing) {
          existing._patch(answer);
        } else {
          this.answers.set(answer.answer_id, new PollAnswer(this.client, answer, this));
        }
      }
    }
  }

  /**
   * The date when this poll expires
   * @type {?Date}
   * @readonly
Repository: discordjs/discord.js
File: packages/discord.js/src/structures/Poll.js (JavaScript)

Code before:
    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 {
      this.resultsFinalized ??= false;
    }

    if (data.question) {
      this.question = {
        text: data.question.text,
      };
    }

    if (data.answers) {
      for (const answer of data.answers) {
        const existing = this.answers.get(answer.answer_id);
        if (existing) {
          existing._patch(answer);
        } else {
          this.answers.set(answer.answer_id, new PollAnswer(this.client, answer, this));
        }
      }
    }

    if (data.partial) {
      Object.defineProperty(this, '_partial', { value: data.partial });
    }
  }

  /**
   * The date when this poll expires
   * @type {?Date}
   * @readonly
   */
  get expiresAt() {
    return this.expiresTimestamp && new Date(this.expiresTimestamp);
  }

  /**

Review comment (suggestion):
Remove the `this.answers` update from the constructor in favor of this one. (You'll also need to move the initial collection set here)

Code after:
      /**
       * 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,
      };
    }

    /**
     * The answers of this poll
     * @type {Collection<number, PollAnswer|PartialPollAnswer>}
     */
    this.answers ??= new Collection();

    if (data.answers) {
      for (const answer of data.answers) {
        const existing = this.answers.get(answer.answer_id);
        if (existing) {
          existing._patch(answer);
        } else {
          this.answers.set(answer.answer_id, new PollAnswer(this.client, answer, this));
        }
      }
    }
  }

  /**
   * The date when this poll expires
   * @type {?Date}
   * @readonly
   */
  get expiresAt() {
    return this.expiresTimestamp && new Date(this.expiresTimestamp);
  }

  /**
   * Whether this poll is a partial
   * @type {boolean}
   * @readonly
   */
Repository: discordjs/discord.js
File: packages/discord.js/src/structures/Poll.js (JavaScript)

Code before:
      for (const answer of data.answers) {
        const existing = this.answers.get(answer.answer_id);
        if (existing) {
          existing._patch(answer);
        } else {
          this.answers.set(answer.answer_id, new PollAnswer(this.client, answer, this));
        }
      }
    }

    if (data.partial) {
      Object.defineProperty(this, '_partial', { value: data.partial });
    }
  }

  /**
   * The date when this poll expires
   * @type {?Date}
   * @readonly
   */
  get expiresAt() {
    return this.expiresTimestamp && new Date(this.expiresTimestamp);
  }

  /**
   * Whether or not this poll is a partial
   * @type {boolean}
   * @readonly
   */
  get partial() {
    return this._partial || typeof this.layoutType !== 'number' || typeof this.allowMultiselect !== 'boolean';
  }

  /**
   * Fetches the message that started this poll, then updates the poll from the fetched message.
   * @returns {Promise<Poll>}
   */
  async fetch() {
    await this.channel.messages.fetch(this.messageId);

    return this;
  }

  /**
   * Ends this poll.
   * @returns {Promise<Message>}
   */
  end() {
    if (Date.now() > this.expiresTimestamp) {
      return Promise.reject(new DiscordjsError(ErrorCodes.PollAlreadyExpired));
    }

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

Code after:
      for (const answer of data.answers) {
        const existing = this.answers.get(answer.answer_id);
        if (existing) {
          existing._patch(answer);
        } else {
          this.answers.set(answer.answer_id, new PollAnswer(this.client, answer, this));
        }
      }
    }
  }

  /**
   * The date when this poll expires
   * @type {?Date}
   * @readonly
   */
  get expiresAt() {
    return this.expiresTimestamp && new Date(this.expiresTimestamp);
  }

  /**
   * Whether this poll is a partial
   * @type {boolean}
   * @readonly
   */
  get partial() {
    return this.allowMultiselect === null;
  }

  /**
   * Fetches the message that started this poll, then updates the poll from the fetched message.
   * @returns {Promise<Poll>}
   */
  async fetch() {
    await this.channel.messages.fetch(this.messageId);

    return this;
  }

  /**
   * Ends this poll.
   * @returns {Promise<Message>}
   */
  async end() {
    if (Date.now() > this.expiresTimestamp) {
      throw new DiscordjsError(ErrorCodes.PollAlreadyExpired);
    }

    return this.channel.messages.endPoll(this.messageId);
  }
}
Repository: discordjs/discord.js
File: packages/discord.js/src/managers/PollAnswerVoterManager.js (JavaScript)

Code before:
 * Manages API methods for users who voted on a poll and stores their cache.
 * @extends {CachedManager}
 */
class PollAnswerVoterManager extends CachedManager {
  constructor(answer) {
    super(answer.client, User);

    /**
     * The poll answer that this manager belongs to
     * @type {PollAnswer}
     */
    this.answer = answer;
  }

  /**
   * The cache of this manager
   * @type {Collection<Snowflake, User>}
   * @name PollAnswerVoterManager#cache
   */

  /**
   * Fetches the users that voted on this poll answer. Resolves with a collection of users, mapped by their ids.
   * @param {BaseFetchPollAnswerVotersOptions} [options] Options for fetching the users
   * @returns {Promise<Collection<Snowflake, User>>}
   */
  async fetch({ after, limit }) {
    const poll = this.answer.poll;
    const query = makeURLSearchParams({ limit, after });
    const data = await this.client.rest.get(Routes.pollAnswerVoters(poll.channelId, poll.messageId, this.answer.id), {
      query,
    });

    return data.reduce((coll, rawUser) => {
      const user = this.client.users._add(rawUser);
      this.cache.set(user.id, user);
      return coll.set(user.id, user);
    }, new Collection());
  }
}

module.exports = PollAnswerVoterManager;


Review comment (suggestion):
```suggestion
  async fetch({ after, limit } = {}) {
```

Code after:
 * Manages API methods for users who voted on a poll and stores their cache.
 * @extends {CachedManager}
 */
class PollAnswerVoterManager extends CachedManager {
  constructor(answer) {
    super(answer.client, User);

    /**
     * The poll answer that this manager belongs to
     * @type {PollAnswer}
     */
    this.answer = answer;
  }

  /**
   * The cache of this manager
   * @type {Collection<Snowflake, User>}
   * @name PollAnswerVoterManager#cache
   */

  /**
   * Fetches the users that voted on this poll answer. Resolves with a collection of users, mapped by their ids.
   * @param {BaseFetchPollAnswerVotersOptions} [options={}] Options for fetching the users
   * @returns {Promise<Collection<Snowflake, User>>}
   */
  async fetch({ after, limit } = {}) {
    const poll = this.answer.poll;
    const query = makeURLSearchParams({ limit, after });
    const data = await this.client.rest.get(Routes.pollAnswerVoters(poll.channelId, poll.messageId, this.answer.id), {
      query,
    });

    return data.users.reduce((coll, rawUser) => {
      const user = this.client.users._add(rawUser);
      this.cache.set(user.id, user);
      return coll.set(user.id, user);
    }, new Collection());
  }
}

exports.PollAnswerVoterManager = PollAnswerVoterManager;