Thursday, July 28, 2011

Wheatley: Semi-Automated JavaScript Refactoring

Incomplete, and I hope to refactor it (ironically enough), but kind of awesome.

it "can do an ultra-simple refactor, namely creating a wrapper function" do
@code = <<-CODE
console.log("foo");
CODE
@refactored = <<-REFACTORED
function asdf(qwerty) {
console.log(qwerty);
}
asdf("foo");
REFACTORED
Wheatley.create_wrapper_function(@code).should == @refactored
end

Extensive documentation on GitHub.

Features

  • Wrap function definitions in wrapper functions
  • Identify similar JavaScript code blocks
  • Calculate similarity percentage between any two code blocks
  • Compare code blocks to see how many specific tokens they differ by
  • Extract the variant tokens by which similar code blocks differ
  • Extract a literal from a simple function
  • Create function calls
  • Create wrapper functions
  • Perform simplistic proof-of-concept refactoring

Named after the dumb AI from Portal 2.