Skip to content

Commit 667cff5

Browse files
committed
fix: move to private attributes where possible
_loadError and _valid still have some magic to them
1 parent b8006ad commit 667cff5

File tree

1 file changed

+57
-64
lines changed

1 file changed

+57
-64
lines changed

workspaces/config/lib/index.js

Lines changed: 57 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,12 @@ const confTypes = new Set([
7272
'cli',
7373
])
7474

75-
const _loaded = Symbol('loaded')
76-
const _get = Symbol('get')
77-
const _find = Symbol('find')
78-
const _loadObject = Symbol('loadObject')
79-
const _loadFile = Symbol('loadFile')
80-
const _checkDeprecated = Symbol('checkDeprecated')
81-
const _flatten = Symbol('flatten')
82-
const _flatOptions = Symbol('flatOptions')
83-
8475
class Config {
76+
#loaded = false
77+
#flatten
78+
// populated the first time we flatten the object
79+
#flatOptions = null
80+
8581
static get typeDefs () {
8682
return typeDefs
8783
}
@@ -113,9 +109,7 @@ class Config {
113109
}
114110
}
115111

116-
// populated the first time we flatten the object
117-
this[_flatOptions] = null
118-
this[_flatten] = flatten
112+
this.#flatten = flatten
119113
this.types = types
120114
this.shorthands = shorthands
121115
this.defaults = defaults
@@ -159,26 +153,27 @@ class Config {
159153
}
160154
Object.freeze(this.list)
161155

162-
this[_loaded] = false
156+
this.#loaded = false
163157
}
164158

165159
get loaded () {
166-
return this[_loaded]
160+
return this.#loaded
167161
}
168162

169163
get prefix () {
170-
return this[_get]('global') ? this.globalPrefix : this.localPrefix
164+
return this.#get('global') ? this.globalPrefix : this.localPrefix
171165
}
172166

173167
// return the location where key is found.
174168
find (key) {
175169
if (!this.loaded) {
176170
throw new Error('call config.load() before reading values')
177171
}
178-
return this[_find](key)
172+
// TODO single use?
173+
return this.#find(key)
179174
}
180175

