describe.js 13.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
'use strict';

var _ = require('underscore-contrib');
var fs = require('fs');
var path = require('path');
var stringify = require('./stringify');
var Types = require('./types');
var util = require('util');

var DEFAULT_OPTIONS = {
	language: 'en',
	resources: {
		en: JSON.parse(fs.readFileSync(path.join(__dirname, '../res/en.json'), 'utf8'))
	}
};

// order matters for these!
var FUNCTION_DETAILS = ['new', 'this'];
var FUNCTION_DETAILS_VARIABLES = ['functionNew', 'functionThis'];
var MODIFIERS = ['optional', 'nullable', 'repeatable'];

var TEMPLATE_VARIABLES = [
	'application',
	'codeTagClose',
	'codeTagOpen',
	'element',
	'field',
	'functionNew',
	'functionParams',
	'functionReturns',
	'functionThis',
	'keyApplication',
	'name',
	'nullable',
	'optional',
	'param',
	'prefix',
	'repeatable',
	'suffix',
	'type'
];

var FORMATS = {
	EXTENDED: 'extended',
	SIMPLE: 'simple'
};

function makeTagOpen(codeTag, codeClass) {
	var tagOpen = '';
	var tags = codeTag ? codeTag.split(' ') : [];

	tags.forEach(function(tag) {
		var tagClass = codeClass ? util.format(' class="%s"', codeClass) : '';
		tagOpen += util.format('<%s%s>', tag, tagClass);
	});

	return tagOpen;
}

function makeTagClose(codeTag) {
	var tagClose = '';
	var tags = codeTag ? codeTag.split(' ') : [];

	tags.reverse();
	tags.forEach(function(tag) {
		tagClose += util.format('</%s>', tag);
	});

	return tagClose;
}

function Result() {
	this.description = '';
	this.modifiers = {
		functionNew: '',
		functionThis: '',
		optional: '',
		nullable: '',
		repeatable: ''
	};
	this.returns = '';
}

function Context(props) {
	var self = this;

	props = props || {};

	TEMPLATE_VARIABLES.forEach(function(variable) {
		self[variable] = props[variable] || '';
	});
}

function Describer(opts) {
	var options;

	this._useLongFormat = true;
	options = this._options = _.defaults(opts || {}, DEFAULT_OPTIONS);
	this._stringifyOptions = _.defaults(options, {_ignoreModifiers: true});

	// use a dictionary, not a Context object, so we can more easily merge this into Context objects
	this._i18nContext = {
		codeTagClose: makeTagClose(options.codeTag),
		codeTagOpen: makeTagOpen(options.codeTag, options.codeClass)
	};

	// templates start out as strings; we lazily replace them with template functions
	this._templates = options.resources[options.language];
	if (!this._templates) {
		throw new Error('I18N resources are not available for the language ' + options.language);
	}
}

function modifierKind(useLongFormat) {
	return useLongFormat ? FORMATS.EXTENDED : FORMATS.SIMPLE;
}

function buildModifierStrings(describer, modifiers, type, useLongFormat) {
	var result = {};

	modifiers.forEach(function(modifier) {
		var key = modifierKind(useLongFormat);
		var modifierStrings = describer[modifier](type[modifier]);

		result[modifier] = modifierStrings[key];
	});

	return result;
}

function addModifiers(describer, context, result, type, useLongFormat) {
	var keyPrefix = 'modifiers.' + modifierKind(useLongFormat);
	var modifiers = buildModifierStrings(describer, MODIFIERS, type, useLongFormat);

	MODIFIERS.forEach(function(modifier) {
		var modifierText = modifiers[modifier] || '';

		result.modifiers[modifier] = modifierText;
		if (!useLongFormat) {
			context[modifier] = modifierText;
		}
	});

	context.prefix = describer._translate(keyPrefix + '.prefix', context);
	context.suffix = describer._translate(keyPrefix + '.suffix', context);
}

function addFunctionModifiers(describer, context, result, type, useLongFormat) {
	var functionDetails = buildModifierStrings(describer, FUNCTION_DETAILS, type, useLongFormat);
	var kind = modifierKind(useLongFormat);
	var strings = [];

	FUNCTION_DETAILS.forEach(function(functionDetail, i) {
		var functionExtraInfo = functionDetails[functionDetail] || '';
		var functionDetailsVariable = FUNCTION_DETAILS_VARIABLES[i];

		result.modifiers[functionDetailsVariable] = functionExtraInfo;
		if (!useLongFormat) {
			context[functionDetailsVariable] += functionExtraInfo;
		}
	});
}

// Replace 2+ whitespace characters with a single whitespace character.
function collapseSpaces(string) {
	return string.replace(/(\s)+/g, '$1');
}

