marcuslucero / jsdoc-toolkit

Automatically exported from code.google.com/p/jsdoc-toolkit
0 stars 0 forks source link

properties assigned using square bracket notation are not handled by jsdoc #322

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Run JsDoc over the code below

/**
 * @constructor
 */
function MyConstructor()
{
    /**
     * do stuff
     * @param foo An argument
     */
    this.privilegedMethod1 = function(foo){

    };

    /**
     * do more stuff
     * @param foo An argument
     */
    this["privilegedMethod2"] = function(foo){

    };

What is the expected output? What do you see instead?
EXPECT: JsDoc documents both privilegedMethod1 and privilegedMethod2
ACTUAL: JsDoc documents privilegedMethod1 but not privilegedMethod2

What version of the product are you using? On what operating system?
JsDoc Toolkit version 2.4.0
Ubuntu 11.04

Please provide any additional information below.

If someone raised this issue I would be thinking to myself "why are you writing 
code that way anyhow?". This occurs in our codebase  where we use properties, 
so actually what we write is like this:

this["${org.foo.bar.writestate.method}"]

but that is rewritten by ANT to something sensible when we  build our project.

I ran the debug jar and made some tweaks to the code to fix this as I need it. 
It fixes square bracket notation when used with string literals, not variables. 
All the tests still pass.

I will be using this patched version locally. I am including the changes here 
in case you would like to incorporate into the codebase.

My test file is included as an attachment.

Changed this function in TextStream.js
/**
 * @param {number} n The index of the character to "look" at, offset from
 * the cursor position (defaults to 0);
 * @param {number} len The number of characters to "look" at (defaults to 1).
 */
JSDOC.TextStream.prototype.look = function(n, len) {
    if (typeof n == "undefined") n = 0;
    if(isNaN(len)) len = 1;
    var result = new String(""),
        ptr = this.cursor+n,
        limit = ptr + len;
    do {
        if (ptr < 0 || ptr >= this.text.length) {
            result.eof = true;
            break;
        }
        result += this.text.charAt(ptr);
    }
    while(++ptr < limit)
    return result;
}

Changed this function in TokenReader.js
/**
    @returns {Boolean} Was the token found?
 */
JSDOC.TokenReader.prototype.read_word = function(/**JSDOC.TokenStream*/stream, 
tokens) {
    var openLiteralInSquareBracket = /\[["|']/,
        closeLiteralInSquareBracket = /["|']\]/,
        found = "";
    while (!stream.look().eof) {
        if(JSDOC.Lang.isWordChar(stream.look())){
            found += stream.next();
        }
        else if(openLiteralInSquareBracket.test(stream.look(0, 2)) && found.length){
            stream.next(2);//throw these away
            found += ".";//put in a dot instead
        }
        else if(closeLiteralInSquareBracket.test(stream.look(0, 2)) && found.length){
            stream.next(2);//throw these away
        }
        else{
            break;
        }
    }

    if (found === "") {
        return false;
    }
    else {
        var name;
        if ((name = JSDOC.Lang.keyword(found))) tokens.push(new JSDOC.Token(found, "KEYW", name));
        else tokens.push(new JSDOC.Token(found, "NAME", "NAME"));
        return true;
    }
}

Original issue reported on code.google.com by ricksbro...@gmail.com on 4 Jun 2011 at 3:18

Attachments: