Skip to content

Conversation

@shalvah
Copy link
Contributor

@shalvah shalvah commented Oct 9, 2022

This PR adjusts the logic used for sorting keys when writing yarn.lock to be more in line with Yarn's own rules, avoiding unnecessary diffs and confusion.

References

Addresses some of the issues raised in #5126, by mostly adapting the code Yarn itself uses. It doesn't solve everything (I didn't address the registry URLs issue, and sorting differs slightly when there are ~ or ^), but it aligns this more closely with what Yarn produces. If the npm team is interested, I can work on PRs to try to address the others.

@shalvah shalvah requested a review from a team as a code owner October 9, 2022 11:48
@shalvah shalvah changed the title feat: sort and quote keys with Yarn's rules when writing yarn.lock fix: sort and quote keys with Yarn's rules when writing yarn.lock Oct 9, 2022
@cachius
Copy link

cachius commented Oct 14, 2022

@npm/cli-team This would be really cool to have in v9 (and 8.19.3)!
@shalvah I'd sure be interested in more fixes.

}

const shouldQuoteString = str => {
return str.indexOf('true') === 0 ||
Copy link
Member

Choose a reason for hiding this comment

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

is there a reason this isn't a test of equality with the string true or false?

str.indexOf('false') === 0 ||
/[:\s\n\\",[\]]/g.test(str) ||
/^[0-9]/g.test(str) ||
!/^[a-zA-Z]/g.test(str)
Copy link
Member

Choose a reason for hiding this comment

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

If it starts with 0-9 then it doesn't start w/ a-zA-Z so the 0-9 check isn't needed.

}

const quoteIfNeeded = val => {
if (typeof val === 'boolean' || typeof val === 'number' || shouldQuoteString(val)) {
Copy link
Member

@wraithgar wraithgar Oct 19, 2022

Choose a reason for hiding this comment

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

idk if we need two functions here. single use shouldQuoteString logic can be inlined w/ the rest of this if statement.

@wraithgar
Copy link
Member

Tweaked and consolidated things in #5724 in the interest of getting this out w/ today's release.

@wraithgar
Copy link
Member

If the npm team is interested, I can work on PRs to try to address the others.

Yes please @shalvah anyone who is willing to own this is more than welcome to collaborate w/ us on keeping npm up to spec.

@shalvah
Copy link
Contributor Author

shalvah commented Oct 19, 2022

Alright @wraithgar . Thanks for picking it up. Guess I'll close this PR then. Currently in the hospital with a broken arm so I couldn't make any changes. 😓Will hopefully work on the future improvements when I'm better.

@shalvah shalvah closed this Oct 19, 2022
@wraithgar
Copy link
Member

Hope you get better soon. Looking forward to future PRs.

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.

3 participants