Skip to content

Conversation

@ryoppippi
Copy link
Owner

@ryoppippi ryoppippi commented Sep 14, 2025

Summary

This PR introduces logger injection capability to the ResponsiveTable class, improving testability and flexibility while maintaining backward compatibility.

What Changed

  • Added optional logger parameter to TableOptions type with signature (message: string) => void
  • Updated ResponsiveTable constructor to accept the injected logger and default to logger.warn
  • Replaced hardcoded logger.warn call with the injected logger instance in warning scenarios
  • Enhanced test coverage by using a mock logger function instead of spying on the global logger

Why

  • Improved Testability: Tests can now inject custom loggers without relying on global state
  • Better Test Isolation: Removes dependency on global logger, making tests more predictable
  • Enhanced Flexibility: Different table instances can use different logging behaviors
  • Backward Compatibility: Changes are completely backward compatible with optional parameter

Testing

  • All existing tests continue to pass (323/323 tests passing)
  • Updated test now uses proper dependency injection with mock logger
  • No breaking changes to existing API surface
  • Pre-commit hooks passed successfully (linting, type checking, tests)

Related Issues

This addresses dependency injection concerns and improves the overall architecture of the ResponsiveTable class by following better software engineering practices.

Summary by CodeRabbit

  • New Features
    • Added an optional logger to table options, allowing custom handling of warning messages (e.g., for missing compact headers). Defaults remain unchanged for existing users.
  • Refactor
    • Centralized warning emission through the injected logger to enable easier customization and testing.
  • Tests
    • Updated tests to use a mock logger injected via options, verifying warning messages without relying on module-level spies.

- Add optional logger parameter to TableOptions type
- Update ResponsiveTable constructor to accept injected logger function
- Replace hardcoded logger.warn call with injected logger instance
- Default to logger.warn when no custom logger is provided
- Update test to use mock logger function instead of spying on global logger

This change improves testability by allowing tests to inject custom loggers
and increases flexibility by enabling different logging behaviors per table
instance. The change is backward compatible as the logger parameter is
optional and defaults to the existing behavior.

Resolves dependency injection concerns and enhances unit test isolation
by removing reliance on global logger state in tests.
@coderabbitai
Copy link

coderabbitai bot commented Sep 14, 2025

Caution

Review failed

The pull request is closed.

Walkthrough

Adds an optional logger function to TableOptions and wires ResponsiveTable to use it for warning about missing compact headers, defaulting to the existing logger.warn. Warning emission in getCompactIndices now calls the instance’s logger. Public API updated to include logger?: (message: string) => void.

Changes

Cohort / File(s) Summary
Responsive table logging injection
src/_table.ts
Adds logger?: (message: string) => void to TableOptions; stores an instance logger defaulting to logger.warn; replaces direct logger.warn calls in getCompactIndices with this.logger to enable custom logging.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor Client
  participant RT as ResponsiveTable
  participant Log as Logger (injected or default)

  Client->>RT: new ResponsiveTable({ logger?: fn })
  Note right of RT: Store this.logger = options.logger || defaultWarn

  Client->>RT: getCompactIndices(headers)
  RT->>RT: Validate compact headers
  alt Missing compact header
    RT->>Log: warn("Compact header not found: ...")
    Note over RT,Log: New: uses injected logger via this.logger
  else All headers exist
    RT-->>Client: indices
  end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

A bunny taps logs with a gentle thump,
Swapping the wire for a pluggable pump.
When headers go missing—no need to despair—
A custom squeak warns with configurable flair.
Carrots compiled, tests hop in line—
Quiet burrows, cleaner sign. 🥕

✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feature/injectable-logger-responsive-table

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 509eb8c and dd25cc5.

📒 Files selected for processing (1)
  • src/_table.ts (6 hunks)

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary of Changes

Hello @ryoppippi, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request refactors the ResponsiveTable class to support logger injection, significantly improving its testability and flexibility. By allowing an external logger to be provided, the class no longer relies on global logging mechanisms, leading to better test isolation and more predictable behavior. The changes are fully backward compatible, ensuring existing implementations remain unaffected.

Highlights

  • Logger Injection: Introduced an optional logger parameter to the TableOptions type, allowing external logger functions to be injected into the ResponsiveTable class.
  • Improved Testability: The ResponsiveTable constructor now accepts an injected logger, defaulting to logger.warn if none is provided, which enhances testability by allowing mock loggers.
  • Code Refactoring: Replaced direct calls to logger.warn within the ResponsiveTable class with the injected this.logger instance, centralizing logging behavior.
  • Enhanced Test Coverage: Updated existing tests to utilize a mock logger function for dependency injection, removing reliance on global state and improving test isolation.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Sep 14, 2025

Open in StackBlitz

npm i https://pkg.pr.new/ryoppippi/ccusage@628

commit: dd25cc5

@ryoppippi ryoppippi merged commit 8c2c97d into main Sep 14, 2025
14 of 16 checks passed
@ryoppippi ryoppippi deleted the feature/injectable-logger-responsive-table branch September 14, 2025 15:38
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces dependency injection for the logger in the ResponsiveTable class, which is a great improvement for testability and flexibility. The changes are well-implemented and maintain backward compatibility. I have a couple of suggestions to make the new logger type even more flexible by allowing for multiple arguments, which aligns better with common logging libraries.

compactColAligns?: TableCellAlign[];
compactThreshold?: number;
forceCompact?: boolean;
logger?: (message: string) => void;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The logger type (message: string) => void is a bit restrictive as it only allows a single string argument. Common logging functions, including consola which is used in this project, often accept multiple arguments or structured data (e.g., logger.warn('message', { details: '...' })). To improve flexibility and align better with standard logging practices, consider using a more generic type like (...args: any[]) => void.

Suggested change
logger?: (message: string) => void;
logger?: (...args: any[]) => void;

private compactThreshold: number;
private compactMode = false;
private forceCompact: boolean;
private logger: (message: string) => void;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For consistency with the recommended change in TableOptions, this property's type should also be updated to (...args: any[]) => void to allow for more flexible logging.

Suggested change
private logger: (message: string) => void;
private logger: (...args: any[]) => void;

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.

2 participants