Skip to content

fix: dice regex matches bare D1/D0 in sourcebook text (#1126)#1389

Open
0xguy07 wants to merge 1 commit into
kakaroto:masterfrom
0xguy07:fix/d1-d0-false-positive-1126
Open

fix: dice regex matches bare D1/D0 in sourcebook text (#1126)#1389
0xguy07 wants to merge 1 commit into
kakaroto:masterfrom
0xguy07:fix/d1-d0-false-positive-1126

Conversation

@0xguy07

@0xguy07 0xguy07 commented Jun 5, 2026

Copy link
Copy Markdown

Summary

Restrict the bare-die dN/DN form in DICE_REGEXP to N=2–9 or N with two-or-more digits, so impossible bare dice like D0/D1 no longer hijack sourcebook map labels (e.g. A2, B1, C1, D1 in Vecna: Eve of Ruin). Explicit-count forms (1d1, 2d0, …) are unchanged. A standalone npm test script is added covering the bug and existing-behavior regressions.

Type of change

  • 🐛 Bug fix (non-breaking change that fixes an issue)
  • ✨ New feature (non-breaking change that adds functionality)
  • ♻️ Refactor (no functional change)
  • ⚡ Performance improvement
  • 🧪 Tests (adds or updates tests)
  • 📝 Documentation
  • 🔧 Build/CI
  • 🚨 Breaking change

⚠️ AI usage disclosure (required)

  • I did not use AI for this PR.
  • I did use AI for this PR (describe below).

What was AI-assisted: The regex change in src/common/utils.js and the test cases in test/dice_regex.test.js were drafted with Claude (Anthropic, Opus 4.7). I (the PR author) reviewed every change, ran the bundled test script, and confirmed the behavior on the issue's reproducer sentence and on standard dice expressions. No build/CI tooling, documentation, or unrelated code was modified by AI.

This is a resubmission of #1375 — the prior PR was closed after @dmportella asked about AI use; this one discloses it upfront and adds the test coverage that the prior thread noted regex changes need.

Motivation & context

What changed?

  • src/common/utils.js: tightened the bare-die alternative in DICE_REGEXP from \d*[dD]\d+ to \d+[dD]\d+|[dD](?:[2-9]|\d{2,}). Applied in both the primary dice group and the trailing-extra-dice modifier group so compound expressions like 1d8+1d6+3 still parse. Comment added inline explaining the rationale.
  • test/dice_regex.test.js (new): 25-case standalone Node script. Loads DICE_REGEXP directly from src/common/utils.js at runtime (no bundler needed) and exits non-zero on any failure.
  • package.json: added "test": "node test/dice_regex.test.js".

How to test

  1. git fetch && git checkout fix/d1-d0-false-positive-1126
  2. npm test — should print 25/25 passed.
  3. Manual: load the unpacked extension build (npm run build:chrome / :firefox), open the Vecna ritual chamber page, scroll to the doors sentence — D1 should render as text, not a Beyond20 icon. Sanity check a known-good page (e.g. any monster stat block with 1d20 / 2d6+3) and confirm dice buttons still appear.

Reviewer notes

  • Scope is intentionally narrow: only D0 and D1 are excluded. The original issue thread noted D2 also collides with map labels, but d2 is a legitimate (if uncommon) die, so excluding it would regress real dice notation. This PR fixes the impossible-die cases and leaves the D2+ ambiguity as a known trade-off (worth a follow-up if a context-aware approach is wanted).
  • Test framework choice: I deliberately did not pull in Jest/Mocha — the test file is a single ~150-line Node script with zero new dependencies. Happy to migrate it to whatever you prefer if you'd rather grow this in a different direction.
  • AI use: disclosed above; commit body also notes it. I'm the human responsible for the change and have verified the behavior.

Restrict the bare-die form `dN`/`DN` to N=2-9 or N with two-or-more digits.
This excludes `D0`/`D1`, which are not real dice and produced false
positives on sourcebook map labels such as the "doors: A2, B1, C1, D1"
sentence reported in issue kakaroto#1126 (Vecna: Eve of Ruin).

Explicit-count forms (`1d1`, `2d0`, ...) are unchanged so existing
behavior is preserved.

Adds test/dice_regex.test.js with 25 cases covering the bug, regressions
for standard dice (1d20, 2d6+3, d100, d2, d20ro<3, d6min2, +5, mixed),
and embedded-word non-matches. Run with `npm test`.

AI assistance: regex change and tests drafted with Claude (Opus 4.7); all
behavior verified by the bundled test script against current master.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@0xguy07

0xguy07 commented Jun 5, 2026

Copy link
Copy Markdown
Author

@dmportella I ensured I used the correct template this time. I just want to assist in fixing some of the issues which are on the list. We use this extension a lot during our games, and I want to ensure I can help in the ways that I can regarding ensuring that I contribute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant