Skip to content

Refactor (packages/opencode/src/util/media.ts): Function with many returns - #175

Open
drek2991 wants to merge 1 commit into
CMU-313:mainfrom
drek2991:derek/p1b-media-refactor-clean
Open

Refactor (packages/opencode/src/util/media.ts): Function with many returns#175
drek2991 wants to merge 1 commit into
CMU-313:mainfrom
drek2991:derek/p1b-media-refactor-clean

Conversation

@drek2991

@drek2991 drek2991 commented Sep 8, 2026

Copy link
Copy Markdown

P1B: Starter Task: Refactoring PR

1. Issue

Link to the associated GitHub issue:
#120

Full path to the refactored file:
packages/opencode/src/util/media.ts

What do you think this file does?
This file provides utility functions for identifying and classifying media attachments based on their MIME types and file signature bytes.

What is the scope of your refactoring within that file?
I refactored the sniffAttachmentMime function and added a small MIME signature structure used by that function. The existing isPdfAttachment, isMedia, and isImageAttachment functions were also covered by new tests but were not refactored.

Which Qlty-reported issue did you address?
I addressed the Qlty Function with many returns issue in sniffAttachmentMime, which had 7 return statements before the refactoring.

2. Refactoring

How did the specific issue you chose impact the codebase’s maintainability?
The multiple early returns made sniffAttachmentMime more difficult to extend and required adding another conditional branch for each new MIME signature.

What changes did you make to resolve the issue?
I replaced the conditional return statements with a MIME_SIGNATURES list containing each supported MIME signature and a matching function, then used .find() to select the first matching signature and return its MIME type.

How do your changes improve maintainability? Did you consider alternatives?
The new structure separates the MIME signatures from the detection logic, making the function shorter and making additional formats easier to add. I considered keeping the conditional structure but chose the signature list because it removes the Qlty issue while keeping the existing detection behavior.

3. Validation

How did you validate that the change is correct?
I added tests for PNG, JPEG, GIF, BMP, PDF, WebP, invalid WebP, and the fallback case, as well as tests for the existing media helper functions. The targeted media test suite passed with 11 tests passing and 0 failing, and the PR's CI checks also passed.

Attach a screenshot of the test coverage showing the lines were executed by the tests.
Screenshot 2026-09-08 at 12 08 00 AM

Attach a screenshot showing the tests that cover the change passing during CI.
Screenshot 2026-09-07 at 11 59 26 PM

Attach a screenshot of qlty smells --no-snippets <full/path/to/file.ts> showing fewer reported issues after the changes.
Screenshot 2026-09-08 at 12 00 25 AM

Screenshot of bun test and bun lint passing locally:
Screenshot 2026-09-08 at 12 00 44 AM

@drek2991 drek2991 changed the title Refactor sniffAttachmentMime to reduce returns Refactor (packages/opencode/src/util/media.ts): Function with many returns Sep 8, 2026
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