Skip to content

[DevOps] Create ESLint rule to prevent usage of "magic number" status codes #893

@choden-dev

Description

@choden-dev

[DevOps] Create ESLint rule to prevent usage of "magic number" status codes

Description

We need to implement an ESLint rule that prevents direct usage of numeric HTTP status codes in our codebase. Instead, developers should use the StatusCodes enum from the http-status-codes library.

Current Problem

In our codebase, there are instances where HTTP status codes are used directly as numbers (e.g., this.setStatus(200) or response.status(404)). These "magic numbers" reduce code readability and maintainability.

Expected Solution

  • Create a custom ESLint rule that detects and flags direct usage of numeric HTTP status codes
  • The rule should warn or error when numbers like 200, 201, 400, 404, etc. are used with setStatus(), response.status(), or similar methods
  • The rule should suggest using the appropriate constant from StatusCodes enum (e.g., StatusCodes.OK instead of 200)

Examples

❌ Disallowed:

this.setStatus(200);
response.status(404).send('Not found');

✅ Allowed:

this.setStatus(StatusCodes.OK);
response.status(StatusCodes.NOT_FOUND).send('Not found');

Implementation Notes

  • Focus on common HTTP methods that set status codes
  • Consider using an ESLint no-restricted-syntax or no-magic-numbers rule with appropriate configuration
  • The rule should also work with Express-style response handling

Acceptance Criteria

  • ESLint rule correctly identifies magic number HTTP status codes
  • Rule provides helpful error messages with suggestions for the correct StatusCodes constant
  • Documentation for the rule is added to our development guidelines
  • Existing codebase passes with the new rule (after necessary refactoring)

BEFORE MERGING

  • Acceptance criteria met
  • PR Reviewed (For non-trivial changes)
  • Changes tested after rebasing on master or merging in master (hint: git fetch origin master:master, then git rebase master or git merge master)
  • All required PR checks passing

Metadata

Metadata

Assignees

No one assigned

    Labels

    devopsAnything to do with development operations

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions