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

Code before:
     */

    Object.defineProperty(this, 'message', { value: message });

    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;
    }

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

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

    /**
     * The timestamp when this poll expires
     * @type {?number}
     */
    this.expiresTimestamp ??= data.expiry ? Date.parse(data.expiry) : 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}

Review comment (suggestion):
Since this has a `?? null` -> type can be nullable

Code after:

    Object.defineProperty(this, 'message', { value: message });

    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 ('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;
    }
Repository: discordjs/discord.js
File: packages/discord.js/src/structures/Poll.js (JavaScript)

Code before:

  _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;
    }

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

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

    /**
     * The timestamp when this poll expires
     * @type {?number}
     */
    this.expiresTimestamp ??= data.expiry ? Date.parse(data.expiry) : 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 ??= {

Review comment (suggestion):
Ditto, but if these are never missing you can just drop the `?? null`

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:
    Object.defineProperty(this, 'message', { value: message });

    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;
    }

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

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

    /**
     * The timestamp when this poll expires
     * @type {?number}
     */
    this.expiresTimestamp ??= data.expiry ? Date.parse(data.expiry) : 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 = {

Review comment (suggestion):
The use of `??=` is confusing to me as we don't do that in any other patch. Is there any reason for this 👀 

Code after:

    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 ('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) {
Repository: discordjs/discord.js
File: packages/discord.js/src/structures/PollAnswer.js (JavaScript)

Code before:
     * @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 this.client.emojis.resolve(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 });
  }
}

exports.PollAnswer = PollAnswer;


Review comment (performance):
Isn't the check for emoji === null redundant? Answers can have no emoji, but we know for a fact that its a partial one if we have no cached text

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

exports.PollAnswer = PollAnswer;
Repository: discordjs/discord.js
File: packages/builders/src/interactions/contextMenuCommands/ContextMenuCommandBuilder.ts (TypeScript)

Code before:
	public readonly dm_permission: boolean | undefined = undefined;

	/**
	 * The integration types for this command.
	 */
	public readonly integration_types?: ApplicationIntegrationType[];

	/**
	 * Adds contexts to this slash command.
	 *
	 * @param contexts - The contexts
	 */
	public addContexts(...contexts: RestOrArray<InteractionContextType>) {
		if (this.contexts === undefined) {
			Reflect.set(this, 'contexts', []);
		}

		this.contexts!.push(...contextsPredicate.parse(normalizeArray(contexts)));

		return this;
	}

	/**
	 * Adds integration types to this slash command.
	 *
	 * @param contexts - The integration types
	 */
	public addIntegrationTypes(...integrationTypes: RestOrArray<ApplicationIntegrationType>) {
		if (this.integration_types === undefined) {
			Reflect.set(this, 'integration_types', []);
		}

		this.integration_types!.push(...integrationTypesPredicate.parse(normalizeArray(integrationTypes)));

		return this;
	}

	/**
	 * Sets the name of this command.
	 *
	 * @param name - The name to use
	 */
	public setName(name: string) {
		// Assert the name matches the conditions
		validateName(name);

		Reflect.set(this, 'name', name);

		return this;
	}


Review comment (suggestion):
```suggestion
	 * @param integrationTypes - The integration types
```

