feat(sort-imports) add support for new groups and customGroups API#505
Merged
Conversation
cb1a0a4 to
2e01cb6
Compare
- Useful for `side-effect-style` selector.
da80d85 to
e213554
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
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. 🚀 New features to boost your workflow:
|
This was referenced Apr 11, 2025
a9615c8 to
92ea0c1
Compare
- It was actually never set.
1debf48 to
9085ca9
Compare
9085ca9 to
6c058d3
Compare
This was referenced Apr 12, 2025
azat-io
approved these changes
Apr 19, 2025
| let twoWordsSelector = input.split('-').slice(-2).join('-') | ||
| let isTwoWordSelectorValid = allSelectors.includes(twoWordsSelector) | ||
| if (!allSelectors.includes(singleWordSelector) && !isTwoWordSelectorValid) { | ||
| let threeWordsSelector = input.split('-').slice(-3).join('-') |
Owner
There was a problem hiding this comment.
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))
)
}
Contributor
Author
There was a problem hiding this comment.
@azat-io Thank you, it's more readable indeed!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
We are finally migrating
sort-importsto the newcustomGroupsAPI 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: matchestypeimports.Selectors
Ordered from most important to least important (it is the same order as the predefined groups today).
index-type(ℹ️ deprecated for thetypemodifier withindexselector)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-styleside-effectstyleindexsiblingparentinternalbuiltinexternalImportant
The
objectgroup was never matched, so it has been deprecated (but is still allowed ingroupsfor backward compatibility).See the code before the refactor to check that the
objectgroup was never defined. It can be re-added and implemented later if needed.customGroupsoptionSupports
elementNamePattern.Follow-up improvements
subpath,tsconfig-pathselectors and 5 modifiers #512)What is the purpose of this pull request?