From d2d48adfe1343705483a1c3d03e51b5cda76d1f0 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Wed, 12 Jan 2022 12:40:12 -0800 Subject: [PATCH] [Refactor] `Shrinkwrap`/`buildIdealTree`: create and use `inferFormattingOptions` method --- .../arborist/lib/arborist/build-ideal-tree.js | 3 +-- workspaces/arborist/lib/shrinkwrap.js | 22 +++++++++++-------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/workspaces/arborist/lib/arborist/build-ideal-tree.js b/workspaces/arborist/lib/arborist/build-ideal-tree.js index fd58271fae857..dffcd546b8677 100644 --- a/workspaces/arborist/lib/arborist/build-ideal-tree.js +++ b/workspaces/arborist/lib/arborist/build-ideal-tree.js @@ -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 }) diff --git a/workspaces/arborist/lib/shrinkwrap.js b/workspaces/arborist/lib/shrinkwrap.js index a7a68c98c6d6d..b45fea0ac6111 100644 --- a/workspaces/arborist/lib/shrinkwrap.js +++ b/workspaces/arborist/lib/shrinkwrap.js @@ -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. @@ -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