Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
types.push('*');
break;
case TYPES.FunctionType:
types.push('function');
break;
case TYPES.NameExpression:
types.push(parsedType.name);
break;
case TYPES.NullLiteral:
types.push('null');
break;
case TYPES.RecordType:
types.push('Object');
break;
case TYPES.TypeApplication:
types.push( catharsis.stringify(parsedType) );
break;
case TYPES.TypeUnion:
parsedType.elements.forEach(function(element) {
types = types.concat( getTypeStrings(element) );
});
break;
case TYPES.UndefinedLiteral:
types.push('undefined');
break;
case TYPES.UnknownLiteral:
types.push('?');
break;
default:
// this shouldn't happen
throw new Error( util.format('unrecognized type %s in parsed type: %j', parsedType.type,
parsedType) );
case TYPES.RecordType:
types.push('Object');
break;
case TYPES.TypeApplication:
// if this is the outermost type, we strip the modifiers; otherwise, we keep them
if (isOutermostType) {
applications = parsedType.applications.map(function(application) {
return catharsis.stringify(application);
}).join(', ');
typeString = util.format( '%s.<%s>', getTypeStrings(parsedType.expression),
applications );
types.push(typeString);
}
else {
types.push( catharsis.stringify(parsedType) );
}
break;
case TYPES.TypeUnion:
parsedType.elements.forEach(function(element) {
types = types.concat( getTypeStrings(element) );
});
break;
case TYPES.UndefinedLiteral:
types.push('undefined');
break;
case TYPES.UnknownLiteral:
types.push('?');
break;
default:
// this shouldn't happen
throw new Error( util.format('unrecognized type %s in parsed type: %j', parsedType.type,
function parseTypeExpression(tagInfo) {
var errorMessage;
var parsedType;
// don't try to parse empty type expressions
if (!tagInfo.typeExpression) {
return tagInfo;
}
try {
parsedType = catharsis.parse(tagInfo.typeExpression, {jsdoc: true});
}
catch (e) {
// always re-throw so the caller has a chance to report which file was bad
throw new Error( util.format('Invalid type expression "%s": %s', tagInfo.typeExpression,
e.message) );
}
tagInfo.type = tagInfo.type.concat( getTypeStrings(parsedType, true) );
tagInfo.parsedType = parsedType;
// Catharsis and JSDoc use the same names for 'optional' and 'nullable'...
['optional', 'nullable'].forEach(function(key) {
if (parsedType[key] !== null && parsedType[key] !== undefined) {
tagInfo[key] = parsedType[key];
}
});
function parseTypeExpression(tagInfo) {
let parsedType;
// don't try to parse empty type expressions
if (!tagInfo.typeExpression) {
return tagInfo;
}
try {
parsedType = catharsis.parse(tagInfo.typeExpression, {jsdoc: true});
}
catch (e) {
// always re-throw so the caller has a chance to report which file was bad
throw new Error(`Invalid type expression "${tagInfo.typeExpression}": ${e.message}`);
}
tagInfo.type = tagInfo.type.concat( getTypeStrings(parsedType, true) );
tagInfo.parsedType = parsedType;
// Catharsis and JSDoc use the same names for 'optional' and 'nullable'...
['optional', 'nullable'].forEach(key => {
if (parsedType[key] !== null && parsedType[key] !== undefined) {
tagInfo[key] = parsedType[key];
}
});
function TypeName (typeStr) {
var type;
var typeName = typeStr;
try {
if ('Function' == typeName) {
typeName = '{' + typeName + '}';
}
type = catharsis.parse(typeName);
if (type.type === catharsis.Types.TypeApplication && (type.expression.name === 'Array' || type.expression.name === 'Object')) {
typeName = type.applications[0].name;
} else if (type.type === catharsis.Types.FunctionType) {
typeName = 'Function';
type = null;
} else {
typeName = typeStr;
type = null;
}
} catch (e) {
// log.error('getTypeName: Parse of "%s" failed with reason: %s', typeStr, e.message);
typeName = typeStr;
}
var doc = aliasMap.getDocs(typeName);
var res = '';
if (doc.length === 1) {
doc = doc[0];
function TypeLink (typeStr) {
var type;
var typeName = typeStr;
try {
if ('Function' == typeName) {
typeName = '{' + typeName + '}';
}
type = catharsis.parse(typeName);
if (type.type === catharsis.Types.TypeApplication && (type.expression.name === 'Array' || type.expression.name === 'Object')) {
typeName = type.applications[0].name;
} else if (type.type === catharsis.Types.FunctionType) {
typeName = 'Function';
type = null;
} else {
typeName = typeStr;
type = null;
}
} catch (e) {
// log.error('getTypeName: Parse of "%s" failed with reason: %s', typeStr, e.message);
typeName = typeStr;
}
var doc = aliasMap.getDocs(typeName);
var res = '';
let description;
if (typeof format !== 'string') {
format = 'simple';
} else if (!['extended', 'simple'].includes(format)) {
throw new Exception('The {{describeType}} helper accepts the options "simple" ' +
'and "extended".');
} else if (format === 'extended' && typeof property !== 'string') {
property = 'description';
}
if (typeof parsedType === 'object') {
description = catharsis.describe(parsedType, catharsisOptions);
} else {
// We don't know the type
description = catharsis.describe(catharsis.parse('?'), catharsisOptions);
}
if (format === 'extended') {
return new SafeString(_.get(description.extended, property));
} else {
return new SafeString(description.simple);
}
},
Helpers.prototype.describeType = function describeType(parsedType, options) {
var catharsisOptions = {
// TODO: add codeTag and/or codeClass based on config
// TODO: use the correct locale
links: templateHelper.longnameToUrl
};
var description;
options = _.defaults(options || {}, {
format: 'simple'
});
description = catharsis.describe(parsedType, catharsisOptions);
switch(options.format) {
case 'extended':
return description.extended;
default: {
return description.simple;
}
}
};
describeType(parsedType, format, property) {
const catharsisOptions = getCatharsisOptions(template);
let description;
if (typeof format !== 'string') {
format = 'simple';
} else if (!['extended', 'simple'].includes(format)) {
throw new Exception('The {{describeType}} helper accepts the options "simple" ' +
'and "extended".');
} else if (format === 'extended' && typeof property !== 'string') {
property = 'description';
}
if (typeof parsedType === 'object') {
description = catharsis.describe(parsedType, catharsisOptions);
} else {
// We don't know the type
description = catharsis.describe(catharsis.parse('?'), catharsisOptions);
}
if (format === 'extended') {
return new SafeString(_.get(description.extended, property));
} else {
return new SafeString(description.simple);
}
},
describe('describeType', () => {
const catharsis = require('catharsis');
const parsedType = catharsis.parse('!string');
it('should use "unknown type" if no type is provided', () => {
const description = instance.describeType(undefined);
expect(description).toBeInstanceOf(SafeString);
expect(description.toString()).toBe('unknown');
});
it('should throw if the requested format is not available', () => {
function shouldThrow() {
return instance.describeType(parsedType, 'marshmallow');
}
expect(shouldThrow).toThrow();
});