diff options
Diffstat (limited to 'deps/npm/node_modules/@npmcli/arborist/lib/node.js')
-rw-r--r-- | deps/npm/node_modules/@npmcli/arborist/lib/node.js | 149 |
1 files changed, 73 insertions, 76 deletions
diff --git a/deps/npm/node_modules/@npmcli/arborist/lib/node.js b/deps/npm/node_modules/@npmcli/arborist/lib/node.js index b21a3d8e3d..bdc021b7c1 100644 --- a/deps/npm/node_modules/@npmcli/arborist/lib/node.js +++ b/deps/npm/node_modules/@npmcli/arborist/lib/node.js @@ -39,7 +39,7 @@ const npa = require('npm-package-arg') const debug = require('./debug.js') const gatherDepSet = require('./gather-dep-set.js') const treeCheck = require('./tree-check.js') -const walkUp = require('walk-up-path') +const { walkUp } = require('walk-up-path') const { resolve, relative, dirname, basename } = require('path') const util = require('util') @@ -47,21 +47,15 @@ const _package = Symbol('_package') const _parent = Symbol('_parent') const _target = Symbol.for('_target') const _fsParent = Symbol('_fsParent') -const _loadDepType = Symbol('_loadDepType') -const _loadWorkspaces = Symbol('_loadWorkspaces') const _reloadNamedEdges = Symbol('_reloadNamedEdges') // overridden by Link class const _loadDeps = Symbol.for('Arborist.Node._loadDeps') -const _root = Symbol('_root') const _refreshLocation = Symbol.for('_refreshLocation') const _changePath = Symbol.for('_changePath') // used by Link class as well const _delistFromMeta = Symbol.for('_delistFromMeta') -const _global = Symbol.for('global') -const _workspaces = Symbol('_workspaces') const _explain = Symbol('_explain') const _explanation = Symbol('_explanation') -const _meta = Symbol('_meta') const relpath = require('./relpath.js') const consistentResolve = require('./consistent-resolve.js') @@ -72,6 +66,11 @@ const CaseInsensitiveMap = require('./case-insensitive-map.js') const querySelectorAll = require('./query-selector-all.js') class Node { + #global + #meta + #root + #workspaces + constructor (options) { // NB: path can be null if it's a link target const { @@ -109,9 +108,9 @@ class Node { this.queryContext = {} // true if part of a global install - this[_global] = global + this.#global = global - this[_workspaces] = null + this.#workspaces = null this.errors = error ? [error] : [] this.isInStore = isInStore @@ -165,7 +164,7 @@ class Node { this.children = new CaseInsensitiveMap() this.fsChildren = new Set() - this.inventory = new Inventory({}) + this.inventory = new Inventory() this.tops = new Set() this.linksIn = new Set(linksIn || []) @@ -262,18 +261,21 @@ class Node { } get meta () { - return this[_meta] + return this.#meta } set meta (meta) { - this[_meta] = meta + this.#meta = meta if (meta) { meta.add(this) } } get global () { - return this.root[_global] + if (this.#root === this) { + return this.#global + } + return this.#root.global } // true for packages installed directly in the global node_modules folder @@ -282,21 +284,21 @@ class Node { } get workspaces () { - return this[_workspaces] + return this.#workspaces } set workspaces (workspaces) { // deletes edges if they already exists - if (this[_workspaces]) { - for (const name of this[_workspaces].keys()) { + if (this.#workspaces) { + for (const name of this.#workspaces.keys()) { if (!workspaces.has(name)) { this.edgesOut.get(name).detach() } } } - this[_workspaces] = workspaces - this[_loadWorkspaces]() + this.#workspaces = workspaces + this.#loadWorkspaces() this[_loadDeps]() } @@ -367,7 +369,7 @@ class Node { pkg = {} } this[_package] = pkg - this[_loadWorkspaces]() + this.#loadWorkspaces() this[_loadDeps]() // do a hard reload, since the dependents may now be valid or invalid // as a result of the package change. @@ -569,12 +571,12 @@ class Node { // this allows us to do new Node({...}) and then set the root later. // just make the assignment so we don't lose it, and move on. if (!this.path || !root.realpath || !root.path) { - this[_root] = root + this.#root = root return } // temporarily become a root node - this[_root] = this + this.#root = this // break all linksIn, we're going to re-set them if needed later for (const link of this.linksIn) { @@ -618,7 +620,7 @@ class Node { current.root = null } - this[_root] = root + this.#root = root // set this.location and add to inventory this[_refreshLocation]() @@ -684,22 +686,22 @@ class Node { // the node at nm/a, which might have the root node as a fsParent. // we can't rely on the public setter here, because it calls into // this function to set up these references! - const nmloc = `${this.location}${this.location ? '/' : ''}node_modules/` - const isChild = n => n.location === nmloc + n.name // check dirname so that /foo isn't treated as the fsparent of /foo-bar - const isFsChild = n => { - return dirname(n.path).startsWith(this.path) && - n !== this && - !n.parent && - (!n.fsParent || - n.fsParent === this || - dirname(this.path).startsWith(n.fsParent.path)) - } - const isKid = n => isChild(n) || isFsChild(n) - + const nmloc = `${this.location}${this.location ? '/' : ''}node_modules/` // only walk top nodes, since anything else already has a parent. for (const child of root.tops) { - if (!isKid(child)) { + const isChild = child.location === nmloc + child.name + const isFsChild = + dirname(child.path).startsWith(this.path) && + child !== this && + !child.parent && + ( + !child.fsParent || + child.fsParent === this || + dirname(this.path).startsWith(child.fsParent.path) + ) + + if (!isChild && !isFsChild) { continue } @@ -712,7 +714,7 @@ class Node { child.fsParent.fsChildren.delete(child) } child[_fsParent] = null - if (isChild(child)) { + if (isChild) { this.children.set(child.name, child) child[_parent] = this root.tops.delete(child) @@ -823,19 +825,21 @@ class Node { } // tree should always be valid upon root setter completion. treeCheck(this) - treeCheck(root) + if (this !== root) { + treeCheck(root) + } } get root () { - return this[_root] || this + return this.#root || this } - [_loadWorkspaces] () { - if (!this[_workspaces]) { + #loadWorkspaces () { + if (!this.#workspaces) { return } - for (const [name, path] of this[_workspaces].entries()) { + for (const [name, path] of this.#workspaces.entries()) { new Edge({ from: this, name, spec: `file:${path.replace(/#/g, '%23')}`, type: 'workspace' }) } } @@ -851,23 +855,24 @@ class Node { // but don't have a 'path' field, only a 'realpath', because we // don't know their canonical location. We don't need their devDeps. const pd = this.package.peerDependencies + const ad = this.package.acceptDependencies || {} if (pd && typeof pd === 'object' && !this.legacyPeerDeps) { const pm = this.package.peerDependenciesMeta || {} const peerDependencies = {} const peerOptional = {} for (const [name, dep] of Object.entries(pd)) { - if (pm[name] && pm[name].optional) { + if (pm[name]?.optional) { peerOptional[name] = dep } else { peerDependencies[name] = dep } } - this[_loadDepType](peerDependencies, 'peer') - this[_loadDepType](peerOptional, 'peerOptional') + this.#loadDepType(peerDependencies, 'peer', ad) + this.#loadDepType(peerOptional, 'peerOptional', ad) } - this[_loadDepType](this.package.dependencies, 'prod') - this[_loadDepType](this.package.optionalDependencies, 'optional') + this.#loadDepType(this.package.dependencies, 'prod', ad) + this.#loadDepType(this.package.optionalDependencies, 'optional', ad) const { globalTop, isTop, path, sourceReference } = this const { @@ -878,12 +883,11 @@ class Node { const thisDev = isTop && !globalTop && path const srcDev = !sourceReference || srcTop && !srcGlobalTop && srcPath if (thisDev && srcDev) { - this[_loadDepType](this.package.devDependencies, 'dev') + this.#loadDepType(this.package.devDependencies, 'dev', ad) } } - [_loadDepType] (deps, type) { - const ad = this.package.acceptDependencies || {} + #loadDepType (deps, type, ad) { // Because of the order in which _loadDeps runs, we always want to // prioritize a new edge over an existing one for (const [name, spec] of Object.entries(deps || {})) { @@ -895,14 +899,8 @@ class Node { } get fsParent () { - const parent = this[_fsParent] - /* istanbul ignore next - should be impossible */ - debug(() => { - if (parent === this) { - throw new Error('node set to its own fsParent') - } - }) - return parent + // in debug setter prevents fsParent from being this + return this[_fsParent] } set fsParent (fsParent) { @@ -997,7 +995,7 @@ class Node { // root dependency brings peer deps along with it. In that case, we // will go ahead and create the invalid state, and then try to resolve // it with more tree construction, because it's a user request. - canReplaceWith (node, ignorePeers = []) { + canReplaceWith (node, ignorePeers) { if (node.name !== this.name) { return false } @@ -1010,7 +1008,6 @@ class Node { if (node.overrides !== this.overrides) { return false } - ignorePeers = new Set(ignorePeers) // gather up all the deps of this node and that are only depended @@ -1022,11 +1019,10 @@ class Node { // when replacing peer sets, we need to be able to replace the entire // peer group, which means we ignore incoming edges from other peers // within the replacement set. - const ignored = !this.isTop && + if (!this.isTop && edge.from.parent === this.parent && edge.peer && - ignorePeers.has(edge.from.name) - if (ignored) { + ignorePeers.has(edge.from.name)) { continue } @@ -1156,9 +1152,7 @@ class Node { // something case-insensitively, so merely setting name and path won't // have the desired effect. just set the path so it'll collide in the // parent's children map, and leave it at that. - const nameMatch = node.parent && - node.parent.children.get(this.name) === node - if (nameMatch) { + if (node.parent?.children.get(this.name) === node) { this.path = resolve(node.parent.path, 'node_modules', this.name) } else { this.path = node.path @@ -1193,14 +1187,8 @@ class Node { } get parent () { - const parent = this[_parent] - /* istanbul ignore next - should be impossible */ - debug(() => { - if (parent === this) { - throw new Error('node set to its own parent') - } - }) - return parent + // setter prevents _parent from being this + return this[_parent] } // This setter keeps everything in order when we move a node from @@ -1405,7 +1393,10 @@ class Node { } get depth () { - return this.isTop ? 0 : this.parent.depth + 1 + if (this.isTop) { + return 0 + } + return this.parent.depth + 1 } get isTop () { @@ -1413,7 +1404,10 @@ class Node { } get top () { - return this.isTop ? this : this.parent.top + if (this.isTop) { + return this + } + return this.parent.top } get isFsTop () { @@ -1421,7 +1415,10 @@ class Node { } get fsTop () { - return this.isFsTop ? this : this.fsParent.fsTop + if (this.isFsTop) { + return this + } + return this.fsParent.fsTop } get resolveParent () { |