Avoid using _ as parameter name.

This commit is contained in:
sasensi 2018-12-03 14:03:24 +01:00 committed by Jürg Lehni
parent f25690aa91
commit b8a0743e3d

View file

@ -25,40 +25,40 @@ classes.forEach(cls => {
cls.comment = formatComment(cls.comment, 'class'); cls.comment = formatComment(cls.comment, 'class');
// Build a filter for deprecated or inherited methods or properties. // Build a filter for deprecated or inherited methods or properties.
const filter = _ => !_.deprecated && _.memberOf == cls.alias && !_.isNamespace; const filter = it => !it.deprecated && it.memberOf == cls.alias && !it.isNamespace;
// Format properties. // Format properties.
cls.properties = cls.properties cls.properties = cls.properties
.filter(filter) .filter(filter)
.map(_ => ({ .map(it => ({
name: _._name, name: it._name,
type: formatType(_.type), type: formatType(it.type),
static: formatStatic(_.isStatic), static: formatStatic(it.isStatic),
readOnly: formatReadOnly(_.readOnly), readOnly: formatReadOnly(it.readOnly),
comment: formatComment(_.comment) comment: formatComment(it.comment)
})); }));
// Format methods. // Format methods.
const methods = cls.methods const methods = cls.methods
.filter(filter) .filter(filter)
.map(_ => { .map(it => {
const name = formatMethodName(_._name); const name = formatMethodName(it._name);
const isStaticConstructor = _.isStatic && _.isConstructor; const isStaticConstructor = it.isStatic && it.isConstructor;
return { return {
name: name, name: name,
// Constructors don't need return type. // Constructors don't need return type.
type: !_.isConstructor type: !it.isConstructor
? formatType(getMethodReturnType(_), true) ? formatType(getMethodReturnType(it), true)
: '', : '',
static: formatStatic(_.isStatic), static: formatStatic(it.isStatic),
// This flag is only used below to filter methods. // This flag is only used below to filter methods.
isStaticConstructor: isStaticConstructor, isStaticConstructor: isStaticConstructor,
comment: formatComment(_.comment, 'desc', _.isConstructor), comment: formatComment(it.comment, 'desc', it.isConstructor),
params: _._params params: it._params
? _._params ? it._params
// Filter internal parameters (starting with underscore). // Filter internal parameters (starting with underscore).
.filter(_ => !/^_/.test(_.name)) .filter(it => !/^_/.test(it.name))
.map(_ => formatParameter(_, isStaticConstructor && cls)) .map(it => formatParameter(it, isStaticConstructor && cls))
.join(', ') .join(', ')
: '' : ''
}; };
@ -72,7 +72,7 @@ classes.forEach(cls => {
methods.forEach(method => { methods.forEach(method => {
if (method.isStaticConstructor) { if (method.isStaticConstructor) {
// Group static constructors by method name. // Group static constructors by method name.
let staticConstructors = cls.staticConstructors.find(_ => _.name === method.name); let staticConstructors = cls.staticConstructors.find(it => it.name === method.name);
if (!staticConstructors) { if (!staticConstructors) {
staticConstructors = { staticConstructors = {
name: method.name, name: method.name,
@ -92,11 +92,11 @@ classes.forEach(cls => {
// Format global vriables. // Format global vriables.
globals = globals globals = globals
// Filter global variables that make no sense in type definition. // Filter global variables that make no sense in type definition.
.filter(_ => !/^on/.test(_._name) && _._name !== 'paper') .filter(it => !/^on/.test(it._name) && it._name !== 'paper')
.map(_ => ({ .map(it => ({
name: _._name, name: it._name,
type: formatType(_.type), type: formatType(it.type),
comment: formatComment(_.comment) comment: formatComment(it.comment)
})); }));
// Format data trough a mustache template. // Format data trough a mustache template.
@ -162,7 +162,7 @@ function parseType(type, isMethodReturnType, staticConstructorClass) {
// `rectangle` parameter type must be mapped to `paper.Rectangle` as it // `rectangle` parameter type must be mapped to `paper.Rectangle` as it
// is declared inside a `Path` namespace and would otherwise be wrongly // is declared inside a `Path` namespace and would otherwise be wrongly
// assumed as being the type of `Path.Rectangle` class. // assumed as being the type of `Path.Rectangle` class.
if (staticConstructorClass && staticConstructorClass.methods.find(_ => _.isStatic && _.isConstructor && formatMethodName(_._name) === singleType) if (staticConstructorClass && staticConstructorClass.methods.find(it => it.isStatic && it.isConstructor && formatMethodName(it._name) === singleType)
) { ) {
return 'paper.' + type; return 'paper.' + type;
} }
@ -183,19 +183,19 @@ function formatMethodName(methodName) {
return methodName; return methodName;
} }
function formatParameter(_, staticConstructorClass) { function formatParameter(param, staticConstructorClass) {
let content = ''; let content = '';
// Handle rest parameter pattern `...Type`. Parameter name needs to be // Handle rest parameter pattern `...Type`. Parameter name needs to be
// prefixed with `...` as in ES6. E.g. `...parameter: type[]`. // prefixed with `...` as in ES6. E.g. `...parameter: type[]`.
if (_.type.match(/^\.\.\.(.+)$/)) { if (param.type.match(/^\.\.\.(.+)$/)) {
content += '...'; content += '...';
} }
content += formatParameterName(_.name); content += formatParameterName(param.name);
// Optional parameters are formatted as: `parameter?: type`. // Optional parameters are formatted as: `parameter?: type`.
if (_.isOptional) { if (param.isOptional) {
content += '?'; content += '?';
} }
content += formatType(_.type, false, staticConstructorClass); content += formatType(param.type, false, staticConstructorClass);
return content; return content;
} }
@ -213,7 +213,7 @@ function formatComment(comment, descriptionTagName = 'desc', skipReturn = false)
let content = ''; let content = '';
// Retrieve description tag. // Retrieve description tag.
const descriptionTag = tags.find(_ => _.title === descriptionTagName); const descriptionTag = tags.find(it => it.title === descriptionTagName);
if (descriptionTag) { if (descriptionTag) {
// Don't display group titles. // Don't display group titles.
content += descriptionTag.desc.replace(/\{@grouptitle .+?\}/g, '').trim(); content += descriptionTag.desc.replace(/\{@grouptitle .+?\}/g, '').trim();
@ -225,10 +225,10 @@ function formatComment(comment, descriptionTagName = 'desc', skipReturn = false)
// provided in the signature... // provided in the signature...
content += formatCommentTags(tags, 'see'); content += formatCommentTags(tags, 'see');
content += formatCommentTags(tags, 'option'); content += formatCommentTags(tags, 'option');
content += formatCommentTags(tags, 'param', _ => _.name + ' - ' + _.desc); content += formatCommentTags(tags, 'param', it => it.name + ' - ' + it.desc);
if (!skipReturn) { if (!skipReturn) {
content += formatCommentTags(tags, 'return', _ => _.desc.trim().replace(/^\{|\}$/g, '').replace(/@([a-zA-Z]+)/, '$1')); content += formatCommentTags(tags, 'return', it => it.desc.trim().replace(/^\{|\}$/g, '').replace(/@([a-zA-Z]+)/, '$1'));
} }
// Make sure links are followable (e.g. by IDEs) by removing parameters. // Make sure links are followable (e.g. by IDEs) by removing parameters.
@ -242,13 +242,13 @@ function formatComment(comment, descriptionTagName = 'desc', skipReturn = false)
function formatCommentTags(tags, tagName, formatter) { function formatCommentTags(tags, tagName, formatter) {
let content = ''; let content = '';
// Default formatter simply outputs description. // Default formatter simply outputs description.
formatter = formatter || (_ => _.desc); formatter = formatter || (it => it.desc);
// Only keep tags that have a description. // Only keep tags that have a description.
tags = tags.filter(_ => _.desc && _.title === tagName); tags = tags.filter(it => it.desc && it.title === tagName);
if (tags.length > 0) { if (tags.length > 0) {
content += '\n'; content += '\n';
// Display tag as it was in original JSDoc, followed by formatted value. // Display tag as it was in original JSDoc, followed by formatted value.
tags.forEach(_ => content += '\n@' + tagName + ' ' + formatter(_)); tags.forEach(it => content += '\n@' + tagName + ' ' + formatter(it));
} }
return content; return content;
} }
@ -284,8 +284,8 @@ function formatJSDoc(offset, render) {
return '/** \n' + content + '\n' + indentation + ' */'; return '/** \n' + content + '\n' + indentation + ' */';
} }
function getMethodReturnType(_) { function getMethodReturnType(method) {
return _.returnType || _.returns.length > 0 && _.returns[0].type; return method.returnType || method.returns.length > 0 && method.returns[0].type;
} }
function sortMethods(methodA, methodB) { function sortMethods(methodA, methodB) {