support the typedef tag correctly for Closure Compiler (#605)

This commit is contained in:
Jeff Williams 2017-07-21 15:23:04 -07:00
parent e33f48c471
commit 56b52779c4
3 changed files with 91 additions and 30 deletions

View File

@ -1003,7 +1003,13 @@ exports.closureTags = {
type: cloneTagDef(baseTags.type, {
mustNotHaveDescription: false
}),
typedef: cloneTagDef(baseTags.typedef),
typedef: {
canHaveType: true,
onTagged: function(doclet, tag) {
setDocletKindToTitle(doclet, tag);
setDocletTypeToValueType(doclet, tag);
}
},
// Closure Compiler only
unrestricted: {
onTagged: ignore

2
test/fixtures/typedeftag2.js vendored Normal file
View File

@ -0,0 +1,2 @@
/** @typedef {(string|number)} */
calc.NumberLike;

View File

@ -1,41 +1,94 @@
'use strict';
describe('@typedef tag', function() {
var docSet = jasmine.getDocSetFromFile('test/fixtures/typedeftag.js');
var numberlike = docSet.getByLongname('calc.NumberLike')[0];
var operator = docSet.getByLongname('calc.Operator')[0];
var result = docSet.getByLongname('calc.Result')[0];
var calculatorBattery = docSet.getByLongname('CalculatorBattery')[0];
it('When a comment has a @typedef tag, the doclet has a kind property set to "typedef".', function() {
expect(numberlike.kind).toBe('typedef');
afterEach(function() {
jasmine.restoreTagDictionary();
});
it('When a comment has a @typedef tag with a type, the doclet has a type property set to that type.', function() {
expect(typeof numberlike.type).toBe('object');
expect(typeof numberlike.type.names).toBe('object');
expect(numberlike.type.names.length).toBe(2);
expect(numberlike.type.names[0]).toBe('string');
expect(numberlike.type.names[1]).toBe('number');
describe('JSDoc tags', function() {
beforeEach(function() {
jasmine.replaceTagDictionary('jsdoc');
});
it('When a comment has a @typedef tag, the doclet has a kind property set to "typedef".', function() {
var docSet = jasmine.getDocSetFromFile('test/fixtures/typedeftag.js');
var numberlike = docSet.getByLongname('calc.NumberLike')[0];
expect(numberlike.kind).toBe('typedef');
});
it('When a comment has a @typedef tag with a type, the doclet has a type property set to that type.', function() {
var docSet = jasmine.getDocSetFromFile('test/fixtures/typedeftag.js');
var numberlike = docSet.getByLongname('calc.NumberLike')[0];
expect(typeof numberlike.type).toBe('object');
expect(Array.isArray(numberlike.type.names)).toBe(true);
expect(numberlike.type.names.length).toBe(2);
expect(numberlike.type.names[0]).toBe('string');
expect(numberlike.type.names[1]).toBe('number');
});
it('When a comment has a @typedef tag with a name, the doclet has a name property set to that name.', function() {
var docSet = jasmine.getDocSetFromFile('test/fixtures/typedeftag.js');
var numberlike = docSet.getByLongname('calc.NumberLike')[0];
expect(numberlike.name).toBe('NumberLike');
expect(numberlike.longname).toBe('calc.NumberLike');
});
it('When a symbol has a @typedef tag without a name, the doclet has a name property set to the symbol name.', function() {
var docSet = jasmine.getDocSetFromFile('test/fixtures/typedeftag.js');
var operator = docSet.getByLongname('calc.Operator')[0];
expect(operator.name).toBe('Operator');
expect(operator.longname).toBe('calc.Operator');
});
it('When a symbol has a @typedef tag with a name, the name in the tag takes precedence over the symbol name.', function() {
var docSet = jasmine.getDocSetFromFile('test/fixtures/typedeftag.js');
var result = docSet.getByLongname('calc.Result')[0];
expect(result.name).toBe('Result');
expect(result.longname).toBe('calc.Result');
});
it('When a symbol has a @typedef tag with a name and no scope, the scope defaults to `global`.', function() {
var docSet = jasmine.getDocSetFromFile('test/fixtures/typedeftag.js');
var calculatorBattery = docSet.getByLongname('CalculatorBattery')[0];
expect(calculatorBattery).toBeDefined();
expect(calculatorBattery.scope).toBe('global');
});
});
it('When a comment has a @typedef tag with a name, the doclet has a name property set to that name.', function() {
expect(numberlike.name).toBe('NumberLike');
expect(numberlike.longname).toBe('calc.NumberLike');
});
describe('Closure Compiler tags', function() {
beforeEach(function() {
jasmine.replaceTagDictionary('closure');
});
it('When a symbol has a @typedef tag without a name, the doclet has a name property set to the symbol name.', function() {
expect(operator.name).toBe('Operator');
expect(operator.longname).toBe('calc.Operator');
});
it('When a comment has a @typedef tag, the doclet has a kind property set to "typedef".', function() {
var docSet = jasmine.getDocSetFromFile('test/fixtures/typedeftag2.js');
var numberlike = docSet.getByLongname('calc.NumberLike')[0];
it('When a symbol has a @typedef tag with a name, the name in the tag takes precedence over the symbol name.', function() {
expect(result.name).toBe('Result');
expect(result.longname).toBe('calc.Result');
});
expect(numberlike.kind).toBe('typedef');
});
it('When a symbol has a @typedef tag with a name and no scope, the scope defaults to `global`.', function() {
expect(calculatorBattery).toBeDefined();
expect(calculatorBattery.scope).toBe('global');
it('When a comment has a @typedef tag with a type, the doclet has a type property set to that type.', function() {
var docSet = jasmine.getDocSetFromFile('test/fixtures/typedeftag2.js');
var numberlike = docSet.getByLongname('calc.NumberLike')[0];
expect(typeof numberlike.type).toBe('object');
expect(Array.isArray(numberlike.type.names)).toBe(true);
expect(numberlike.type.names.length).toBe(2);
expect(numberlike.type.names[0]).toBe('string');
expect(numberlike.type.names[1]).toBe('number');
});
it('When a symbol has a @typedef tag, the doclet has a name property set to the symbol name.', function() {
var docSet = jasmine.getDocSetFromFile('test/fixtures/typedeftag2.js');
var numberlike = docSet.getByLongname('calc.NumberLike')[0];
expect(numberlike.name).toBe('NumberLike');
});
});
});