Skip to content

fix: ignore None flags on serialization of bitfields#11563

Open
Qjuh wants to merge 2 commits into
discordjs:mainfrom
Qjuh:fix/ignore-none-flags
Open

fix: ignore None flags on serialization of bitfields#11563
Qjuh wants to merge 2 commits into
discordjs:mainfrom
Qjuh:fix/ignore-none-flags

Conversation

@Qjuh

@Qjuh Qjuh commented Jun 28, 2026

Copy link
Copy Markdown
Member

Makes discord.js mainlib and @discordjs/structures robust for the change in discordjs/discord-api-types#1705

@Qjuh Qjuh requested review from a team as code owners June 28, 2026 07:18
@Qjuh Qjuh requested review from almeidx, ckohen, iCrawl and vladfrangu June 28, 2026 07:18
@Qjuh Qjuh requested review from SpaceEEC and kyranet June 28, 2026 07:18
@vercel

vercel Bot commented Jun 28, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

2 Skipped Deployments
Project Deployment Actions Updated (UTC)
discord-js Skipped Skipped Jun 28, 2026 7:37am
discord-js-guide Skipped Skipped Jun 28, 2026 7:37am

Request Review

@coderabbitai

coderabbitai Bot commented Jun 28, 2026

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

In both packages/discord.js/src/util/BitField.js and packages/structures/src/bitfields/BitField.ts, serialize() and [Symbol.iterator]() switch from Object.keys to Object.entries on Flags, adding a > 0 check on the bit value to exclude zero-value entries from serialization and iteration.

BitField zero-value flag filtering

Layer / File(s) Summary
Add >0 guard in serialize and iterator
packages/discord.js/src/util/BitField.js, packages/structures/src/bitfields/BitField.ts
Both serialize() and [Symbol.iterator]() switch to Object.entries(Flags) and add a value > 0 condition alongside the existing non-numeric name filter, preventing zero-value flags from appearing in serialized output or iteration.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~5 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly matches the main change: ignoring None flags during bitfield serialization.
Description check ✅ Passed The description is directly related to the bitfield serialization fix and the linked api-types change.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/structures/src/bitfields/BitField.ts`:
- Around line 177-179: The iterator guard in BitField’s flag iteration is using
the wrong condition, so zero-valued flags like None are not being filtered out.
Update the logic inside BitField’s iterator (the loop over
this.constructor.Flags) so the zero-value check is applied directly to the flag
value before calling this.has(...), and only yield flags whose numeric/bigint
value is greater than zero. Make sure the fix preserves the existing hasParams
behavior and prevents toArray() from emitting zero-bit flags.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: cf1caa20-2c5a-44f9-be37-e8a0eea9c905

📥 Commits

Reviewing files that changed from the base of the PR and between b9c59f7 and b5bdf6f.

📒 Files selected for processing (2)
  • packages/discord.js/src/util/BitField.js
  • packages/structures/src/bitfields/BitField.ts
📜 Review details
⏰ Context from checks skipped due to timeout. (1)
  • GitHub Check: Tests
🧰 Additional context used
🧠 Learnings (5)
📚 Learning: 2026-03-30T11:04:39.419Z
Learnt from: almeidx
Repo: discordjs/discord.js PR: 11471
File: packages/discord.js/src/util/Util.js:83-86
Timestamp: 2026-03-30T11:04:39.419Z
Learning: For the discord.js and rest packages, follow the existing convention for constructing request URLs: use the `api` base URL and `version` string via raw interpolation (e.g., `${api}/v${version}${route}`) without adding normalization such as trimming trailing slashes on `api` or stripping/adding/remapping a leading `v` on `version`. Do not recommend changing this behavior during code review unless the existing pattern in this repo is intentionally being replaced.

Applied to files:

  • packages/discord.js/src/util/BitField.js
📚 Learning: 2026-05-18T13:40:11.014Z
Learnt from: almeidx
Repo: discordjs/discord.js PR: 11530
File: packages/core/src/api/user.ts:190-190
Timestamp: 2026-05-18T13:40:11.014Z
Learning: When reviewing discord.js (and related) source files, JSDoc `see` links that point to Discord’s documentation on `https://docs.discord.com/developers/...` are correct and should not be flagged as inconsistent. For new/updated links going forward, prefer `https://docs.discord.com/developers` over the legacy `https://discord.com/developers/docs` domain.

