kangax / kratko.js

Simple tool to help refactor Javascript

Home Page:http://perfectionkills.com/refactoring-javascript-with-kratko-js

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Minified code analysis reports incorrect method length

bernhard-hofmann opened this issue · 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.

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.

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! ^^

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.

Guess we should talk.. ;)