Skip to content

feat(sort-imports) add support for new groups and customGroups API#505

Merged
azat-io merged 8 commits into
azat-io:mainfrom
hugop95:feat/sort-imports/groups
Apr 19, 2025
Merged

feat(sort-imports) add support for new groups and customGroups API#505
azat-io merged 8 commits into
azat-io:mainfrom
hugop95:feat/sort-imports/groups

Conversation

@hugop95
Copy link
Copy Markdown
Contributor

@hugop95 hugop95 commented Apr 6, 2025

Description

We are finally migrating sort-imports to the new customGroups API with selectors and modifiers.
This PR only focuses on the migration to the new API. The old API is still supported.

Here is the list of selectors and modifiers:

Modifiers

  • type: matches type imports.

Selectors

Ordered from most important to least important (it is the same order as the predefined groups today).

  • index-type (ℹ️ deprecated for the type modifier with index selector)
  • sibling-type (ℹ️ deprecated, same as above)
  • parent-type (ℹ️ deprecated, same as above)
  • internal-type (ℹ️ deprecated, same as above)
  • builtin-type (ℹ️ deprecated, same as above)
  • external-type (ℹ️ deprecated, same as above)
  • type (not marked as deprecated, as it can be useful to quickly aggregate all types at the same place)
  • side-effect-style
  • side-effect
  • style
  • index
  • sibling
  • parent
  • internal
  • builtin
  • external

Important

The object group was never matched, so it has been deprecated (but is still allowed in groups for backward compatibility).
See the code before the refactor to check that the object group was never defined. It can be re-added and implemented later if needed.

customGroups option

Supports elementNamePattern.

Follow-up improvements

What is the purpose of this pull request?

  • New Feature

@hugop95 hugop95 force-pushed the feat/sort-imports/groups branch 10 times, most recently from cb1a0a4 to 2e01cb6 Compare April 9, 2025 17:21
@hugop95 hugop95 force-pushed the feat/sort-imports/groups branch 3 times, most recently from da80d85 to e213554 Compare April 10, 2025 20:22
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 10, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (f790779) to head (9fea396).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #505   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           96        97    +1     
  Lines         7764      7857   +93     
  Branches      1563      1576   +13     
=========================================
+ Hits          7764      7857   +93     

☔ 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-imports/groups branch from a9615c8 to 92ea0c1 Compare April 12, 2025 11:41
- It was actually never set.
@hugop95 hugop95 force-pushed the feat/sort-imports/groups branch 2 times, most recently from 1debf48 to 9085ca9 Compare April 12, 2025 12:16
@hugop95 hugop95 marked this pull request as ready for review April 12, 2025 12:22
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.

Hi @hugop95! Thanks for this PR that adds a new API for sort-imports.

This PR looks good. 👍

let twoWordsSelector = input.split('-').slice(-2).join('-')
let isTwoWordSelectorValid = allSelectors.includes(twoWordsSelector)
if (!allSelectors.includes(singleWordSelector) && !isTwoWordSelectorValid) {
let threeWordsSelector = input.split('-').slice(-3).join('-')
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Perhaps we can simplify the code of this function and make it more declarative?

let isPredefinedGroup = (
  allSelectors: string[],
  allModifiers: string[],
  input: string,
): boolean => {
  if (input === 'unknown') {
    return true
  }
  
  let parts = input.split('-')
  if (parts.length === 0) {
    return false
  }
  
  let possibleSelectors = [
    { selector: parts.slice(-3).join('-'), wordCount: 3 },
    { selector: parts.slice(-2).join('-'), wordCount: 2 },
    { selector: parts.slice(-1)[0], wordCount: 1 }
  ].filter(({ selector, wordCount }) => 
    allSelectors.includes(selector) && parts.length >= wordCount
  )
  
  if (possibleSelectors.length === 0) {
    return false
  }
  
  let { wordCount } = possibleSelectors[0]
  
  let modifiers = parts.slice(0, -wordCount)
  
  return (
    new Set(modifiers).size === modifiers.length &&
    modifiers.every(modifier => allModifiers.includes(modifier))
  )
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@azat-io Thank you, it's more readable indeed!

9fea396 (#505)

@azat-io azat-io merged commit f818b4f into azat-io:main Apr 19, 2025
10 checks passed
@hugop95 hugop95 deleted the feat/sort-imports/groups branch April 19, 2025 15:10
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.

Feature: path aware import sorting

2 participants