Skip to content

feat(sort-import-attributes): add 2 useConfigurationIf options: allNamesMatchPattern and matchesAstSelector#719

Merged
azat-io merged 3 commits into
azat-io:mainfrom
hugop95:feat/sort-import-attributes/ast-matcher
Mar 10, 2026
Merged

feat(sort-import-attributes): add 2 useConfigurationIf options: allNamesMatchPattern and matchesAstSelector#719
azat-io merged 3 commits into
azat-io:mainfrom
hugop95:feat/sort-import-attributes/ast-matcher

Conversation

@hugop95
Copy link
Copy Markdown
Contributor

@hugop95 hugop95 commented Mar 10, 2026

Description

This PR adds support for the following sort-import-attributes and sort-export-attributes options:

  • useConfigurationIf.allNamesMatchPattern.
  • useConfigurationIf.matchesAstSelector.

It also puts some logic in common between the two rules, as they essentially sort the same things.

How to use

Users need to pass an AST selector that matches a ImportDeclaration or ExportNamedDeclaration node.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Mar 10, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 0cdd9b8f-5a66-437a-ad9e-27e4707f6bf6

📥 Commits

Reviewing files that changed from the base of the PR and between 1d3dd30 and 80ca077.

📒 Files selected for processing (11)
  • docs/content/rules/sort-export-attributes.mdx
  • docs/content/rules/sort-import-attributes.mdx
  • rules/sort-export-attributes.ts
  • rules/sort-export-attributes/types.ts
  • rules/sort-import-attributes.ts
  • rules/sort-import-attributes/compute-matched-context-options.ts
  • rules/sort-import-attributes/compute-node-name.ts
  • rules/sort-import-attributes/sort-import-or-export-attributes.ts
  • rules/sort-import-attributes/types.ts
  • test/rules/sort-export-attributes.test.ts
  • test/rules/sort-import-attributes.test.ts
💤 Files with no reviewable changes (1)
  • rules/sort-export-attributes/types.ts

📝 Walkthrough

Summary by CodeRabbit

  • New Features

    • Added useConfigurationIf option to sort-export-attributes and sort-import-attributes rules, enabling conditional configuration based on attribute name patterns and AST selectors.
  • Documentation

    • Updated rule documentation with details and usage examples for the new useConfigurationIf option.
  • Tests

    • Added comprehensive test coverage for conditional configuration scenarios across both rules.

Walkthrough

Introduces a new useConfigurationIf option for sort-export-attributes and sort-import-attributes rules, enabling conditional configuration selection based on attribute name patterns or AST selector matching. Both rules are refactored to delegate sorting logic to a shared utility with centralized configuration matching.

Changes

Cohort / File(s) Summary
Documentation Updates
docs/content/rules/sort-export-attributes.mdx, docs/content/rules/sort-import-attributes.mdx
Adds documentation for new useConfigurationIf option with type signature, default value, and examples demonstrating usage with allNamesMatchPattern and matchesAstSelector fields.
sort-export-attributes Rule Refactor
rules/sort-export-attributes.ts, rules/sort-export-attributes/types.ts
Delegates sorting logic to shared sortImportOrExportAttributes utility via buildAstListeners. Removes local sorting helpers and validation flow. Type definitions moved to shared location; rule now uses SortImportAttributesOptions alias. Extends defaultOptions with useConfigurationIf: {}.
sort-import-attributes Rule Refactor
rules/sort-import-attributes.ts, rules/sort-import-attributes/types.ts
Adds public jsonSchema export integrating useConfigurationIf schema block. Refactors to delegate to shared sortImportOrExportAttributes via buildAstListeners. Updates Options type to include useConfigurationIf with allNamesMatchPattern and matchesAstSelector fields.
Configuration Matching Utilities
rules/sort-import-attributes/compute-matched-context-options.ts, rules/sort-import-attributes/compute-node-name.ts
New helper functions for conditional configuration selection. computeMatchedContextOptions iterates rule options to find first matching configuration via allNamesMatchPattern and AST selector filters. computeNodeName extracts attribute identifier or literal value for pattern matching.
Shared Sorting Logic
rules/sort-import-attributes/sort-import-or-export-attributes.ts
New centralized sorting function accepting ImportDeclaration or ExportNamedDeclaration nodes. Orchestrates multi-step flow: compute matched options, validate configurations, collect ESLint-disabled lines, partition attributes into groups, construct sorting nodes, and report errors via reportAllErrors.
Test Coverage
test/rules/sort-export-attributes.test.ts, test/rules/sort-import-attributes.test.ts
Extensive new test suites for useConfigurationIf.allNamesMatchPattern and useConfigurationIf.matchesAstSelector. Tests verify configuration matching behavior, fallback logic, first-matching precedence, and interaction with existing sorting across various input shapes and selector forms.

Sequence Diagram(s)

sequenceDiagram
    participant Rule as Rule Listener
    participant Sorter as sortImportAttributes/<br/>sortExportAttributes
    participant Matcher as computeMatched<br/>ContextOptions
    participant NameComp as computeNodeName
    participant Sort as sortImportOrExport<br/>Attributes
    participant Reporter as reportAllErrors

    Rule->>Sorter: node, context, matchedAstSelectors
    Sorter->>Matcher: attributes, context, matchedAstSelectors
    
    loop For each configuration option
        Matcher->>NameComp: extract attribute names
        NameComp-->>Matcher: nodeNames
        Matcher->>Matcher: check allNamesMatchPattern match
        Matcher->>Matcher: check matchesAstSelector match
        alt Both conditions met
            Matcher-->>Sorter: matched configuration
        end
    end
    
    Sorter->>Sort: matched config, node, context
    Sort->>Sort: partition attributes into groups
    Sort->>Sort: construct sorting nodes
    Sort->>Reporter: report ordering errors
    Reporter-->>Sort: void
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~30 minutes

Possibly related PRs

Suggested reviewers

  • azat-io
🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 28.57% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main feature: adding two new useConfigurationIf options (allNamesMatchPattern and matchesAstSelector) to the sort-import-attributes rule, which directly matches the changeset's primary objective.
Description check ✅ Passed The PR description adequately covers the key changes: adds two new options to both rules, notes that logic was consolidated between rules, and explains how to use the feature with AST selectors.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@hugop95 hugop95 force-pushed the feat/sort-import-attributes/ast-matcher branch from 42fa0a3 to 768c959 Compare March 10, 2026 13:38
@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 10, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (1d3dd30) to head (80ca077).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #719   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          216       219    +3     
  Lines         4358      4341   -17     
  Branches      1346      1347    +1     
=========================================
- Hits          4358      4341   -17     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@hugop95 hugop95 force-pushed the feat/sort-import-attributes/ast-matcher branch from 768c959 to 80ca077 Compare March 10, 2026 13:54
@hugop95 hugop95 marked this pull request as ready for review March 10, 2026 13:55
Copy link
Copy Markdown
Owner

@azat-io azat-io left a comment

Choose a reason for hiding this comment

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

Thanks!

@azat-io azat-io merged commit 8f6f21d into azat-io:main Mar 10, 2026
13 checks passed
@hugop95 hugop95 deleted the feat/sort-import-attributes/ast-matcher branch March 10, 2026 17:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants