Skip to content

Commit d2d48ad

Browse files
committed
[Refactor] Shrinkwrap/buildIdealTree: create and use inferFormattingOptions method
1 parent 8ad7713 commit d2d48ad

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

workspaces/arborist/lib/arborist/build-ideal-tree.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,8 +333,7 @@ module.exports = cls => class IdealTreeBuilder extends cls {
333333
root.meta.lockfileVersion = defaultLockfileVersion
334334
}
335335
}
336-
// ensure indentation is inferred properly
337-
root.meta.indent = root.package[Symbol.for('indent')]
336+
root.meta.inferFormattingOptions(root.package)
338337
return root
339338
})
340339

workspaces/arborist/lib/shrinkwrap.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,18 @@ class Shrinkwrap {
424424
.map(fn => fn && maybeStatFile(fn)))
425425
}
426426

427+
inferFormattingOptions (packageJSONData) {
428+
// don't use detect-indent, just pick the first line.
429+
// if the file starts with {" then we have an indent of '', ie, none
430+
// which will default to 2 at save time.
431+
const {
432+
[Symbol.for('indent')]: indent,
433+
[Symbol.for('newline')]: newline,
434+
} = packageJSONData
435+
this.indent = indent !== undefined ? indent : this.indent
436+
this.newline = newline !== undefined ? newline : this.newline
437+
}
438+
427439
load () {
428440
// we don't need to load package-lock.json except for top of tree nodes,
429441
// only npm-shrinkwrap.json.
@@ -451,15 +463,7 @@ class Shrinkwrap {
451463

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

464468
if (!this.hiddenLockfile || !data.packages) {
465469
return data

0 commit comments

Comments
 (0)