Skip to content

Refactor (packages/web/src/components/share/part.tsx): Function with high complexity - #23

Open
sissizh3n wants to merge 4 commits into
CMU-313:mainfrom
sissizh3n:fix-flatten-tool-args
Open

Refactor (packages/web/src/components/share/part.tsx): Function with high complexity#23
sissizh3n wants to merge 4 commits into
CMU-313:mainfrom
sissizh3n:fix-flatten-tool-args

Conversation

@sissizh3n

@sissizh3n sissizh3n commented Sep 1, 2026

Copy link
Copy Markdown

P1B: Starter Task: Refactoring PR

Use this pull request template to briefly answer the questions below in one to two sentences each.
Feel free to delete this text at the top after filling out the template.

1. Issue

Link to the associated GitHub issue:
#17

Full path to the refactored file:
packages/web/src/components/share/part.tsx

What do you think this file does?
(Your answer does not have to be 100% correct; give a reasonable, evidence‑based guess.)
This file matches data passed as PartProps to corresponding HTML blocks on the UI.

What is the scope of your refactoring within that file?
(Name specific functions/blocks/regions touched.)
Edited functions: flattenToolArgs()
Added helper functions: walk(), formatToolArgValue()
Lines changed (shown in diff): 758 - 794

Which Qlty‑reported issue did you address?
(Name the rule/metric and include the BEFORE value; e.g., “Cognitive Complexity 18 in render()”.)
Function with high complexity (count = 21) in flattenToolArgs()

2. Refactoring

How did the specific issue you chose impact the codebase’s maintainability?

  • flattenToolArgs() checked if obj was nested in 3 places in function body.
  • flattenToolArgs() takes obj with unknown type, so there's no contract on what data type we stop flattening (i.e. what data type counts as a single leaf node that's no longer nested.)

What changes did you make to resolve the issue?

  • Delegated recursion logic to helper walk(), adding the conditional check as a base case at the top of walk()
  • Wrote formatToolArgValue() to isolate formatting logic, adding JSON.stringify for non-primitive data types.
  • Removed old implementation of flattenToolArgs()

How do your changes improve maintainability? Did you consider alternatives?

  • Refactored code fix the check in 1 place in the helper function walk(). If a new leaf node data type is added, there's only 1 if conditional to check instead of 3, reducing probability of missing one of the if branches.
  • Formatting logic is separate from flattening logic. A new helper owns how display text is rendered. If text formatting is altered in the future we only need to check the helper.
    Alternatives: refactoring the function to an iterative implementation instead of recursion. I didn't implement this because it addresses scalability (stack overflow if the input is deeply nested / very large), not maintainability.

3. Validation

How did you validate that the change is correct?
By writing tests in part.test.tsx in the same directory.

Attach a screenshot of the test coverage showing the lines were executed by the tests.
Screenshot 2026-08-31 at 10 53 12 PM
Note: Screenshot below displays that the uncovered lines in part.tsx does not include lines for my implemented changes.
Screenshot 2026-08-31 at 10 54 30 PM
Screenshot 2026-08-31 at 10 54 44 PM

Attach a screenshot showing the tests that cover the change passing during CI
Screenshot 2026-08-31 at 11 25 15 PM
Screenshot 2026-09-01 at 9 51 25 PM

Attach a screenshot of qlty smells --no-snippets <full/path/to/file.ts> showing fewer reported issues after the changes.
Before: (main branch)
Screenshot 2026-08-31 at 11 16 38 PM

After: (fix-flatten-tool-args branch)
Screenshot 2026-08-31 at 11 06 17 PM

Additional GradeScope Screenshots
bun lint and bun test passing locally
Screenshot 2026-09-01 at 9 41 53 PM
Screenshot 2026-09-01 at 9 35 55 PM

@sissizh3n sissizh3n changed the title Fix flatten tool args Fix flatten tool args: packages/web/src/components/share/part.tsx Sep 1, 2026
@sissizh3n sissizh3n changed the title Fix flatten tool args: packages/web/src/components/share/part.tsx Refactor (packages/web/src/components/share/part.tsx): Function with high complexity Sep 2, 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