Skip to content

Fix public-api rule: Remove file extensions when suggesting directory imports #17

@conarti

Description

@conarti

Bug Description

The public-api rule incorrectly handles file extensions in fix suggestions when converting from specific file imports to directory imports.

Current Behavior

When importing from:

import { unblockNode, unblockNodesBulk } from "@/entities/node/api.ts";

The rule suggests:

import { unblockNode, unblockNodesBulk } from "@/entities/node.ts";

Expected Behavior

The rule should suggest:

import { unblockNode, unblockNodesBulk } from "@/entities/node";

Root Cause

In src/rules/public-api/model/convert-to-public-api.ts, the convertToPublicApi function removes the segment path (e.g., /api.ts) but doesn't account for the case where the remaining path still has a file extension that should be removed when the result is a directory reference.

Reproduction

// Input
import { something } from "@/entities/node/api.ts";

// Current incorrect output  
import { something } from "@/entities/node.ts";

// Expected correct output
import { something } from "@/entities/node";

Proposed Solution

The convertToPublicApi function should check if the resulting path ends with a file extension (.ts, .js, etc.) and remove it when the result is meant to be a directory import.

Files Affected

  • src/rules/public-api/model/convert-to-public-api.ts - Main logic
  • src/rules/public-api/index.test.ts - Add test case

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions