Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use private class fields #67

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 40 additions & 40 deletions lib/describe.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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) {
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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) {
Expand All @@ -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;
Expand All @@ -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();
Expand All @@ -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;
}
Expand All @@ -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) {
Expand All @@ -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);
Expand All @@ -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)}`);
Expand All @@ -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;
Expand All @@ -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;

Expand All @@ -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 || '';

Expand All @@ -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();
Expand All @@ -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;
}
Expand Down
36 changes: 18 additions & 18 deletions lib/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -187,7 +187,7 @@ class Stringifier {
}

// Adds optional, nullable, and repeatable modifiers if necessary.
_addModifiers(type, typeString) {
#addModifiers(type, typeString) {
let combined;

let optional = '';
Expand All @@ -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 || '';

Expand All @@ -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;

Expand All @@ -246,7 +246,7 @@ class Stringifier {
return href;
}

_signature(type) {
#signature(type) {
let param;
let prop;
let signature;
Expand Down