Describer.prototype._stringify = function(type, typeString, useLongFormat) {
	var context = new Context({
		type: typeString || stringify(type, this._stringifyOptions)
	});
	var result = new Result();

	addModifiers(this, context, result, type, useLongFormat);
	result.description = this._translate('type', context).trim();

	return result;
};

Describer.prototype._translate = function(key, context) {
	var result;
	var templateFunction = _.getPath(this._templates, key);

	context = context || new Context();

	if (templateFunction === undefined) {
		throw new Error(util.format('The template %s does not exist for the language %s', key,
			this._options.language));
	}

	// compile and cache the template function if necessary
	if (typeof templateFunction === 'string') {
		// force the templates to use the `context` object
		templateFunction = templateFunction.replace(/\<\%\= /g, '<%= context.');
		templateFunction = _.template(templateFunction, null, {variable: 'context'});
		_.setPath(this._templates, templateFunction, key);
	}

	result = (templateFunction(_.extend(context, this._i18nContext)) || '')
		// strip leading spaces
		.replace(/^\s+/, '');
	result = collapseSpaces(result);

	return result;
};

Describer.prototype._modifierHelper = function(key, modifierPrefix, context) {
	modifierPrefix = modifierPrefix || '';

	return {
		extended: key ?
			this._translate(util.format('%s.%s.%s', modifierPrefix, FORMATS.EXTENDED, key),
				context) :
			'',
		simple: key ?
			this._translate(util.format('%s.%s.%s', modifierPrefix, FORMATS.SIMPLE, key), context) :
			''
	};
};

Describer.prototype._translateModifier = function(key, context) {
	return this._modifierHelper(key, 'modifiers', context);
};

Describer.prototype._translateFunctionModifier = function(key, context) {
	return this._modifierHelper(key, 'function', context);
};

function getApplicationKey(type, applications) {
	if (applications.length === 1) {
		if (/[Aa]rray/.test(type.expression.name)) {
			return 'array';
		} else {
			return 'other';
		}
	} else if (/[Ss]tring/.test(applications[0].name)) {
		// object with string keys
		return 'object';
	} else {
		// object with non-string keys
		return 'objectNonString';
	}
}

Describer.prototype.application = function(type, useLongFormat) {
	var applications = type.applications.slice(0);
	var context = new Context();
	var key = 'application.' + getApplicationKey(type, applications);
	var result = new Result();
	var self = this;

	addModifiers(this, context, result, type, useLongFormat);

	context.type = this.type(type.expression).description;
	context.application = this.type(applications.pop()).description;
	context.keyApplication = applications.length ? this.type(applications.pop()).description : '';

	result.description = this._translate(key, context).trim();

	return result;
};

function reduceMultiple(context, keyName, contextName, translate, previous, current, index, items) {
	var key =
		index === 0 ? '.first.many' :
		index === (items.length - 1) ? '.last.many' :
		'.middle.many';

	key = keyName + key;
	context[contextName] = items[index];

	return previous + translate(key, context);
}

Describer.prototype.elements = function(type, useLongFormat) {
	var context = new Context();
	var items = type.elements.slice(0);
	var result = new Result();

	addModifiers(this, context, result, type, useLongFormat);
	result.description = this._combineMultiple(items, context, 'union', 'element', useLongFormat);

	return result;
};

Describer.prototype.new = function(funcNew) {
	var context = new Context({'functionNew': this.type(funcNew).description});
	var key = funcNew ? 'new' : '';

	return this._translateFunctionModifier(key, context);
};

Describer.prototype.nullable = function(nullable) {
	var key = nullable === true ? 'nullable' :
		nullable === false ? 'nonNullable' :
		'';

	return this._translateModifier(key);
};

Describer.prototype.optional = function(optional) {
	var key = (optional === true) ? 'optional' : '';

	return this._translateModifier(key);
};

Describer.prototype.repeatable = function(repeatable) {
	var key = (repeatable === true) ? 'repeatable' : '';

	return this._translateModifier(key);
};

Describer.prototype._combineMultiple = function(items, context, keyName, contextName,
	useLongFormat) {
	var result = new Result();
	var self = this;
	var strings;

	strings = typeof items[0] === 'string' ?
		items.slice(0) :
		items.map(function(item) {
			return self.type(item).description;
		});

	switch(strings.length) {
		case 0:
			// falls through
		case 1:
			context[contextName] = strings[0] || '';
			result.description = this._translate(keyName + '.first.one', context);
			break;
		case 2:
			strings.forEach(function(item, idx) {
				var key = keyName + (idx === 0 ? '.first' : '.last' ) + '.two';

				context[contextName] = item;
				result.description += self._translate(key, context);
			});
			break;
		default:
			result.description = strings.reduce(reduceMultiple.bind(null, context, keyName,
				contextName, this._translate.bind(this)), '');
	}

	return result.description.trim();
};

