kuceb / eslint-plugin-json-format

Format and auto-fix your JSON using ESLint
MIT License
41 stars 3 forks source link

Question: about `sort-package-json` usage #10

Closed fisker closed 4 years ago

fisker commented 4 years ago

Update: see comment bellow

I commited a new change to sort-package-json recently, with the new behavior, options.sortOrder will prepend to defaultSortOrder instead of override. And I'm planning make sort-package-json exported sortOrder a copy of defaultSortOrder(or maybe remove it), since set sortPackageJson.sortOrder directly has no effect.

But I checked this repo

https://github.com/Bkucera/eslint-plugin-json-format/blob/feb247427f79674b0dcb720e00f724a0d3266670/lib/utils.js#L91-L98

We are overriding sortPackageJson.sortOrder by remove them and push new keys, can you explain why we need this?

fisker commented 4 years ago

Sorry, I missed the union line. So you are prepending them to defaulSortOrder too... Which I did happened be the behavior you want. We can rewrite like this now

-const _sortOrder = _.clone(sortPackageJSON.sortOrder)
-
-const sortPkgJSON = (jsonObj, pkgSortOrder) => {
-
-  _.remove(sortPackageJSON.sortOrder)
-
-  _.each(_.union(pkgSortOrder, _sortOrder), (v) => sortPackageJSON.sortOrder.push(v))
-
-  return sortPackageJSON(jsonObj)
-}
+const sortPkgJSON = (jsonObj, pkgSortOrder) => sortPackageJSON(jsonObj, {sortOrder: pkgSortOrder})
kuceb commented 4 years ago

@fisker nice, in hindsight I should have opened an issue with sort-package-json when I had to hack around it. thanks!