Code after:
	public readonly dm_permission: boolean | undefined = undefined;

	/**
	 * The integration types for this command.
	 */
	public readonly integration_types?: ApplicationIntegrationType[];

	/**
	 * Sets the contexts of this command.
	 *
	 * @param contexts - The contexts
	 */
	public setContexts(...contexts: RestOrArray<InteractionContextType>) {
		Reflect.set(this, 'contexts', contextsPredicate.parse(normalizeArray(contexts)));

		return this;
	}

	/**
	 * Sets integration types of this command.
	 *
	 * @param integrationTypes - The integration types
	 */
	public setIntegrationTypes(...integrationTypes: RestOrArray<ApplicationIntegrationType>) {
		Reflect.set(this, 'integration_types', integrationTypesPredicate.parse(normalizeArray(integrationTypes)));

		return this;
	}

	/**
	 * Sets the name of this command.
	 *
	 * @param name - The name to use
	 */
	public setName(name: string) {
		// Assert the name matches the conditions
		validateName(name);

		Reflect.set(this, 'name', name);

		return this;
	}

	/**
	 * Sets the type of this command.
	 *
	 * @param type - The type to use
	 */
	public setType(type: ContextMenuCommandType) {
		// Assert the type is valid
		validateType(type);
Repository: discordjs/discord.js
File: packages/core/src/api/oauth2.ts (TypeScript)

Code before:
	/**
	 * Fetches the current authorization information
	 *
	 * @see {@link https://discord.com/developers/docs/topics/oauth2#get-current-authorization-information}
	 * @param options - The options for the current authorization information request
	 */
	public async getCurrentAuthorizationInformation({ signal }: Pick<RequestData, 'signal'> = {}) {
		return this.rest.get(Routes.oauth2CurrentAuthorization(), {
			signal,
		}) as Promise<RESTGetAPIOAuth2CurrentAuthorizationResult>;
	}

	/**
	 * Revokes an OAuth2 token
	 * 
	 * @see {@link https://discord.com/developers/docs/topics/oauth2#authorization-code-grant-token-revocation-example}
	 * @param body - The body of the token revocation request
	 * @param options - The options for the token revocation request
	 */
	public async revokeToken(applicationId: Snowflake, applicationSecret: string, body: RESTPostOAuth2TokenRevocation, { signal }: Pick<RequestData, 'signal'> = {}) {
	 return this.rest.post(Routes.oauth2TokenRevocation(), {
	  body: makeURLSearchParams(body),
	  passThroughBody: true,
	  headers: {
	   'Content-Type': 'application/x-www-form-urlencoded',
	   'Authorization': `Basic ${Buffer.from(`${applicationId}:${applicationSecret}`).toString('base64')}`,
	  },
	  auth: false,
	  signal,
	 }) as Promise<{}>;
	}
}


Review comment (security):
We can use the `btoa()` global instead of the node-specific `Buffer.from()`
```suggestion
	   'Authorization': `Basic ${btoa(`${applicationId}:${applicationSecret}`)}`,
```

Code after:
	 * Fetches the current authorization information
	 *
	 * @see {@link https://discord.com/developers/docs/topics/oauth2#get-current-authorization-information}
	 * @param options - The options for the current authorization information request
	 */
	public async getCurrentAuthorizationInformation({ signal }: Pick<RequestData, 'signal'> = {}) {
		return this.rest.get(Routes.oauth2CurrentAuthorization(), {
			signal,
		}) as Promise<RESTGetAPIOAuth2CurrentAuthorizationResult>;
	}

	/**
	 * Revokes an OAuth2 token
	 *
	 * @see {@link https://discord.com/developers/docs/topics/oauth2#authorization-code-grant-token-revocation-example}
	 * @param applicationId - The application id
	 * @param applicationSecret - The application secret
	 * @param body - The body of the token revocation request
	 * @param options - The options for the token revocation request
	 */
	public async revokeToken(
		applicationId: Snowflake,
		applicationSecret: string,
		body: RESTPostOAuth2TokenRevocationQuery,
		{ signal }: Pick<RequestData, 'signal'> = {},
	) {
		await this.rest.post(Routes.oauth2TokenRevocation(), {
			body: makeURLSearchParams(body),
			passThroughBody: true,
			headers: {
				Authorization: `Basic ${btoa(`${applicationId}:${applicationSecret}`)}`,
				'Content-Type': 'application/x-www-form-urlencoded',
			},
			auth: false,
			signal,
		});
	}
}
Repository: discordjs/discord.js
File: packages/core/src/api/oauth2.ts (TypeScript)

Code before:
		return this.rest.get(Routes.oauth2CurrentApplication(), {
			signal,
		}) as Promise<RESTGetAPIOAuth2CurrentApplicationResult>;
	}

	/**
	 * Fetches the current authorization information
	 *
	 * @see {@link https://discord.com/developers/docs/topics/oauth2#get-current-authorization-information}
	 * @param options - The options for the current authorization information request
	 */
	public async getCurrentAuthorizationInformation({ signal }: Pick<RequestData, 'signal'> = {}) {
		return this.rest.get(Routes.oauth2CurrentAuthorization(), {
			signal,
		}) as Promise<RESTGetAPIOAuth2CurrentAuthorizationResult>;
	}

	/**
	 * Revokes an OAuth2 token
	 * 
	 * @see {@link https://discord.com/developers/docs/topics/oauth2#authorization-code-grant-token-revocation-example}
	 * @param body - The body of the token revocation request
	 * @param options - The options for the token revocation request
	 */
	public async revokeToken(applicationId: Snowflake, applicationSecret: string, body: RESTPostOAuth2TokenRevocation, { signal }: Pick<RequestData, 'signal'> = {}) {
	 return this.rest.post(Routes.oauth2TokenRevocation(), {
	  body: makeURLSearchParams(body),
	  passThroughBody: true,
	  headers: {
	   'Content-Type': 'application/x-www-form-urlencoded',
	   'Authorization': `Basic ${Buffer.from(`${applicationId}:${applicationSecret}`).toString('base64')}`,
	  },
	  auth: false,
	  signal,
	 }) as Promise<{}>;
	}
}