181-
[_find] (key) {
176+
#find (key) {
182177
// have to look in reverse order
183178
const entries = [...this.data.entries()]
184179
for (let i = entries.length - 1; i > -1; i--) {
@@ -194,12 +189,12 @@ class Config {
194189
if (!this.loaded) {
195190
throw new Error('call config.load() before reading values')
196191
}
197-
return this[_get](key, where)
192+
return this.#get(key, where)
198193
}
199194

200195
// we need to get values sometimes, so use this internal one to do so
201196
// while in the process of loading.
202-
[_get] (key, where = null) {
197+
#get (key, where = null) {
203198
if (where !== null && !confTypes.has(where)) {
204199
throw new Error('invalid config location param: ' + where)
205200
}
@@ -214,32 +209,32 @@ class Config {
214209
if (!confTypes.has(where)) {
215210
throw new Error('invalid config location param: ' + where)
216211
}
217-
this[_checkDeprecated](key)
212+
this.#checkDeprecated(key)
218213
const { data } = this.data.get(where)
219214
data[key] = val
220215

221216
// this is now dirty, the next call to this.valid will have to check it
222217
this.data.get(where)[_valid] = null
223218

224219
// the flat options are invalidated, regenerate next time they're needed
225-
this[_flatOptions] = null
220+
this.#flatOptions = null
226221
}
227222

228223
get flat () {
229-
if (this[_flatOptions]) {
230-
return this[_flatOptions]
224+
if (this.#flatOptions) {
225+
return this.#flatOptions
231226
}
232227

233228
// create the object for flat options passed to deps
234229
process.emit('time', 'config:load:flatten')
235-
this[_flatOptions] = {}
230+
this.#flatOptions = {}
236231
// walk from least priority to highest
237232
for (const { data } of this.data.values()) {
238-
this[_flatten](data, this[_flatOptions])
233+
this.#flatten(data, this.#flatOptions)
239234
}
240235
process.emit('timeEnd', 'config:load:flatten')
241236

242-
return this[_flatOptions]
237+
return this.#flatOptions
243238
}
244239

245240
delete (key, where = 'cli') {
@@ -290,8 +285,8 @@ class Config {
290285
process.emit('timeEnd', 'config:load:global')
291286

292287
// set this before calling setEnvs, so that we don't have to share
293-
// symbols, as that module also does a bunch of get operations
294-
this[_loaded] = true
288+
// private attributes, as that module also does a bunch of get operations
289+
this.#loaded = true
295290

296291
// set proper globalPrefix now that everything is loaded
297292
this.globalPrefix = this.get('prefix')
@@ -307,7 +302,7 @@ class Config {
307302
this.loadGlobalPrefix()
308303
this.loadHome()
309304

310-
this[_loadObject]({
305+
this.#loadObject({
311306
...this.defaults,
312307
prefix: this.globalPrefix,
313308
}, 'default', 'default values')
@@ -316,13 +311,13 @@ class Config {
316311

317312
// the metrics-registry defaults to the current resolved value of
318313
// the registry, unless overridden somewhere else.
319-
settableGetter(data, 'metrics-registry', () => this[_get]('registry'))
314+
settableGetter(data, 'metrics-registry', () => this.#get('registry'))
320315

321316
// if the prefix is set on cli, env, or userconfig, then we need to
322317
// default the globalconfig file to that location, instead of the default
323318
// global prefix. It's weird that `npm get globalconfig --prefix=/foo`
324319
// returns `/foo/etc/npmrc`, but better to not change it at this point.
325-
settableGetter(data, 'globalconfig', () => resolve(this[_get]('prefix'), 'etc/npmrc'))
320+
settableGetter(data, 'globalconfig', () => resolve(this.#get('prefix'), 'etc/npmrc'))
326321
}
327322

328323
loadHome () {
@@ -363,7 +358,7 @@ class Config {
363358
}
364359
conf[key] = envVal
365360
}
366-
this[_loadObject](conf, 'env', 'environment')
361+
this.#loadObject(conf, 'env', 'environment')
367362
}
368363

369364
loadCLI () {
@@ -373,7 +368,7 @@ class Config {
373368
nopt.invalidHandler = null
374369
this.parsedArgv = conf.argv
375370
delete conf.argv
376-
this[_loadObject](conf, 'cli', 'command line options')
371+
this.#loadObject(conf, 'cli', 'command line options')
377372
}
378373

379374
get valid () {
@@ -541,7 +536,7 @@ class Config {
541536
log.warn('invalid config', msg, desc)
542537
}
543538

544-
[_loadObject] (obj, where, source, er = null) {
539+
#loadObject (obj, where, source, er = null) {
545540
const conf = this.data.get(where)
546541
if (conf.source) {
547542
const m = `double-loading "${where}" configs from ${source}, ` +
@@ -568,14 +563,14 @@ class Config {
568563
const k = envReplace(key, this.env)
569564
const v = this.parseField(value, k)
570565
if (where !== 'default') {
571-
this[_checkDeprecated](k, where, obj, [key, value])
566+
this.#checkDeprecated(k, where, obj, [key, value])
572567
}
573568
conf.data[k] = v
574569
}
575570
}
576571
}
577572

578-
[_checkDeprecated] (key, where, obj, kv) {
573+
#checkDeprecated (key, where, obj, kv) {
579574
// XXX(npm9+) make this throw an error
580575
if (this.deprecated[key]) {
581576
log.warn('config', key, this.deprecated[key])
@@ -587,18 +582,18 @@ class Config {
587582
return parseField(f, key, this, listElement)
588583
}
589584

590-
async [_loadFile] (file, type) {
585+
async #loadFile (file, type) {
591586
process.emit('time', 'config:load:file:' + file)
592587
// only catch the error from readFile, not from the loadObject call
593588
await readFile(file, 'utf8').then(
594-
data => this[_loadObject](ini.parse(data), type, file),
595-
er => this[_loadObject](null, type, file, er)
589+
data => this.#loadObject(ini.parse(data), type, file),
590+
er => this.#loadObject(null, type, file, er)
596591
)
597592
process.emit('timeEnd', 'config:load:file:' + file)
598593
}
599594

600595
loadBuiltinConfig () {
601-
return this[_loadFile](resolve(this.npmPath, 'npmrc'), 'builtin')
596+
return this.#loadFile(resolve(this.npmPath, 'npmrc'), 'builtin')
602597
}
603598

604599
async loadProjectConfig () {
@@ -613,7 +608,7 @@ class Config {
613608
this.localPackage = await fileExists(this.localPrefix, 'package.json')
614609
}
615610

616-
if (this[_get]('global') === true || this[_get]('location') === 'global') {
611+
if (this.#get('global') === true || this.#get('location') === 'global') {
617612
this.data.get('project').source = '(global mode enabled, ignored)'
618613
this.sources.set(this.data.get('project').source, 'project')
619614
return
@@ -625,23 +620,23 @@ class Config {
625620
// up loading the "project" config where the "userconfig" will be,
626621
// which causes some calamaties. So, we only load project config if
627622
// it doesn't match what the userconfig will be.
628-
if (projectFile !== this[_get]('userconfig')) {
629-
return this[_loadFile](projectFile, 'project')
623+
if (projectFile !== this.#get('userconfig')) {
624+
return this.#loadFile(projectFile, 'project')
630625
} else {
631626
this.data.get('project').source = '(same as "user" config, ignored)'
632627
this.sources.set(this.data.get('project').source, 'project')
633628
}
634629
}
635630

636631
async loadLocalPrefix () {
637-
const cliPrefix = this[_get]('prefix', 'cli')
632+
const cliPrefix = this.#get('prefix', 'cli')
638633
if (cliPrefix) {
639634
this.localPrefix = cliPrefix
640635
return
641636
}
642637

643-
const cliWorkspaces = this[_get]('workspaces', 'cli')
644-
const isGlobal = this[_get]('global') || this[_get]('location') === 'global'
638+
const cliWorkspaces = this.#get('workspaces', 'cli')
639+
const isGlobal = this.#get('global') || this.#get('location') === 'global'
645640

646641
for (const p of walkUp(this.cwd)) {
647642
// HACK: this is an option set in tests to stop the local prefix from being set
@@ -701,11 +696,11 @@ class Config {
701696
}
702697

703698
loadUserConfig () {
704-
return this[_loadFile](this[_get]('userconfig'), 'user')
699+
return this.#loadFile(this.#get('userconfig'), 'user')
705700
}
706701

707702
loadGlobalConfig () {
708-
return this[_loadFile](this[_get]('globalconfig'), 'global')
703+
return this.#loadFile(this.#get('globalconfig'), 'global')
709704
}
710705

711706
async save (where) {
@@ -717,7 +712,6 @@ class Config {
717712
}
718713

719714
const conf = this.data.get(where)
720-
conf[_raw] = { ...conf.data }
721715
conf[_loadError] = null
722716

723717
if (where === 'user') {
@@ -870,41 +864,40 @@ class Config {
870864
}
871865
}
872866

873-
const _data = Symbol('data')
874-
const _raw = Symbol('raw')
875867
const _loadError = Symbol('loadError')
876-
const _source = Symbol('source')
877868
const _valid = Symbol('valid')
869+
878870
class ConfigData {
871+
#data
872+
#source = null
873+
#raw = null
879874
constructor (parent) {
880-
this[_data] = Object.create(parent && parent.data)
881-
this[_source] = null
882-
this[_loadError] = null
883-
this[_raw] = null
875+
this.#data = Object.create(parent && parent.data)
876+
this.#raw = null
884877
this[_valid] = true
885878
}
886879

887880
get data () {
888-
return this[_data]
881+
return this.#data
889882
}
890883

891884
get valid () {
892885
return this[_valid]
893886
}
894887

895888
set source (s) {
896-
if (this[_source]) {
889+
if (this.#source) {
897890
throw new Error('cannot set ConfigData source more than once')
898891
}
899-
this[_source] = s
892+
this.#source = s
900893
}
901894

902895
get source () {
903-
return this[_source]
896+
return this.#source
904897
}
905898

906899
set loadError (e) {
907-
if (this[_loadError] || this[_raw]) {
900+
if (this[_loadError] || this.#raw) {
908901
throw new Error('cannot set ConfigData loadError after load')
909902
}
910903
this[_loadError] = e
@@ -915,14 +908,14 @@ class ConfigData {
915908
}
916909

917910
set raw (r) {
918-
if (this[_raw] || this[_loadError]) {
911+
if (this.#raw || this[_loadError]) {
919912
throw new Error('cannot set ConfigData raw after load')
920913
}
921-
this[_raw] = r
914+
this.#raw = r
922915
}
923916

924917
get raw () {
925-
return this[_raw]
918+
return this.#raw
926919
}
927920
}
928921

0 commit comments

Comments
 (0)