Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support for sorting dependenciesMeta in Yarn berry #323

Merged
merged 2 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,27 @@ const sortObjectBySemver = sortObjectBy((a, b) => {
return semver.compare(semver.minVersion(aRange), semver.minVersion(bRange))
})

const sortObjectByIdent = (a, b) => {
const getIdent = (ident) => {
rickh18 marked this conversation as resolved.
Show resolved Hide resolved
const parts = ident.split('@')

if (ident.startsWith('@')) {
// Handle cases where ident starts with '@'
return parts.length > 2 ? parts.slice(0, -1).join('@') : ident
}

// Handle cases where ident doesn't start with '@'
return parts.length > 1 ? parts.slice(0, -1).join('@') : ident
}

const identA = getIdent(a)
const identB = getIdent(b)

if (identA < identB) return -1
if (identA > identB) return 1
return 0
}

// https://github.com/eslint/eslint/blob/acc0e47572a9390292b4e313b4a4bf360d236358/conf/config-schema.js
const eslintBaseConfigProperties = [
// `files` and `excludedFiles` are only on `overrides[]`
Expand Down Expand Up @@ -341,7 +362,7 @@ const fields = [
{ key: 'resolutions', over: sortObject },
{ key: 'dependencies', over: sortObject },
{ key: 'devDependencies', over: sortObject },
{ key: 'dependenciesMeta', over: sortObjectBy(undefined, true) },
{ key: 'dependenciesMeta', over: sortObjectBy(sortObjectByIdent, true) },
{ key: 'peerDependencies', over: sortObject },
// TODO: only sort depth = 2
{ key: 'peerDependenciesMeta', over: sortObjectBy(undefined, true) },
Expand Down
35 changes: 35 additions & 0 deletions tests/_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,40 @@ function sortObjectAlphabetically(t, options = {}) {
}
}

function sortObjectWithRangeAlphabetically(t, options = {}) {
const { maxDepth = 1, expect } = options

for (let depth = 1; depth < maxDepth + 1; depth++) {
sortObject(t, {
...options,
value: keysToObject(
[
'@[email protected]',
'[email protected]',
'[email protected]',
'@[email protected]',
'[email protected]',
'@b-package',
],
depth,
),
expect:
expect ||
keysToObject(
[
'@[email protected]',
'@b-package',
'@[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
],
depth,
),
})
}
}

function sortObject(
t,
{
Expand Down Expand Up @@ -211,6 +245,7 @@ export const macro = {
sortObject,
asItIs,
sortObjectAlphabetically,
sortObjectWithRangeAlphabetically,
testCLI,
uniqueArray,
uniqueAndSort,
Expand Down
9 changes: 8 additions & 1 deletion tests/deps.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,17 @@ test('peerDependenciesMeta', macro.sortObjectAlphabetically, {
expect: 'snapshot',
})

// peerDependenciesMeta
// dependenciesMeta
test('dependenciesMeta', macro.sortObjectAlphabetically, {
path: 'dependenciesMeta',
maxDepth: 2,
// TODO: don't use snapshot, find a esaier way for review
expect: 'snapshot',
})

test('dependenciesMetaRange', macro.sortObjectWithRangeAlphabetically, {
path: 'dependenciesMeta',
maxDepth: 2,
// TODO: don't use snapshot, find a esaier way for review
expect: 'snapshot',
})
140 changes: 140 additions & 0 deletions tests/snapshots/deps.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,143 @@ Generated by [AVA](https://avajs.dev).
}`,
pretty: true,
}

## dependenciesMetaRange

> Should sort `dependenciesMeta` as object.

{
input: `{␊
"dependenciesMeta": {␊
"@[email protected]": "@[email protected]",␊
"[email protected]": "[email protected]",␊
"[email protected]": "[email protected]",␊
"@[email protected]": "@[email protected]",␊
"[email protected]": "[email protected]",␊
"@b-package": "@b-package"␊
}␊
}`,
options: undefined,
output: `{␊
"dependenciesMeta": {␊
"@[email protected]": "@[email protected]",␊
"@b-package": "@b-package",␊
"@[email protected]": "@[email protected]",␊
"[email protected]": "[email protected]",␊
"[email protected]": "[email protected]",␊
"[email protected]": "[email protected]"␊
}␊
}`,
pretty: true,
}

> Should sort `dependenciesMeta` as object.

{
input: `{␊
"dependenciesMeta": {␊
"@[email protected]": {␊
"@[email protected]": "@[email protected]",␊
"[email protected]": "[email protected]",␊
"[email protected]": "[email protected]",␊
"@[email protected]": "@[email protected]",␊
"[email protected]": "[email protected]",␊
"@b-package": "@b-package"␊
},␊
"[email protected]": {␊
"@[email protected]": "@[email protected]",␊
"[email protected]": "[email protected]",␊
"[email protected]": "[email protected]",␊
"@[email protected]": "@[email protected]",␊
"[email protected]": "[email protected]",␊
"@b-package": "@b-package"␊
},␊
"[email protected]": {␊
"@[email protected]": "@[email protected]",␊
"[email protected]": "[email protected]",␊
"[email protected]": "[email protected]",␊
"@[email protected]": "@[email protected]",␊
"[email protected]": "[email protected]",␊
"@b-package": "@b-package"␊
},␊
"@[email protected]": {␊
"@[email protected]": "@[email protected]",␊
"[email protected]": "[email protected]",␊
"[email protected]": "[email protected]",␊
"@[email protected]": "@[email protected]",␊
"[email protected]": "[email protected]",␊
"@b-package": "@b-package"␊
},␊
"[email protected]": {␊
"@[email protected]": "@[email protected]",␊
"[email protected]": "[email protected]",␊
"[email protected]": "[email protected]",␊
"@[email protected]": "@[email protected]",␊
"[email protected]": "[email protected]",␊
"@b-package": "@b-package"␊
},␊
"@b-package": {␊
"@[email protected]": "@[email protected]",␊
"[email protected]": "[email protected]",␊
"[email protected]": "[email protected]",␊
"@[email protected]": "@[email protected]",␊
"[email protected]": "[email protected]",␊
"@b-package": "@b-package"␊
}␊
}␊
}`,
options: undefined,
output: `{␊
"dependenciesMeta": {␊
"@[email protected]": {␊
"@[email protected]": "@[email protected]",␊
"@b-package": "@b-package",␊
"@[email protected]": "@[email protected]",␊
"[email protected]": "[email protected]",␊
"[email protected]": "[email protected]",␊
"[email protected]": "[email protected]"␊
},␊
"@b-package": {␊
"@[email protected]": "@[email protected]",␊
"@b-package": "@b-package",␊
"@[email protected]": "@[email protected]",␊
"[email protected]": "[email protected]",␊
"[email protected]": "[email protected]",␊
"[email protected]": "[email protected]"␊
},␊
"@[email protected]": {␊
"@[email protected]": "@[email protected]",␊
"@b-package": "@b-package",␊
"@[email protected]": "@[email protected]",␊
"[email protected]": "[email protected]",␊
"[email protected]": "[email protected]",␊
"[email protected]": "[email protected]"␊
},␊
"[email protected]": {␊
"@[email protected]": "@[email protected]",␊
"@b-package": "@b-package",␊
"@[email protected]": "@[email protected]",␊
"[email protected]": "[email protected]",␊
"[email protected]": "[email protected]",␊
"[email protected]": "[email protected]"␊
},␊
"[email protected]": {␊
"@[email protected]": "@[email protected]",␊
"@b-package": "@b-package",␊
"@[email protected]": "@[email protected]",␊
"[email protected]": "[email protected]",␊
"[email protected]": "[email protected]",␊
"[email protected]": "[email protected]"␊
},␊
"[email protected]": {␊
"@[email protected]": "@[email protected]",␊
"@b-package": "@b-package",␊
"@[email protected]": "@[email protected]",␊
"[email protected]": "[email protected]",␊
"[email protected]": "[email protected]",␊
"[email protected]": "[email protected]"␊
}␊
}␊
}`,
pretty: true,
}
Binary file modified tests/snapshots/deps.js.snap
Binary file not shown.
Loading