Review comment (security):
```suggestion
	 await this.rest.post(Routes.oauth2TokenRevocation(), {
```

Code after:
			signal,
		}) as Promise<RESTGetAPIOAuth2CurrentApplicationResult>;
	}

	/**
	 * Fetches the current authorization information
	 *
	 * @see {@link https://discord.com/developers/docs/topics/oauth2#get-current-authorization-information}
	 * @param options - The options for the current authorization information request
	 */
	public async getCurrentAuthorizationInformation({ signal }: Pick<RequestData, 'signal'> = {}) {
		return this.rest.get(Routes.oauth2CurrentAuthorization(), {
			signal,
		}) as Promise<RESTGetAPIOAuth2CurrentAuthorizationResult>;
	}

	/**
	 * Revokes an OAuth2 token
	 *
	 * @see {@link https://discord.com/developers/docs/topics/oauth2#authorization-code-grant-token-revocation-example}
	 * @param applicationId - The application id
	 * @param applicationSecret - The application secret
	 * @param body - The body of the token revocation request
	 * @param options - The options for the token revocation request
	 */
	public async revokeToken(
		applicationId: Snowflake,
		applicationSecret: string,
		body: RESTPostOAuth2TokenRevocationQuery,
		{ signal }: Pick<RequestData, 'signal'> = {},
	) {
		await this.rest.post(Routes.oauth2TokenRevocation(), {
			body: makeURLSearchParams(body),
			passThroughBody: true,
			headers: {
				Authorization: `Basic ${btoa(`${applicationId}:${applicationSecret}`)}`,
				'Content-Type': 'application/x-www-form-urlencoded',
			},
			auth: false,
			signal,
		});
	}
}
Repository: discordjs/discord.js
File: packages/discord.js/src/managers/VoiceStateManager.js (JavaScript)

Code before:
    this.guild = guild;
  }

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

  _add(data, cache = true) {
    const existing = this.cache.get(data.user_id);
    if (existing) return existing._patch(data);

    const entry = new this.holds(this.guild, data);
    if (cache) this.cache.set(data.user_id, entry);
    return entry;
  }

  /**
   * Obtains a user's voice state from discord or from the cache if it's already available.
   * @param {GuildMemberResolvable} member The member whose voice state is to be fetched
   * @param {BaseFetchOptions} [options] Additional options for this fetch
   * @returns {Promise<VoiceState>}
   * @example
   * // Fetch a member's voice state
   * guild.voiceStates.fetch("851588007697580033")
   *    .then(state => console.log(`Member's voice channel: ${state.channel}`))
   *    .catch(console.error);
   */
  async fetch(member, { cache = true, force = false } = {}) {
    const id = this.guild.members.resolveId(member);
    if (!force) {
      const existing = this.cache.get(id);
      if (existing) return existing;
    }
    const data = await this.client.rest.get(Routes.guildVoiceState(this.guild.id, id));
    return this._add(data, cache);
  }
}

module.exports = VoiceStateManager;


Review comment (suggestion):
The docs uses hydrabolt's id
```suggestion
   * guild.voiceStates.fetch("66564597481480192")
```

Code after:
    this.guild = guild;
  }

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

  _add(data, cache = true) {
    const existing = this.cache.get(data.user_id);
    if (existing) return existing._patch(data);

    const entry = new this.holds(this.guild, data);
    if (cache) this.cache.set(data.user_id, entry);
    return entry;
  }

  /**
   * Obtains a user's voice state from discord or from the cache if it's already available.
   * @param {GuildMemberResolvable|'@me'} member The member whose voice state is to be fetched
   * @param {BaseFetchOptions} [options] Additional options for this fetch
   * @returns {Promise<VoiceState>}
   * @example
   * // Fetch a member's voice state
   * guild.voiceStates.fetch("66564597481480192")
   *    .then(console.log)
   *    .catch(console.error);
   */
  async fetch(member, { cache = true, force = false } = {}) {
    const id = member === '@me' ? member : this.guild.members.resolveId(member);
    if (!force) {
      const existing = this.cache.get(id === '@me' ? this.client.user.id : id);
      if (existing) return existing;
    }
    const data = await this.client.rest.get(Routes.guildVoiceState(this.guild.id, id));
    return this._add(data, cache);
  }
}

module.exports = VoiceStateManager;