Skip to content

Commit

Permalink
fix(type): union in generics
Browse files Browse the repository at this point in the history
  • Loading branch information
h13i32maru committed Dec 30, 2016
1 parent 09452b6 commit eb051e7
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 5 deletions.
8 changes: 7 additions & 1 deletion src/Parser/ParamParser.js
Expand Up @@ -102,7 +102,13 @@ export default class ParamParser {
typeText = typeText.replace(/^[(]/, '').replace(/[)]$/, '');
result.types = typeText.split('|');
} else if (typeText.includes('|')) {
result.types = typeText.split('|');
if (typeText.match(/<.*?\|.*?>/)) {
// union in generics. e.g. `Array<string|number>`
// hack: in this case, process this type in DocBuilder#_buildTypeDocLinkHTML
result.types = [typeText];
} else {
result.types = typeText.split('|');
}
} else {
result.types = [typeText];
}
Expand Down
6 changes: 4 additions & 2 deletions src/Publisher/Builder/DocBuilder.js
Expand Up @@ -777,8 +777,10 @@ export default class DocBuilder {
.replace(/<.*?>/g, (a)=> a.replace(/,/g, '\\Z'))
.replace(/{.*?}/g, (a)=> a.replace(/,/g, '\\Z').replace(/:/g, '\\Y'));
const innerTypes = inner.split(',').map((v) => {
v = v.trim().replace(/\\Z/g, ',').replace(/\\Y/g, ':');
return this._buildTypeDocLinkHTML(v);
return v.split('|').map((vv) => {
vv = vv.trim().replace(/\\Z/g, ',').replace(/\\Y/g, ':');
return this._buildTypeDocLinkHTML(vv);
}).join('|');
});

const html = `${this._buildDocLinkHTML(mainType, mainType)}<${innerTypes.join(', ')}>`;
Expand Down
7 changes: 7 additions & 0 deletions test/fixture/package/src/Type/Complex.js
Expand Up @@ -27,4 +27,11 @@ export default class TestTypeComplex {
* @param {!(number|string)} p2 - this is p2.
*/
method4(p1, p2){}

// union in generics
/**
* this is method5.
* @param {Promise<string|number, Error>} p1 - this is p1.
*/
method5(p1){}
}
4 changes: 2 additions & 2 deletions test/src/HTMLTest/CoverageTest/CoverageTest.js
Expand Up @@ -9,7 +9,7 @@ describe('test coverage', ()=> {
it('has coverage summary', ()=> {
assert(badge.includes('79%'));
assert.includes(doc, '[data-ice="coverageBadge"]', './badge.svg', 'src');
assert.includes(doc, '[data-ice="totalCoverageCount"]', '277/347');
assert.includes(doc, '[data-ice="totalCoverageCount"]', '278/348');
assert.equal(doc.find('[data-ice="file"] [data-ice="coverage"]').length, 119);
});

Expand Down Expand Up @@ -120,7 +120,7 @@ describe('test coverage', ()=> {
test('file/src/TrailingComma/Definition.js.html', '100 %3/3');
test('file/src/Type/Array.js.html', '100 %2/2');
test('file/src/Type/Class.js.html#errorLines=1,9', '33 %1/3');
test('file/src/Type/Complex.js.html#errorLines=1', '80 %4/5');
test('file/src/Type/Complex.js.html#errorLines=1', '83 %5/6');
test('file/src/Type/Default.js.html', '100 %2/2');
test('file/src/Type/External.js.html', '100 %2/2');
test('file/src/Type/Function.js.html', '100 %2/2');
Expand Down
12 changes: 12 additions & 0 deletions test/src/HTMLTest/DocumentTest/TypeTest/ComplexTest.js
Expand Up @@ -57,4 +57,16 @@ describe('TestTypeComplex', ()=> {
], 'href');
});
});

it('has complex union type in generics.', ()=>{
findParent(doc, '[data-ice="summary"] [href$="#instance-method-method5"]', '[data-ice="target"]', (doc)=> {
assert.includes(doc, null, 'method5(p1: Promise<string|number, Error>)');
assert.multiIncludes(doc, '[data-ice="signature"] a', [
'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise',
'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String',
'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number',
'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error'
], 'href');
});
});
});

0 comments on commit eb051e7

Please sign in to comment.