diff --git a/FINAL_UNUSED_VARIABLES_FIXED.md b/FINAL_UNUSED_VARIABLES_FIXED.md new file mode 100644 index 0000000..fcc6f54 --- /dev/null +++ b/FINAL_UNUSED_VARIABLES_FIXED.md @@ -0,0 +1,85 @@ +# ✅ Unused Variables Fixed - Final Check Complete + +All unused variables and imports have been **removed** from the refactor plan. + +## Fixed Issues + +### 1. ✅ Removed Unused CFI Variables +**File:** `page-calculator.ts`, `generateCFI()` function +**Removed:** +```typescript +// REMOVED these single-use variables: +const stepInto = "!"; // Line 268 +const textNodePath = "/4/2/1"; // Line 272 +const charOffsetPart = `:${offset}`; // Line 275 +const spinePath = `/6/${spineIndex + 2}${escapedId}`; // Now inlined +``` +**Now:** All inlined into the return statement +```typescript +return `epubcfi(${spinePath}!/4/2/1:${offset})`; +``` + +### 2. ✅ Removed Unused parseCFI Variables +**File:** `page-calculator.ts`, `parseCFI()` function +**Removed:** +```typescript +// REMOVED: +const spinePath = parts[0]; // Line 295 +const contentPath = parts[1]; // Line 296 +``` +**Now:** Used directly in match() calls +```typescript +const spineMatch = parts[0].match(/\/6\/(\d+)/); +const offsetMatch = parts[1].match(/:(\d+)$/); +``` + +### 3. ✅ Removed Unused beforeText Variable +**File:** `page-calculator.ts`, `extractHTMLSlice()` function +**Removed:** +```typescript +// REMOVED: +let beforeText = ""; // Line 501 +beforeText = text.substring(0, charStart - startChar); // Line 505 +``` +**Reason:** Only `after` is used in the output (line 514), `before` property was set but never read + +### 4. ✅ Removed Unused range Variable +**File:** `page-calculator.ts`, `extractHTMLSlice()` function +**Removed:** +```typescript +// REMOVED: +const range = doc.createRange(); // Line 518 +``` +**Reason:** Created but never used in the extraction logic + +### 5. ✅ Fixed Bug: getPageContent Spine Lookup +**File:** `page-calculator.ts`, `getPageContent()` function +**Before (BUGGY):** +```typescript +const spine = pagination.spineMap.get(page.charStart); // Wrong: charStart is not a spine index +``` +**After (FIXED):** +```typescript +// Find the spine that contains this page by checking its pages array +let spine: SpineInfo | undefined; +for (const s of pagination.spines) { + if (s.pages.some(p => p.pageIndex === pageIndex)) { + spine = s; + break; + } +} +``` +**Impact:** This was a critical bug that would cause spine lookup to fail + +--- + +## Final Verification + +✅ No unused variables in `generateCFI()` +✅ No unused variables in `parseCFI()` +✅ No unused variables in `extractHTMLSlice()` +✅ No unused variables in `getPageContent()` +✅ All imports are necessary +✅ Critical bug fixed in spine lookup + +**The refactor plan is now completely clean with zero unused variables or imports.** diff --git a/UNUSED_VARIABLES_FIXES.md b/UNUSED_VARIABLES_FIXES.md new file mode 100644 index 0000000..8f7b5bc --- /dev/null +++ b/UNUSED_VARIABLES_FIXES.md @@ -0,0 +1,200 @@ +# Unused Variables and Imports in Refactor Plan + +Found 3 issues in the refactor plan that need fixing: + +--- + +## Issue 1: Unused Constant in `page-calculator.ts` + +**File:** `web/src/reader/formats/reflowable/page-calculator.ts` +**Line:** 201 +**Issue:** `AVG_WORD_LENGTH` is declared but never used + +```typescript +// REMOVE THIS LINE: +const AVG_WORD_LENGTH = 5; // For char count estimation +``` + +**Reason:** This constant is declared with a comment saying "For char count estimation" but is never actually used in the code. All character counting is done with `.length` on strings. + +--- + +## Issue 2: Unused Import in `navigation.ts` + +**File:** `web/src/reader/formats/reflowable/navigation.ts` +**Line:** 413 +**Issue:** `shouldRecalculate` is imported but never used + +```typescript +// BEFORE (line 413): +import { getPageContent, findPageByCFI, shouldRecalculate } from "./page-calculator"; + +// AFTER: +import { getPageContent, findPageByCFI } from "./page-calculator"; +``` + +**Reason:** `shouldRecalculate` is used in `reader-shell.ts` (line 1197) but not in `navigation.ts`. It doesn't need to be imported here. + +--- + +## Issue 3: Inconsistent Import Style in `progress-tracker.ts` + +**File:** `web/src/reader/formats/reflowable/progress-tracker.ts` +**Lines:** 601, 608 +**Issue:** Uses `require()` inline instead of ES6 imports at top + +```typescript +// BEFORE (lines 544-604): +import type { ReflowableBook, ReadingPosition } from "./types"; + +export function restorePosition( + book: ReflowableBook, + savedCFI: string, + savedPage?: number +): ReadingPosition { + if (!book.pagination) { + return book.position; + } + + // If we have saved CFI, try to find exact position + if (savedCFI) { + const { findPageByCFI, createPositionFromPage } = require("./page-calculator"); + const pageNum = findPageByCFI(book.pagination, savedCFI); + return createPositionFromPage(book, pageNum); + } + + // Otherwise use saved page number + if (savedPage && savedPage > 0) { + const { createPositionFromPage } = require("./page-calculator"); + return createPositionFromPage(book, savedPage); + } + + return book.position; +} + +// AFTER: +import type { ReflowableBook, ReadingPosition } from "./types"; +import { findPageByCFI } from "./page-calculator"; +import { createPositionFromPage } from "./navigation"; + +export function restorePosition( + book: ReflowableBook, + savedCFI: string, + savedPage?: number +): ReadingPosition { + if (!book.pagination) { + return book.position; + } + + // If we have saved CFI, try to find exact position + if (savedCFI) { + const pageNum = findPageByCFI(book.pagination, savedCFI); + return createPositionFromPage(book, pageNum); + } + + // Otherwise use saved page number + if (savedPage && savedPage > 0) { + return createPositionFromPage(book, savedPage); + } + + return book.position; +} +``` + +**Wait, there's a circular dependency issue here!** + +- `progress-tracker.ts` imports from `navigation.ts` (for `createPositionFromPage`) +- `navigation.ts` imports from `progress-tracker.ts` (for `updateCurrentPosition`) + +**Better fix:** Move `createPositionFromPage` to `page-calculator.ts` where it's used: + +```typescript +// In page-calculator.ts, ADD at the end: +export function createPositionFromPage( + book: ReflowableBook, + page: number +): ReadingPosition { + if (!book.pagination) { + return { + currentPage: 1, + spineIndex: 0, + localPageIndex: 0, + cfi: "", + progress: 0, + }; + } + + const pageIndex = page - 1; + const pageData = book.pagination.pageMap.get(pageIndex); + + if (!pageData) { + return { + currentPage: 1, + spineIndex: 0, + localPageIndex: 0, + cfi: "", + progress: 0, + }; + } + + // Find which spine this page belongs to + let spineIndex = 0; + for (const spine of book.pagination.spines) { + if (pageData.localPageIndex < spine.pages.length) { + spineIndex = spine.spineIndex; + break; + } + } + + return { + currentPage: page, + spineIndex, + localPageIndex: pageData.localPageIndex, + cfi: pageData.cfi, + progress: book.pagination.totalPages > 0 ? page / book.pagination.totalPages : 0, + }; +} +``` + +Then update `progress-tracker.ts`: + +```typescript +import type { ReflowableBook, ReadingPosition } from "./types"; +import { findPageByCFI, createPositionFromPage } from "./page-calculator"; +``` + +And update `navigation.ts` to import it from page-calculator: + +```typescript +import type { PaginationData, ReadingPosition, ReflowableBook } from "./types"; +import { getPageContent, findPageByCFI, createPositionFromPage } from "./page-calculator"; +``` + +--- + +## Summary of Fixes + +1. **Delete** `AVG_WORD_LENGTH` constant from `page-calculator.ts` (line 201) +2. **Remove** `shouldRecalculate` from import in `navigation.ts` (line 413) +3. **Move** `createPositionFromPage` from `navigation.ts` to `page-calculator.ts` +4. **Update** imports in `progress-tracker.ts` to use ES6 imports instead of require() +5. **Update** imports in `navigation.ts` to import `createPositionFromPage` from page-calculator + +--- + +## Updated File Changes + +### page-calculator.ts +- Remove line 201 (`AVG_WORD_LENGTH`) +- Add `createPositionFromPage` function at the end (before the `shouldRecalculate` function) + +### navigation.ts +- Remove `shouldRecalculate` from import on line 413 +- Change import of `createPositionFromPage` to come from `page-calculator` +- Remove the `createPositionFromPage` function definition (lines 470-507) + +### progress-tracker.ts +- Change lines 544-545 to add proper imports +- Remove `require()` statements on lines 601 and 608 + +These changes will fix all unused variables/imports and resolve the circular dependency issue.