Applied to files:

  • packages/discord.js/src/util/BitField.js
  • packages/structures/src/bitfields/BitField.ts
📚 Learning: 2026-05-22T17:29:55.128Z
Learnt from: kshitijanurag
Repo: discordjs/discord.js PR: 11537
File: packages/discord.js/src/structures/Guild.js:1486-1495
Timestamp: 2026-05-22T17:29:55.128Z
Learning: When handling Discord Gateway opcode `RequestChannelInfo` (opcode name: `RequestChannelInfo` / payload gateway opcode), do not add or suggest a `nonce` field for correlating responses. This opcode’s payload supports only `guild_id` and `fields` (e.g., `fields: ["status", "voice_start_time"]`); ensure any review feedback reflects that schema and treats `nonce` as unsupported for this opcode.

Applied to files:

  • packages/discord.js/src/util/BitField.js
📚 Learning: 2026-01-24T21:41:15.330Z
Learnt from: Qjuh
Repo: discordjs/discord.js PR: 11393
File: packages/structures/src/bitfields/ApplicationFlagsBitField.ts:13-15
Timestamp: 2026-01-24T21:41:15.330Z
Learning: For BitField classes, preserve numeric flags by using super.toJSON(true) when you need a numeric value. The toJSON(asNumber?: boolean) method returns a number when asNumber is true and a string otherwise, so always pass true to obtain a numeric representation for storage/comparison. This guidance applies to similar bitfield classes across the codebase (not just ApplicationFlagsBitField).

Applied to files:

  • packages/structures/src/bitfields/BitField.ts
📚 Learning: 2026-05-18T13:40:11.014Z
Learnt from: almeidx
Repo: discordjs/discord.js PR: 11530
File: packages/core/src/api/user.ts:190-190
Timestamp: 2026-05-18T13:40:11.014Z
Learning: When reviewing JSDoc comments in the discord.js monorepo (e.g., discord.js and related packages), treat `see` links to Discord developer docs as consistent with the new documentation domain. Prefer `https://docs.discord.com/developers/...` over `https://discord.com/developers...`, and do not flag `see` links that already use `https://docs.discord.com/developers/...` as inconsistent.

Applied to files:

  • packages/structures/src/bitfields/BitField.ts
🔇 Additional comments (2)
packages/discord.js/src/util/BitField.js (1)

134-134: LGTM!

Also applies to: 159-160

packages/structures/src/bitfields/BitField.ts (1)

141-142: LGTM!

Comment thread packages/structures/src/bitfields/BitField.ts
@codecov

codecov Bot commented Jun 28, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 31.89%. Comparing base (b9c59f7) to head (d5961eb).

Files with missing lines Patch % Lines
packages/structures/src/bitfields/BitField.ts 0.00% 5 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #11563      +/-   ##
==========================================
- Coverage   31.90%   31.89%   -0.01%     
==========================================
  Files         390      390              
  Lines       14067    14069       +2     
  Branches     1107     1107              
==========================================
  Hits         4488     4488              
- Misses       9441     9443       +2     
  Partials      138      138              
Flag Coverage Δ
structures 35.18% <0.00%> (-0.05%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@vercel vercel Bot temporarily deployed to Preview – discord-js June 28, 2026 07:37 Inactive
@vercel vercel Bot temporarily deployed to Preview – discord-js-guide June 28, 2026 07:37 Inactive
const serialized: Partial<Record<keyof Flags, boolean>> = {};
for (const [flag, bit] of Object.entries(this.constructor.Flags)) {
if (Number.isNaN(Number(flag))) serialized[flag as keyof Flags] = this.has(bit as bigint | number, ...hasParams);
if (Number.isNaN(Number(flag)) && (bit as bigint | number) > 0)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will not work if bit is bigint. You'll need to either convert to number or bigint and do the check

@Qjuh Qjuh Jun 28, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-project-automation github-project-automation Bot moved this from Todo to Review in Progress in discord.js Jun 28, 2026
@Qjuh Qjuh requested a review from vladfrangu June 28, 2026 17:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Review in Progress

Development

Successfully merging this pull request may close these issues.

2 participants