Skip to content

Maintenance: Improve code quality in commons package - nullish coalescing and union types #4249

@dreamorosi

Description

@dreamorosi

Summary

The packages/commons package contains 2 minor code quality issues identified by SonarQube that can be improved for better code consistency and type safety:

  1. Nullish Coalescing Operator Usage (packages/commons/src/LRUCache.ts, line 84): Using ternary expression instead of the more readable nullish coalescing operator
  2. Union Type Optimization (packages/commons/src/types/middy.ts, line 63): The unknown type overrides all other types in a union type, making the union redundant

These are both MINOR severity issues that improve code readability and type safety without affecting functionality.

Why is this needed?

  1. Code Consistency: Adopts modern TypeScript patterns and improves code readability
  2. SonarQube Compliance: Resolves 2 MINOR severity issues (typescript:S6606 and typescript:S6571)
  3. Type Safety: Ensures union types are meaningful and provide proper type checking
  4. Developer Experience: Makes the code more maintainable and easier to understand

The commons package is a foundational package used across all other Powertools utilities, so maintaining high code quality standards here benefits the entire project.

Solution

Important

The following changes are included as reference to help you understand the refactoring. Before implementing, please make sure to check the codebase and ensure that they make sense and they are exhaustive.

1. Fix Nullish Coalescing in LRUCache.ts

Current code (line 84):

this.maxSize =
  config?.maxSize !== undefined ? config.maxSize : DEFAULT_MAX_SIZE;

Improved code:

this.maxSize = config?.maxSize ?? DEFAULT_MAX_SIZE;

This change:

  • Uses the nullish coalescing operator (??) which is more concise and readable
  • Maintains the same functionality while being more idiomatic TypeScript
  • Follows the project's coding standards for modern JavaScript/TypeScript patterns

2. Fix Union Type in middy.ts

Current code (line 63):

type MiddyLikeRequest = {
  event: unknown;
  context: Context;
  response: unknown | null;
  error: Error | null;
  internal: {
    [key: string]: unknown;
  };
};

Analysis needed: The issue indicates that unknown overrides other types in a union. This likely refers to a different union type in the file that needs to be reviewed and simplified.

Potential solutions:

  • If the union is SomeType | unknown, simplify to just unknown
  • If the union should be more specific, replace unknown with the appropriate type
  • Review the context to determine the intended type behavior

Implementation Details

  1. Files to modify:

    • packages/commons/src/LRUCache.ts (line 84)
    • packages/commons/src/types/middy.ts (line 63 area)
  2. Testing:

    • Ensure all existing tests continue to pass
    • Run type checking to verify no TypeScript errors are introduced
    • Verify LRUCache behavior remains identical with the nullish coalescing change
  3. Validation:

    • Run npm run test:unit -w packages/commons to ensure no regressions
    • Run npm run lint -w packages/commons to verify code style compliance
    • Run npm run build -ws to verify TypeScript compilation

Additional Context

This issue was identified as part of a SonarQube code quality review. The commons package is a foundational component used by all other Powertools packages, making code quality improvements here particularly valuable for the overall project health.

These changes are purely cosmetic/stylistic improvements that:

  • Do not change any functionality
  • Improve code readability and maintainability
  • Follow modern TypeScript best practices
  • Align with the project's coding standards

SonarQube Issue Keys:

Acknowledgment

Future readers

Please react with 👍 and your use case to help us understand customer demand.

Metadata

Metadata

Assignees

No one assigned

    Labels

    commonsThis item relates to the Commons UtilityconfirmedThe scope is clear, ready for implementationgood-first-issueSomething that is suitable for those who want to start contributinghelp-wantedWe would really appreciate some support from community for this one

    Type

    No type

    Projects

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions