Repository navigation
useTTSText のアナウンステンプレートをミニエンジン化してリファクタリング - #5916
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughテンプレートエンジンとテンプレート定義、言語別フォーマッターを追加し、useTTSText を TemplateContext を使うテンプレートベースの出力に差し替えています。 変更内容TTS テンプレートエンジン統合
Sequence DiagramsequenceDiagram
participant Hook as useTTSText
participant Formatters as formatters.ts
participant Engine as templateEngine.compile
participant Templates as JA/EN_TEMPLATES
participant Output as TTSTextResult
Hook->>Formatters: フラグと断片を生成(replaceJapaneseText等)
Hook->>Engine: TemplateContext を渡してレンダラー実行
Engine->>Templates: テンプレート内で変数参照と if 評価
Templates->>Engine: レンダリング結果(jaText/enText/nextText)を返す
Engine->>Output: 最終 TTS テキストを提供
推定コード確認労力🎯 4 (Complex) | ⏱️ ~45 分 詩
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 4
🤖 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 `@src/hooks/tts/formatters.ts`:
- Around line 92-95: formatFirstConnectedLineEnPhrase currently early-returns
when first.nameTtsSegments is missing, which drops lines that only have
nameRoman; change the guard to base the decision on the rendered text: call
wrapPhoneme(first.nameTtsSegments, first.nameRoman) into a local (e.g.
renderedName) and return '' only if renderedName is falsy, otherwise return `on
the ${renderedName}`; reference symbols: formatFirstConnectedLineEnPhrase,
lines, first.nameTtsSegments, first.nameRoman, wrapPhoneme.
- Around line 5-17: The function replaceJapaneseText currently hardcodes the
fallback '<sub alias="かくえきていしゃ">各駅停車</sub>', causing unrelated outputs for
station/line/company names; change replaceJapaneseText to not embed a fixed
fallback: accept an explicit fallback parameter (string | undefined) or return
an empty string when both name and nameKatakana are missing, keep the
katakanaToHiragana behavior for the alias branch, and then update callers that
need "各駅停車" (e.g., trainTypeJa / trainTypeJaPlain / trainTypeJaKyushu) to pass
that specific fallback explicitly.
In `@src/hooks/tts/templateEngine.ts`:
- Around line 71-77: The current {:else} handling silently overwrites a previous
alternate by assigning ifNode.alternate = [] even when an alternate already
exists; change the logic in the {:else} branch (the block that reads tag ===
'{:else}' and references top.ifNode, ifNode.alternate, and stack) to first check
whether ifNode.alternate is already set and, if so, throw a syntax Error (e.g.,
"{:else} already defined for this {`#if`}") instead of replacing it; otherwise,
initialize ifNode.alternate = [] and push the new stack frame as before.
In `@src/hooks/useTTSText.ts`:
- Around line 285-316: The context builder in the useMemo (function useTTSText)
must not construct a context when nextStation is unresolved; update the
early-exit condition to return null if nextStation is falsy (in addition to
currentLine and selectedBound) so no downstream fields (e.g., nextStationJa
which calls replaceJapaneseText) are evaluated with undefined values; locate the
useMemo that returns the TemplateContext and add a guard like `if (!currentLine
|| !selectedBound || !nextStation) return null`, or otherwise ensure nextStation
is checked before calling replaceJapaneseText and initializing nextStationJa.
🪄 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: CHILL
Plan: Pro
Run ID: 3c560c32-fa92-4b94-a835-1097e63e3961
📒 Files selected for processing (5)
src/hooks/tts/formatters.tssrc/hooks/tts/templateEngine.test.tssrc/hooks/tts/templateEngine.tssrc/hooks/tts/templates.tssrc/hooks/useTTSText.ts
CodeRabbit Autofix SummaryCodeRabbit から指摘された 4 件をレビューし、1 件を本 PR で適用、残り 3 件は別チケットに切り出しました。 Applied (1 件)
Commit: Deferred (3 件) → Issue #5917以下 3 件は本 PR のリファクタ前から存在する既存挙動で、純粋リファクタとして byte-for-byte 維持するため別 issue (#5917) にまとめました:
|
概要
useTTSTextの巨大化していたテンプレートリテラル群を、極小テンプレートエンジン + テーマ別宣言的テンプレート + リスト整形ヘルパに分離。挙動は byte-for-byte で維持し、既存のスナップショットテスト 31 件すべて pass。変更の種類
変更内容
src/hooks/tts/templateEngine.ts:{var}と{#if/:else/}のみサポートする ~80 行のミニエンジンを新規追加 (反復は意図的に未サポート、配列は呼び出し側で文字列化して渡す)src/hooks/tts/templates.ts: テーマ別の NEXT/ARRIVING テンプレートを宣言的に定義 (JA/EN それぞれ)src/hooks/tts/formatters.ts:formatLinesListJa/formatLinesListEn/formatJrWestStopsList*等のリスト整形ヘルパを新規追加src/hooks/tts/templateEngine.test.ts: エンジン単体の挙動テスト 20 件を追加src/hooks/useTTSText.ts: 1381 → 465 行 (約 -66%) に縮小。巨大なjapaneseTemplate/englishTemplateuseMemo を廃止し「context 構築 → テーマ解決 → render」の3ステップに再構成テスト
npm run lintが通ることnpm testが通ることnpm run typecheckが通ること既存の
src/hooks/useTTSText.test.tsx31 件 + 新規templateEngine.test.ts20 件、合計 51 件すべて pass を確認。関連Issue
スクリーンショット(任意)
Summary by CodeRabbit
New Features
Refactor
Tests