Conversation
Q&Aページではcomments/_commentsパーシャルを使わず 独自の回答表示をしているため、#comments.thread-comments.loaded 要素が存在しない。commentContentがnullの場合にearly returnする ガードを追加。
📝 WalkthroughWalkthrough
Changes
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
app/javascript/comments.js (2)
77-98: 任意: より防御的なコーディングを検討
moreCommentButtonに対してイベントリスナーを追加する前に、null チェックを行うとより堅牢になります。現在のガード句により、この要素が存在するページでのみここに到達するはずですが、予期しないDOM構造の変更に対する防御として有効です。♻️ 防御的なチェックの提案
+ if (!moreCommentButton) { + return + } + moreCommentButton.addEventListener('click', () => { const nextComments = []🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@app/javascript/comments.js` around lines 77 - 98, Add a defensive null check before attaching the click handler to moreCommentButton: verify moreCommentButton is non-null (and optionally is an HTMLElement) before calling moreCommentButton.addEventListener, and return/skip if it's absent; keep the existing logic inside the guarded block so variables and calls like setComments, displayMoreComments, commentRemaining, incrementSize, and nextCommentAmount only run when the element exists.
15-18: 任意: より明確な条件チェックを検討
querySelectorAllは空の NodeList を返す場合でも truthy なので、15行目のif (comments)は常に true になります。意図としてはif (comments.length > 0)の方が意味的に明確です。ただし、現状の実装でもforEachは空の場合に何も実行しないため、機能的な問題はありません。♻️ より明確な条件チェックの提案
- if (comments) { + if (comments.length > 0) { loadingContent.classList.add('is-hidden') commentContent.classList.remove('is-hidden') }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@app/javascript/comments.js` around lines 15 - 18, The current check uses the NodeList variable comments (from querySelectorAll) in if (comments), which is always truthy; update the condition to explicitly check for items, e.g. if (comments.length > 0) (or Boolean(comments.length)), so the block that hides loadingContent and shows commentContent only runs when comment nodes exist; locate where comments is used alongside loadingContent and commentContent in app/javascript/comments.js and replace the truthy check accordingly.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@app/javascript/comments.js`:
- Around line 77-98: Add a defensive null check before attaching the click
handler to moreCommentButton: verify moreCommentButton is non-null (and
optionally is an HTMLElement) before calling moreCommentButton.addEventListener,
and return/skip if it's absent; keep the existing logic inside the guarded block
so variables and calls like setComments, displayMoreComments, commentRemaining,
incrementSize, and nextCommentAmount only run when the element exists.
- Around line 15-18: The current check uses the NodeList variable comments (from
querySelectorAll) in if (comments), which is always truthy; update the condition
to explicitly check for items, e.g. if (comments.length > 0) (or
Boolean(comments.length)), so the block that hides loadingContent and shows
commentContent only runs when comment nodes exist; locate where comments is used
alongside loadingContent and commentContent in app/javascript/comments.js and
replace the truthy check accordingly.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: bf2d23ec-3bb1-4427-8d73-360e82bbaf25
📒 Files selected for processing (1)
app/javascript/comments.js
🚀 Review AppURL: https://bootcamp-pr-9829-fvlfu45apq-an.a.run.app
|
問題
Q&Aの個別ページにアクセスすると以下のJavaScriptエラーがConsoleに出る:
原因
comments.jsが#comments.thread-comments.loaded要素をquerySelectorで取得しているが、Q&Aページではcomments/_commentsパーシャルを使わず独自の回答表示(.answer-content)をしているため、この要素が存在しない。結果としてcommentContentがnullになりclassList.removeでTypeErrorが発生。修正
commentContentがnullの場合に early return するガードを追加。Q&Aページのようにcomments構造が異なるページではcomments.jsの処理をスキップする。Summary by CodeRabbit