Added doclet parsing for object literal getters and setters (#100)

This commit is contained in:
Konstantin Pozin 2012-07-11 09:13:04 -04:00
parent b8a7d977b1
commit 9196f730de
3 changed files with 81 additions and 2 deletions

View File

@ -224,7 +224,8 @@ function aboutNode(node) {
about.type = 'undefined'; about.type = 'undefined';
} }
} }
else if (node.type === Token.ASSIGN || node.type === Token.COLON) { else if (node.type === Token.ASSIGN || node.type === Token.COLON ||
node.type === Token.GET || node.type === Token.SET) {
about.name = nodeToString(node.left); about.name = nodeToString(node.left);
if (node.type === Token.COLON) { if (node.type === Token.COLON) {
@ -235,7 +236,13 @@ function aboutNode(node) {
} }
about.node = node.right; about.node = node.right;
about.value = nodeToString(about.node); about.value = nodeToString(about.node);
about.type = getTypeName(node.right);
// Getter and setter functions should be treated as properties
if (node.type === Token.GET || node.type === Token.SET) {
about.type = nodeToString(node);
} else {
about.type = getTypeName(node.right);
}
if (about.type === 'FUNCTION' && about.node.name) { if (about.type === 'FUNCTION' && about.node.name) {
about.node.type = tkn.NAMEDFUNCTIONSTATEMENT; about.node.type = tkn.NAMEDFUNCTIONSTATEMENT;
@ -363,6 +370,18 @@ function visitNode(node) {
finishers: [currentParser.addDocletRef, currentParser.resolveEnum] finishers: [currentParser.addDocletRef, currentParser.resolveEnum]
}; };
} }
else if (node.type === Token.GET || node.type === Token.SET) { // assignment within an object literal
e = {
id: 'astnode'+node.hashCode(), // the id of the GET/SET node
comment: String(node.left.getJsDoc()||'@undocumented'),
lineno: node.left.getLineno(),
filename: currentSourceName,
astnode: node,
code: aboutNode(node),
event: "symbolFound",
finishers: [currentParser.addDocletRef]
};
}
else if (node.type == Token.VAR || node.type == Token.LET || node.type == Token.CONST) { else if (node.type == Token.VAR || node.type == Token.LET || node.type == Token.CONST) {
if (node.variables) { if (node.variables) {

37
test/fixtures/getset.js vendored Normal file
View File

@ -0,0 +1,37 @@
/** @class */
var Person = makeClass(
/** @lends Person# */
{
/** Set up initial values. */
initialize: function(name) {
},
/** Speak a message. */
say: function(message) {
return this.name + " says: " + message;
},
/**
* The name of the person.
* @type {string}
*/
get name() {
return this._name;
},
/**
* @type {string}
* @param val
*/
set name(val) {
this._name = name;
},
/**
* @type {number}
*/
get age() {
return 25;
}
}
);

View File

@ -0,0 +1,23 @@
describe("When a getter or setter is the child of an object literal", function () {
var docSet = jasmine.getDocSetFromFile("test/fixtures/getset.js"),
foundName = docSet.getByLongname("Person#name"),
foundAge = docSet.getByLongname("Person#age");
it("should have a doclet with the correct longname", function () {
expect(foundName.length).toEqual(2);
expect(foundAge.length).toEqual(1);
});
it("should have a doclet with the correct name", function () {
expect(foundName[0].name).toEqual("name");
expect(foundName[1].name).toEqual("name");
expect(foundAge[0].name).toEqual("age");
});
it("should have the correct memberof", function () {
expect(foundName[0].memberof).toEqual("Person");
expect(foundName[1].memberof).toEqual("Person");
expect(foundAge[0].memberof).toEqual("Person");
});
});