Skip to content

Commit

Permalink
[Refactor] Shrinkwrap/buildIdealTree: create and use `inferFormat…
Browse files Browse the repository at this point in the history
…tingOptions` method
  • Loading branch information
ljharb committed Jan 15, 2022
1 parent 8ad7713 commit d2d48ad
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
3 changes: 1 addition & 2 deletions workspaces/arborist/lib/arborist/build-ideal-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,7 @@ module.exports = cls => class IdealTreeBuilder extends cls {
root.meta.lockfileVersion = defaultLockfileVersion
}
}
// ensure indentation is inferred properly
root.meta.indent = root.package[Symbol.for('indent')]
root.meta.inferFormattingOptions(root.package)
return root
})

Expand Down
22 changes: 13 additions & 9 deletions workspaces/arborist/lib/shrinkwrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,18 @@ class Shrinkwrap {
.map(fn => fn && maybeStatFile(fn)))
}

inferFormattingOptions (packageJSONData) {
// don't use detect-indent, just pick the first line.
// if the file starts with {" then we have an indent of '', ie, none
// which will default to 2 at save time.
const {
[Symbol.for('indent')]: indent,
[Symbol.for('newline')]: newline,
} = packageJSONData
this.indent = indent !== undefined ? indent : this.indent
this.newline = newline !== undefined ? newline : this.newline
}

load () {
// we don't need to load package-lock.json except for top of tree nodes,
// only npm-shrinkwrap.json.
Expand Down Expand Up @@ -451,15 +463,7 @@ class Shrinkwrap {

return data ? parseJSON(data) : {}
}).then(async data => {
// don't use detect-indent, just pick the first line.
// if the file starts with {" then we have an indent of '', ie, none
// which will default to 2 at save time.
const {
[Symbol.for('indent')]: indent,
[Symbol.for('newline')]: newline,
} = data
this.indent = indent !== undefined ? indent : this.indent
this.newline = newline !== undefined ? newline : this.newline
this.inferFormattingOptions(data)

if (!this.hiddenLockfile || !data.packages) {
return data
Expand Down

0 comments on commit d2d48ad

Please sign in to comment.