GeezerVison is showing github.com — refactor: backlink / unresolved wikilink scan で lineContent を producer 側で 1 度 trim する (#227) by ymnao · Pull Request #235 · ymnao/scripta
Conversation
概要
BacklinkPanel が毎 render reference.lineContent.trim() を呼んでいた所、参照数 × re-render 回数分の短命 string allocation が発生していた。これを producer 側 (iterateWikilinkOccurrences) で 1 度 trim することに集約する (#227)。
PR #234 で確立した backlink / wikilink store の共有化と組み合わせて、scan IPC が返す WikilinkReference.lineContent の semantics を「常に表示用 preview (trim 済)」に統一する。
関連 Issue
Closes #227
移行 Stage
該当なし (Electron 移行は Stage 6 完了済、本変更は v0.6.0 quick win の継続 refactor)
変更内容
採用案: producer 側 (helper-level) で 1 度 trim
初稿は consumer 側 (scanBacklinksImpl) で mutate trim を検討していたが、/code-review max Phase 2 で「iterateWikilinkOccurrences 自体で lineContent: line.trim() する方が strictly better」と CONFIRMED 判定 (verify agent: 既存テスト全 pass / 他 consumer に raw lineContent 期待者なし / diff 純減)。/simplify Phase 2 の Altitude 提案を昇格採用。
採用理由:
- diff 純減: 8 行 comment + mutation 1 行 +
wikilink.ts:21 trim 1 行 = -10 行 / +6 文字
- concept 統一:
WikilinkReference.lineContent semantics が producer 別に分岐しない (常に trim 済)
- mutation pattern 排除: 「short-lived ref を mutate」という precedent を作らない
src/stores/wikilink.ts:21 の .trim() も削除: 「render policy 不一致」として別 issue 候補にしていた seam が消滅 (HANDOFF 39 §2E chore: GitHub リポジトリ初期セットアップ(Dependabot / セキュリティ / 設定) #1 → 消滅)
- e2e mock parity: 本番との parity 確保で ADR-0009 の 2 モード IPC contract が一意化
具体的変更:
electron/main/ipc/search.ts:371 iterateWikilinkOccurrences の lineContent: line → lineContent: line.trim() (5 行 comment で意図明示)
src/components/search/BacklinkPanel.tsx:157 {reference.lineContent.trim()} → {reference.lineContent}
src/stores/wikilink.ts:21 ${ref.lineContent.trim()} → ${ref.lineContent}
e2e/helpers/electron-api-mock.ts:759, 850 mock の scanUnresolvedWikilinks / scanBacklinks も対称に lineContent: line.trim() (本番と parity 確保)
electron/main/ipc/search.test.ts scanBacklinksImpl / scanUnresolvedWikilinksImpl 両方に trim test 1 件ずつ追加 (references.length + lineContent 値 assert)
byteOffset の方針
byteOffset は raw line に対する 1-based UTF-8 位置のままで、trim 後の lineContent との offset 整合は要求しない。byteOffset は BacklinkPanel.tsx:146 / UnresolvedLinksPanel.tsx:145 で ${filePath}-${lineNumber}-${byteOffset} という React key uniqueness token としてのみ使用される (lineContent への slice はしない)。helper 内のコメントで明示。
/simplify で skip / reject した案
- 「mutate を spread
arr.push({...ref, lineContent: ref.lineContent.trim()}) に置き換える」: object 1 alloc 増 / 意図不変。当初 consumer-side trim で実装した時の判断だが、producer-side trim 採用で問題自体が消滅。
- 「prod-code の 6 行 comment を 2 行に圧縮」: 当初 consumer-side mutate で残していた 8 行 comment は producer-side trim 採用で 5 行に圧縮 (mutation 安全性の説明が不要に)。
/code-review max で確認した correctness
- scanBacklinks の
_scanId race-condition: PR refactor: backlink / wikilink store の _scanId race-prevention pattern を createScanAction factory に共通化 (#228) #234 の createScanAction factory で既に保護済、本変更は影響なし
- CommonMark indented code block (
\t[[target]]): 検証 REFUTED — iterateWikilinkOccurrences の test fixture では line 1 が list item ( - list with [[target]]) で line 2 の \t 開始は list-item continuation (paragraph 扱い)、CommonMark / Lezer / scripta いずれも wikilink として認識
- trim() の Unicode 挙動 (U+3000 / BOM strip): PLAUSIBLE (severity: low) — JP markdown の 全角空白 padding を strip する case が起き得るが、SearchPanel との asymmetric は構造的に存在 (matchStart/matchEnd の整合性) で本 PR とは独立、別 issue 候補に記録
title={reference.lineContent} UX: CONFIRMED — tooltip と display が同一文字列 (trim 済) になり、raw indent 確認 UX が消失。Altitude chore: GitHub リポジトリ初期セットアップ(Dependabot / セキュリティ / 設定) #1 (producer-side trim) 採用に伴う許容範囲の UX 微回帰として記録 (raw を残すなら別 field lineContentRaw 案 = /code-review Altitude finding でも premature と判定、回避)
動作確認
ローカル検証 (sandbox EPERM のため e2e は CI):
-
node_modules/.bin/biome check — 5 files 全 pass
-
node_modules/.bin/tsc --noEmit (node + web) — エラーなし
-
node_modules/.bin/tsc --noEmit -p tsconfig.e2e.json — エラーなし
-
node_modules/.bin/vitest run — 98 files / 2228 passed (+1 件 scanUnresolvedWikilinks trim test)
-
node_modules/.bin/electron-vite build — ✓ built
-
pnpm test:e2e (renderer-only) — sandbox EPERM のためローカル不可、CI で確認
-
pnpm test:e2e:electron (実 Electron) — ローカル不可、CI で確認
別 issue 候補 (本 PR scope 外、本セッションで発見)
String.prototype.trim() の Unicode 挙動 vs SearchPanel asymmetry (/code-review Angle D): JP markdown の U+3000 (全角空白) padding を strip する case で、SearchPanel (raw 表示) との見た目不整合が起きる。構造的に SearchPanel 側は trim 不可 (matchStart/matchEnd 整合) なので、解消するなら BacklinkPanel 側を CSS preserve-whitespace + display:contents 等で raw を表示する別アプローチ要。severity low。
BacklinkPanel.tsx:94 の targetPageName useMemo 化 (/code-review Efficiency): basename().slice().normalize("NFC") を毎 render 呼んでいる。useMemo([targetFilePath]) で同 hook の targetFilePath memo と並べる。1 行追加。
BacklinkPanel.tsx:115-117 の basename() + toRelativePath() も per-render → scan-time に hoist 候補 (/code-review Efficiency): BacklinkSource.displayName / displayPath field 案。allocation 効果は trim より小さい (per-source vs per-reference)、優先度低。
search.ts:530-538 の fileMap build で basename().replace(/\.md$/i, "").normalize("NFC") (/code-review Efficiency): regex .replace を endsWith('.md') + slice(0, -3) に置換可能 (line 512 で既に採用済の pattern)、1k file workspace で +N regex eval / NFC norm。優先度低。
scanBacklinks の cancel test なし (/code-review Phase 3 sweep): scanUnresolvedWikilinks には 3 件あるが scanBacklinks 側にない (pre-existing gap、本 PR と独立)。
BacklinkPanel.test.tsx が存在しない (/code-review Phase 3 sweep): panel 側の renderer-test がない (scripta の convention: panel 単体 test は持たず e2e でカバーしている)。
- same-line
[[target]] [[target]] の trim test なし (/code-review Phase 3 sweep): 1 行に同じ target が 2 回出る case の test。iterateWikilinkOccurrences が ref を毎回新規 alloc している前提を test で lock する案。defensive、優先度低。
🤖 Generated with Claude Code
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
…r 側で 1 度 trim する (#227)
- iterateWikilinkOccurrences で lineContent: line → line.trim()
に変更し、BacklinkPanel / wikilink store (buildInitialContent)
の render-time .trim() を排除
- byteOffset は raw line に対する 1-based UTF-8 位置のままで
unique key 用途に留め、trim との offset 整合は要求しない
- e2e mock (scanUnresolvedWikilinks / scanBacklinks) も対称に
trim 適用 (ADR-0009 の 2 モード parity)
- scanBacklinks / scanUnresolvedWikilinks 両方に
trim assertion + references.length check を追加
Closes #227
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Uh oh!
There was an error while loading. Please reload this page.
This was referenced Jun 26, 2026
ymnao
added a commit
that referenced
this pull request
Jun 26, 2026
- `CHANGELOG.md` に v0.6.0 セクションを追加
- Internal: #206 → #229 (AppLayout useShallow), #208 → #230 (settings
_schemaVersion + MIGRATIONS), #207 → #231 (2 モード e2e 振り分け +
ADR-0009 集約), #225 → #232 (onNavigate wikilink target),
#226 → #233 (useCollapseToggle hook), #228 → #234 (createScanAction
factory), #227 → #235 (producer-side lineContent trim)
- `package.json` の `version` を `0.5.0` → `0.6.0`
- HANDOFF の規約通り、本 commit では `pnpm <script>` を呼ばない
(lockfile / node_modules には触れない)
ymnao
added a commit
that referenced
this pull request
Jun 27, 2026
`iterateWikilinkOccurrences` (search.ts:367-380) は yield 毎に新規 ref オブ
ジェクトを alloc する設計で、PR #235 で producer-side に置いた `line.trim()`
はその「per-yield 新規 alloc」前提に依存している。将来 helper が ref を pool /
reuse する refactor を入れると trim 効果が壊れる可能性があるため、同一行に
`[[target]]` が複数回出る case を defensive な test として lock する。
- `scanBacklinksImpl` describe: 既存 #227 trim test (line 432-443) の隣に追加。
`src.md` に `" see [[target]] and [[target]] in the same line \n"` を書き、
両 ref の `lineContent` が同一 trim 済 string であること、`byteOffset` は別
であることを assert。
- `scanUnresolvedWikilinksImpl` describe: 対称に追加。issue では optional 扱い
だが、既存 #227 trim test が両 consumer にペアで存在する pattern と整合させ
るため両側追加する判断。
production code は無変更 (test 追加のみ、+38 / -0)。
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.