Skip to content

Commit

Permalink
when creating links, do not treat <anonymous> types as type applica…
Browse files Browse the repository at this point in the history
…tions, and handle union types without parentheses (jsdoc#654)
  • Loading branch information
hegemonic committed Jun 16, 2014
1 parent eeb9c94 commit be60e81
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/jsdoc/util/templateHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ function hasUrlPrefix(text) {
return (/^(http|ftp)s?:\/\//).test(text);
}

function isComplexTypeExpression(expr) {
// record types, type unions, and type applications all count as "complex"
return expr.search(/[{(|]/) !== -1 || expr.search(/</) > 0;
}

/**
* Build an HTML link to the symbol with the specified longname. If the longname is not
* associated with a URL, this method simply returns the link text, if provided, or the longname.
Expand Down Expand Up @@ -185,7 +190,7 @@ function buildLink(longname, linkText, options) {
}
// handle complex type expressions that may require multiple links
// (but skip anything that looks like an inline tag)
else if (longname && longname.search(/[<{(]/) !== -1 && /\{\@.+\}/.test(longname) === false) {
else if (longname && isComplexTypeExpression(longname) && /\{\@.+\}/.test(longname) === false) {
parsedType = parseType(longname);
return stringifyType(parsedType, options.cssClass, options.linkMap);
}
Expand Down
16 changes: 16 additions & 0 deletions test/specs/jsdoc/util/templateHelper.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/*global afterEach, beforeEach, describe, expect, env, it, jasmine, spyOn */
'use strict';

var hasOwnProp = Object.prototype.hasOwnProperty;

describe("jsdoc/util/templateHelper", function() {
Expand Down Expand Up @@ -293,6 +295,20 @@ describe("jsdoc/util/templateHelper", function() {
'<a href="fakeclass.html">LinktoFakeClass</a>)>');
});

it('works correctly with type unions that are not enclosed in parentheses', function() {
var link = helper.linkto('linktoTest|LinktoFakeClass', 'link text');
expect(link).toBe('(<a href="test.html">linktoTest</a>|' +
'<a href="fakeclass.html">LinktoFakeClass</a>)');
});

it('does not try to parse a longname starting with <anonymous> as a type application',
function() {
spyOn(logger, 'error');

helper.linkto('<anonymous>~foo');
expect(logger.error).not.toHaveBeenCalled();
});

it('returns a link when a URL is specified', function() {
var link = helper.linkto('http://example.com');
expect(link).toBe('<a href="http://example.com">http://example.com</a>');
Expand Down

0 comments on commit be60e81

Please sign in to comment.