From 9be425d513f5b200d54902532fa073a1f8a68075 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20Wa=CC=88rting?= Date: Sat, 14 Aug 2021 12:47:24 +0200 Subject: [PATCH] use private class fields --- lib/describe.js | 80 ++++++++++++++++++++++++------------------------ lib/stringify.js | 36 +++++++++++----------- 2 files changed, 58 insertions(+), 58 deletions(-) diff --git a/lib/describe.js b/lib/describe.js index 53f058a..84cb129 100644 --- a/lib/describe.js +++ b/lib/describe.js @@ -203,19 +203,19 @@ class Describer { } } - _stringify(type, typeString, useLongFormat) { + #stringify(type, typeString, useLongFormat) { const context = new Context({ type: typeString || stringify(type, this._stringifyOptions) }); const result = new Result(); addModifiers(this, context, result, type, useLongFormat); - result.description = this._translate('type', context).trim(); + result.description = this.#translate('type', context).trim(); return result; } - _translate(key, context) { + #translate(key, context) { let result; let templateFunction = _.get(this._templates, key); @@ -242,23 +242,23 @@ class Describer { return result; } - _modifierHelper(key, modifierPrefix = '', context) { + #modifierHelper(key, modifierPrefix = '', context) { return { extended: key ? - this._translate(`${modifierPrefix}.${FORMATS.EXTENDED}.${key}`, context) : + this.#translate(`${modifierPrefix}.${FORMATS.EXTENDED}.${key}`, context) : '', simple: key ? - this._translate(`${modifierPrefix}.${FORMATS.SIMPLE}.${key}`, context) : + this.#translate(`${modifierPrefix}.${FORMATS.SIMPLE}.${key}`, context) : '' }; } - _translateModifier(key, context) { - return this._modifierHelper(key, 'modifiers', context); + #translateModifier(key, context) { + return this.#modifierHelper(key, 'modifiers', context); } - _translateFunctionModifier(key, context) { - return this._modifierHelper(key, 'function', context); + #translateFunctionModifier(key, context) { + return this.#modifierHelper(key, 'function', context); } application(type, useLongFormat) { @@ -273,7 +273,7 @@ class Describer { context.application = this.type(applications.pop()).description; context.keyApplication = applications.length ? this.type(applications.pop()).description : ''; - result.description = this._translate(key, context).trim(); + result.description = this.#translate(key, context).trim(); return result; } @@ -284,7 +284,7 @@ class Describer { const result = new Result(); addModifiers(this, context, result, type, useLongFormat); - result.description = this._combineMultiple(items, context, 'union', 'element'); + result.description = this.#combineMultiple(items, context, 'union', 'element'); return result; } @@ -293,7 +293,7 @@ class Describer { const context = new Context({'functionNew': this.type(funcNew).description}); const key = funcNew ? 'new' : ''; - return this._translateFunctionModifier(key, context); + return this.#translateFunctionModifier(key, context); } nullable(nullable) { @@ -312,22 +312,22 @@ class Describer { key = ''; } - return this._translateModifier(key); + return this.#translateModifier(key); } optional(optional) { const key = (optional === true) ? 'optional' : ''; - return this._translateModifier(key); + return this.#translateModifier(key); } repeatable(repeatable) { const key = (repeatable === true) ? 'repeatable' : ''; - return this._translateModifier(key); + return this.#translateModifier(key); } - _combineMultiple(items, context, keyName, contextName) { + #combineMultiple(items, context, keyName, contextName) { const result = new Result(); const self = this; let strings; @@ -341,19 +341,19 @@ class Describer { // falls through case 1: context[contextName] = strings[0] || ''; - result.description = this._translate(`${keyName}.first.one`, context); + result.description = this.#translate(`${keyName}.first.one`, context); break; case 2: strings.forEach((item, idx) => { const key = `${keyName + (idx === 0 ? '.first' : '.last' )}.two`; context[contextName] = item; - result.description += self._translate(key, context); + result.description += self.#translate(key, context); }); break; default: result.description = strings.reduce(reduceMultiple.bind(null, context, keyName, - contextName, this._translate.bind(this)), ''); + contextName, this.#translate.bind(this)), ''); } return result.description.trim(); @@ -379,7 +379,7 @@ class Describer { if (functionContext.functionNew) { strings.unshift(functionContext.functionNew); } - result.description = this._combineMultiple(strings, context, 'params', 'param'); + result.description = this.#combineMultiple(strings, context, 'params', 'param'); return result; } @@ -388,7 +388,7 @@ class Describer { const context = new Context({'functionThis': this.type(funcThis).description}); const key = funcThis ? 'this' : ''; - return this._translateFunctionModifier(key, context); + return this.#translateFunctionModifier(key, context); } type(type, useLongFormat) { @@ -406,19 +406,19 @@ class Describer { switch (type.type) { case Types.AllLiteral: - result = this._stringify(type, this._translate('all'), useLongFormat); + result = this.#stringify(type, this.#translate('all'), useLongFormat); break; case Types.FunctionType: - result = this._signature(type, useLongFormat); + result = this.#signature(type, useLongFormat); break; case Types.NameExpression: - result = this._stringify(type, null, useLongFormat); + result = this.#stringify(type, null, useLongFormat); break; case Types.NullLiteral: - result = this._stringify(type, this._translate('null'), useLongFormat); + result = this.#stringify(type, this.#translate('null'), useLongFormat); break; case Types.RecordType: - result = this._record(type, useLongFormat); + result = this.#record(type, useLongFormat); break; case Types.TypeApplication: result = this.application(type, useLongFormat); @@ -427,10 +427,10 @@ class Describer { result = this.elements(type, useLongFormat); break; case Types.UndefinedLiteral: - result = this._stringify(type, this._translate('undefined'), useLongFormat); + result = this.#stringify(type, this.#translate('undefined'), useLongFormat); break; case Types.UnknownLiteral: - result = this._stringify(type, this._translate('unknown'), useLongFormat); + result = this.#stringify(type, this.#translate('unknown'), useLongFormat); break; default: throw new Error(`Unknown type: ${JSON.stringify(type)}`); @@ -439,20 +439,20 @@ class Describer { return result; } - _record(type, useLongFormat) { + #record(type, useLongFormat) { const context = new Context(); let items; const result = new Result(); - items = this._recordFields(type.fields); + items = this.#recordFields(type.fields); addModifiers(this, context, result, type, useLongFormat); - result.description = this._combineMultiple(items, context, 'record', 'field'); + result.description = this.#combineMultiple(items, context, 'record', 'field'); return result; } - _recordFields(fields) { + #recordFields(fields) { const context = new Context(); let result = []; const self = this; @@ -469,13 +469,13 @@ class Describer { context.type = self.type(field.value).description; } - return self._translate(key, context); + return self.#translate(key, context); }); return result; } - _getHrefForString(nameString) { + #getHrefForString(nameString) { let href = ''; const links = this._options.links; @@ -493,8 +493,8 @@ class Describer { return href; } - _addLinks(nameString) { - const href = this._getHrefForString(nameString); + #addLinks(nameString) { + const href = this.#getHrefForString(nameString); let link = nameString; let linkClass = this._options.linkClass || ''; @@ -517,12 +517,12 @@ class Describer { context.type = this.type(type).description; addModifiers(this, context, result, type, useLongFormat); - result.description = this._translate(key, context); + result.description = this.#translate(key, context); return result; } - _signature(type, useLongFormat) { + #signature(type, useLongFormat) { const context = new Context(); const kind = modifierKind(useLongFormat); const result = new Result(); @@ -542,7 +542,7 @@ class Describer { } } - result.description += this._translate(`function.${kind}.signature`, context).trim(); + result.description += this.#translate(`function.${kind}.signature`, context).trim(); return result; } diff --git a/lib/stringify.js b/lib/stringify.js index 441d103..6f16ded 100644 --- a/lib/stringify.js +++ b/lib/stringify.js @@ -123,48 +123,48 @@ class Stringifier { switch (type.type) { case Types.AllLiteral: - typeString = this._formatNameAndType(type, '*'); + typeString = this.#formatNameAndType(type, '*'); break; case Types.FunctionType: - typeString = this._signature(type); + typeString = this.#signature(type); break; case Types.NullLiteral: - typeString = this._formatNameAndType(type, 'null'); + typeString = this.#formatNameAndType(type, 'null'); break; case Types.RecordType: - typeString = this._record(type); + typeString = this.#record(type); break; case Types.TypeApplication: typeString = this.type(type.expression) + this.applications(type.applications); break; case Types.UndefinedLiteral: - typeString = this._formatNameAndType(type, 'undefined'); + typeString = this.#formatNameAndType(type, 'undefined'); break; case Types.TypeUnion: typeString = this.elements(type.elements); break; case Types.UnknownLiteral: - typeString = this._formatNameAndType(type, '?'); + typeString = this.#formatNameAndType(type, '?'); break; default: - typeString = this._formatNameAndType(type); + typeString = this.#formatNameAndType(type); } // add optional/nullable/repeatable modifiers if (!this._options._ignoreModifiers) { - typeString = this._addModifiers(type, typeString); + typeString = this.#addModifiers(type, typeString); } return typeString; } - _record(type) { - const fields = this._recordFields(type.fields); + #record(type) { + const fields = this.#recordFields(type.fields); return `{${fields.join(', ')}}`; } - _recordFields(fields) { + #recordFields(fields) { let field; let keyAndValue; @@ -187,7 +187,7 @@ class Stringifier { } // Adds optional, nullable, and repeatable modifiers if necessary. - _addModifiers(type, typeString) { + #addModifiers(type, typeString) { let combined; let optional = ''; @@ -203,8 +203,8 @@ class Stringifier { return repeatable + combined + optional; } - _addLinks(nameString) { - const href = this._getHrefForString(nameString); + #addLinks(nameString) { + const href = this.#getHrefForString(nameString); let link = nameString; let linkClass = this._options.linkClass || ''; @@ -219,16 +219,16 @@ class Stringifier { return link; } - _formatNameAndType(type, literal) { + #formatNameAndType(type, literal) { let nameString = type.name || literal || ''; const typeString = type.type ? this.type(type.type) : ''; - nameString = this._addLinks(nameString); + nameString = this.#addLinks(nameString); return combineNameAndType(nameString, typeString); } - _getHrefForString(nameString) { + #getHrefForString(nameString) { let href = ''; const links = this._options.links; @@ -246,7 +246,7 @@ class Stringifier { return href; } - _signature(type) { + #signature(type) { let param; let prop; let signature;