From 9196f730de2afab8b4a31b6eec329bfcc1b31c7a Mon Sep 17 00:00:00 2001 From: Konstantin Pozin Date: Wed, 11 Jul 2012 09:13:04 -0400 Subject: [PATCH] Added doclet parsing for object literal getters and setters (#100) --- rhino_modules/jsdoc/src/parser.js | 23 +++++++++++++++++-- test/fixtures/getset.js | 37 ++++++++++++++++++++++++++++++ test/specs/documentation/getset.js | 23 +++++++++++++++++++ 3 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/getset.js create mode 100644 test/specs/documentation/getset.js diff --git a/rhino_modules/jsdoc/src/parser.js b/rhino_modules/jsdoc/src/parser.js index 49a019bf..503d64fd 100644 --- a/rhino_modules/jsdoc/src/parser.js +++ b/rhino_modules/jsdoc/src/parser.js @@ -224,7 +224,8 @@ function aboutNode(node) { 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); if (node.type === Token.COLON) { @@ -235,7 +236,13 @@ function aboutNode(node) { } about.node = node.right; 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) { about.node.type = tkn.NAMEDFUNCTIONSTATEMENT; @@ -363,6 +370,18 @@ function visitNode(node) { 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) { if (node.variables) { diff --git a/test/fixtures/getset.js b/test/fixtures/getset.js new file mode 100644 index 00000000..34a362f3 --- /dev/null +++ b/test/fixtures/getset.js @@ -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; + } + } +); \ No newline at end of file diff --git a/test/specs/documentation/getset.js b/test/specs/documentation/getset.js new file mode 100644 index 00000000..d88ebe6f --- /dev/null +++ b/test/specs/documentation/getset.js @@ -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"); + }); + +});