kratko.js icon indicating copy to clipboard operation
kratko.js copied to clipboard

Minified code analysis reports incorrect method length

Open bernhard-hofmann opened this issue 13 years ago • 4 comments

The method of determining the function length is fooled when running against minified code because functions are typically all on one line.

getLength: function() {
    return this.getLines().length;
}

It might resolve more than one issue if the JavaScript was beautified (see http://jsbeautifier.org/) prior to analysis. This would had the added benefit of showing more readable functions when the function is displayed in the preview.

A cheap and dirty re-liner like the following (sorry for not spending more time on it) would help make the line counting more accurate:

methodStr = methodStr.replace(/{/g,'{\n').replace(/}/g,'}\n').replace(/;/g,';\n').replace(/\n\n/g, '\n');

A far better option would be to allow (by choice maybe) the use of a service such as the Google Closure compiler API to compile with compilation_level=SIMPLE_OPTIMIZATIONS to remove comments, and specify formatting=pretty_print to format in a consistent way for line counting. If the call to compile the JS is done before methods are extracted, then only one call should be required.

bernhard-hofmann avatar Jun 16 '11 08:06 bernhard-hofmann

Interesting idea about using Goog's Closure API. I was thinking of adding actual parser (like http://esparser.qfox.nl/ by @qfox), which would completely solve the problems with inconsistent results.

I didn't worry about minified code because I refactor in development, not production, so code is properly formatted there. I think most of the people do the same. Is there a reason someone would need to refactor minified code? Maybe I'm missing something.

kangax avatar Jun 17 '11 17:06 kangax

It's a valid point that debug builds should not be minified, but as you pointed out the "elephant in the room" is the possible inconsistency of different styles and formatting. I've run kratko on other public sites to see how they measure up and that's where I found the line counting bothered me.

Also, I thought asking for cyclomatic complexity might be a step too far! ^^

bernhard-hofmann avatar Jun 17 '11 21:06 bernhard-hofmann

Checking cyclomatic complexity could be a fun project! Using that same @qfox's (or other) parser. Too bad I don't have time to play with that :)

I'll keep this ticket open to see what we can do about line count (and making it better) in the future.

kangax avatar Jun 17 '11 21:06 kangax

Guess we should talk.. ;)

pvdz avatar Jun 18 '11 06:06 pvdz