Describer.prototype.params = function(params, functionContext) {
	var context = new Context();
	var result = new Result();
	var self = this;
	var strings;

	// TODO: this hardcodes the order and placement of functionNew and functionThis; need to move
	// this to the template (and also track whether to put a comma after the last modifier)
	functionContext = functionContext || {};
	params = params || [];
	strings = params.map(function(param) {
		return self.type(param).description;
	});

	if (functionContext.functionThis) {
		strings.unshift(functionContext.functionThis);
	}
	if (functionContext.functionNew) {
		strings.unshift(functionContext.functionNew);
	}
	result.description = this._combineMultiple(strings, context, 'params', 'param', false);

	return result;
};

Describer.prototype.this = function(funcThis) {
	var context = new Context({'functionThis': this.type(funcThis).description});
	var key = funcThis ? 'this' : '';

	return this._translateFunctionModifier(key, context);
};

Describer.prototype.type = function(type, useLongFormat) {
	var result = new Result();

	if (useLongFormat === undefined) {
		useLongFormat = this._useLongFormat;
	}
	// ensure we don't use the long format for inner types
	this._useLongFormat = false;

	if (!type) {
		return result;
	}

	switch(type.type) {
		case Types.AllLiteral:
			result = this._stringify(type, this._translate('all'), useLongFormat);
			break;
		case Types.FunctionType:
			result = this._signature(type, useLongFormat);
			break;
		case Types.NameExpression:
			result = this._stringify(type, null, useLongFormat);
			break;
		case Types.NullLiteral:
			result = this._stringify(type, this._translate('null'), useLongFormat);
			break;
		case Types.RecordType:
			result = this._record(type, useLongFormat);
			break;
		case Types.TypeApplication:
			result = this.application(type, useLongFormat);
			break;
		case Types.TypeUnion:
			result = this.elements(type, useLongFormat);
			break;
		case Types.UndefinedLiteral:
			result = this._stringify(type, this._translate('undefined'), useLongFormat);
			break;
		case Types.UnknownLiteral:
			result = this._stringify(type, this._translate('unknown'), useLongFormat);
			break;
		default:
			throw new Error('Unknown type: ' + JSON.stringify(type));
	}

	return result;
};

Describer.prototype._record = function(type, useLongFormat) {
	var context = new Context();
	var items;
	var result = new Result();

	items = this._recordFields(type.fields);

	addModifiers(this, context, result, type, useLongFormat);
	result.description = this._combineMultiple(items, context, 'record', 'field', useLongFormat);

	return result;
};

Describer.prototype._recordFields = function(fields) {
	var context = new Context();
	var result = [];
	var self = this;

	if (!fields.length) {
		return result;
	}

	result = fields.map(function(field) {
		var key = 'field.' + (field.value ? 'typed' : 'untyped');

		context.name = self.type(field.key).description;
		if (field.value) {
			context.type = self.type(field.value).description;
		}

		return self._translate(key, context);
	});

	return result;
};

Describer.prototype._addLinks = function(nameString) {
	var linkClass = '';
	var options = this._options;
	var result = nameString;


	if (options.links && Object.prototype.hasOwnProperty.call(options.links, nameString)) {
		if (options.linkClass) {
			linkClass = util.format(' class="%s"', options.linkClass);
		}

		nameString = util.format('<a href="%s"%s>%s</a>', options.links[nameString], linkClass,
			nameString);
	}

	return nameString;
};

Describer.prototype.result = function(type, useLongFormat) {
	var context = new Context();
	var description;
	var key = 'function.' + modifierKind(useLongFormat) + '.returns';
	var result = new Result();

	context.type = this.type(type).description;

	addModifiers(this, context, result, type, useLongFormat);
	result.description = this._translate(key, context);

	return result;
};

Describer.prototype._signature = function(type, useLongFormat) {
	var context = new Context();
	var functionModifiers;
	var kind = modifierKind(useLongFormat);
	var result = new Result();
	var returns;
	var self = this;

	addModifiers(this, context, result, type, useLongFormat);
	addFunctionModifiers(this, context, result, type, useLongFormat);

	context.functionParams = this.params(type.params || [], context).description;

	if (type.result) {
		returns = this.result(type.result, useLongFormat);
		if (useLongFormat) {
			result.returns = returns.description;
		} else {
			context.functionReturns = returns.description;
		}
	}

	result.description += this._translate('function.' + kind + '.signature', context).trim();

	return result;
};

module.exports = function(type, options) {
	var simple = new Describer(options).type(type, false);
	var extended = new Describer(options).type(type);

	[simple, extended].forEach(function(result) {
		result.description = collapseSpaces(result.description.trim());
	});

	return {
		simple: simple.description,
		extended: extended
	};
};