Skip to content

Comments

feat(formatter): support custom internal import patterns#16322

Closed
relsunkaev wants to merge 1 commit intooxc-project:mainfrom
relsunkaev:feat/internal-pattern-oxfmt
Closed

feat(formatter): support custom internal import patterns#16322
relsunkaev wants to merge 1 commit intooxc-project:mainfrom
relsunkaev:feat/internal-pattern-oxfmt

Conversation

@relsunkaev
Copy link

Summary

  • honor .oxfmtrc experimentalSortImports.internalPattern by parsing prefixes once in the formatter core, validating config input, and surfacing the option through the NAPI playground + schema docs
  • add Vitest write-mode coverage (with a RegExp.escape polyfill) so the CLI path stays exercised end to end when internal prefixes or literal asterisks are configured

Testing

  • pnpm --filter oxfmt-app run build-test
  • pnpm --filter oxfmt-app test

@relsunkaev relsunkaev requested a review from Dunqing as a code owner December 1, 2025 05:38
Copilot AI review requested due to automatic review settings December 1, 2025 05:38
@graphite-app
Copy link
Contributor

graphite-app bot commented Dec 1, 2025

How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

@github-actions github-actions bot added A-cli Area - CLI A-formatter Area - Formatter C-enhancement Category - New feature or request labels Dec 1, 2025
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request adds support for custom internal import patterns in the formatter's experimental import sorting feature. It allows users to configure patterns (e.g., "@company/", "pkg-") via .oxfmtrc's experimentalSortImports.internalPattern field to classify imports as "internal" when sorting. The implementation includes configuration validation, NAPI playground integration, and comprehensive end-to-end tests.

Key changes:

  • Added internalPattern configuration option accepting single string or array of strings in JSON schema and config files
  • Implemented prefix-based pattern matching with optional trailing * support (e.g., @acme/* becomes @acme/ prefix matcher)
  • Added ES2025 RegExp.escape polyfill for test utilities to handle special characters in path normalization

Reviewed changes

Copilot reviewed 16 out of 17 changed files in this pull request and generated 9 comments.

Show a summary per file
File Description
npm/oxfmt/configuration_schema.json Added internalPattern and StringOrStringArray schema definitions for import sorting configuration
napi/playground/src/options.rs Added internal_pattern field to OxcSortImportsOptions accepting string or array of strings
napi/playground/src/lib.rs Implemented conversion logic from NAPI options to internal patterns with error handling
napi/playground/index.d.ts Added TypeScript type definition for internalPattern option
crates/oxc_formatter/tests/snapshots/schema_json.snap Updated snapshot with new schema definitions
crates/oxc_formatter/src/service/oxfmtrc.rs Added deserialization, validation, and normalization for internalPattern config with comprehensive tests
crates/oxc_formatter/src/options.rs Implemented InternalImportPattern struct with prefix-based matching logic and unit tests
crates/oxc_formatter/src/lib.rs Exported normalize_internal_pattern_value for NAPI integration
crates/oxc_formatter/src/ir_transform/sort_imports/compute_metadata.rs Integrated internal pattern matching into import path classification logic
crates/oxc_formatter/examples/sort_imports.rs Updated example to initialize new internal_patterns field
apps/oxfmt/test/utils.ts Added RegExp.escape polyfill for ES2025 compatibility in path normalization
apps/oxfmt/test/sort_imports_internal_pattern.test.ts Added end-to-end tests for internal pattern configuration
apps/oxfmt/test/fixtures/sort_imports_internal_pattern/* Added test fixtures for monorepo patterns and literal asterisk handling
apps/oxfmt/test/__snapshots__/sort_imports_internal_pattern.test.ts.snap Added snapshots verifying correct import sorting with internal patterns

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@relsunkaev
Copy link
Author

@copilot These fixture files are intentional inputs for the write-mode snapshot tests—the imports are the actual content we run through oxfmt, so they are expected to be unused in the fixture itself.

@relsunkaev
Copy link
Author

@copilot Clarified how the trailing works per your suggestion. Thanks!

@relsunkaev relsunkaev force-pushed the feat/internal-pattern-oxfmt branch from 7d3821e to 93b5a9d Compare December 1, 2025 06:03
@nnnnoel
Copy link
Contributor

nnnnoel commented Dec 2, 2025

I think it's duplicate with #16372

@leaysgur
Copy link
Member

leaysgur commented Dec 2, 2025

Oops, thanks for the PR and for your interest in the project!

Unfortunately, I've already been working on this same feature myself, so I won't be able to merge this... Sorry for the overlap! 🙏🏻

For next time, feel free to open or comment an issue first — that way we can check if something's already in progress and avoid stepping on each other's toes.

I appreciate the effort, and hope to see you around the project again!

@leaysgur leaysgur closed this Dec 2, 2025
@relsunkaev
Copy link
Author

All good! Just glad this is getting implemented

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-cli Area - CLI A-formatter Area - Formatter C-enhancement Category - New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants