Skip to content

Maintenance: Mark private members as readonly in jmespath package #4252

@dreamorosi

Description

@dreamorosi

Summary

The packages/jmespath package contains 4 MAJOR severity code quality issues identified by SonarQube where private class members are never reassigned after initialization but are not marked as readonly. These issues affect core JMESPath parsing and interpretation functionality:

  1. Parser.ts (2 issues): #projectionStop (line 51) and #maxCacheSize (line 59)
  2. TreeInterpreter.ts (1 issue): #functions (line 28)
  3. Lexer.ts (1 issue): #consumeComparatorSigns (line 84)

All of these are private members that are initialized once and never modified, making them ideal candidates for the readonly modifier.

Why is this needed?

  1. Code Quality: Improves code clarity by explicitly marking immutable members as readonly
  2. SonarQube Compliance: Resolves 4 MAJOR severity issues (typescript:S2933)
  3. Type Safety: Prevents accidental reassignment of these critical configuration members
  4. Developer Intent: Makes the immutable nature of these members explicit to future maintainers
  5. Best Practices: Follows TypeScript best practices for class member declarations

The JMESPath package is a core utility for JSON path expressions used throughout Powertools, so maintaining high code quality standards here is particularly important.

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 Parser.ts (2 members)

Current code:

class Parser {
  // ...
  #projectionStop: Set<string>;
  // ...
  #maxCacheSize: number;
  // ...
}

Improved code:

class Parser {
  // ...
  readonly #projectionStop: Set<string>;
  // ...
  readonly #maxCacheSize: number;
  // ...
}

2. Fix TreeInterpreter.ts (1 member)

Current code:

class TreeInterpreter {
  // ...
  #functions: Functions;
  // ...
}

Improved code:

class TreeInterpreter {
  // ...
  readonly #functions: Functions;
  // ...
}

3. Fix Lexer.ts (1 member)

Current code:

class Lexer {
  // ...
  #consumeComparatorSigns: Set<string>;
  // ...
}

Improved code:

class Lexer {
  // ...
  readonly #consumeComparatorSigns: Set<string>;
  // ...
}

These changes:

  • Make the immutable nature of these members explicit
  • Prevent accidental reassignment in future code changes
  • Follow TypeScript best practices for class design
  • Improve code readability and maintainability

Implementation Details

  1. Files to modify:

    • packages/jmespath/src/Parser.ts (lines 51, 59)
    • packages/jmespath/src/TreeInterpreter.ts (line 28)
    • packages/jmespath/src/Lexer.ts (line 84)
  2. Testing:

    • Ensure all existing tests continue to pass
    • Run type checking to verify no TypeScript errors are introduced
    • Verify that the readonly modifier doesn't break any existing functionality
  3. Validation:

    • Run npm run test:unit -w packages/jmespath to ensure no regressions
    • Run npm run lint -w packages/jmespath to verify code style compliance
    • Run npm run build -ws to verify TypeScript compilation
    • Run SonarQube analysis to confirm issues are resolved

Additional Context

This issue was identified as part of a SonarQube code quality review. The JMESPath package is a foundational utility used for JSON path expressions throughout the Powertools ecosystem, making code quality improvements here particularly valuable.

These changes are purely declarative improvements that should not change any functionality or behavior.

SonarQube Issue Keys:

Acknowledgment

Future readers

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

Metadata

Metadata

Assignees

Labels

confirmedThe 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 onejmespathThis item relates to the JMESPath Utility

Type

No type

Projects

Status

Shipped

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions