CoffeeScript JS guessing

This commit is contained in:
Joshua Peek
2011-05-25 11:25:11 -05:00
parent 3ea66af178
commit 972632b9db
10 changed files with 188 additions and 0 deletions

View File

@@ -150,6 +150,8 @@ module Linguist
# Generated source code is supressed in diffs and is ignored by
# langauge statistics.
#
# Requires Blob#data
#
# Includes:
# - XCode project XML files
# - Minified JavaScript
@@ -158,6 +160,8 @@ module Linguist
def generated?
if ['.xib', '.nib', '.pbxproj'].include?(pathname.extname)
true
elsif generated_coffeescript?
true
elsif pathname.extname == '.js'
# JS is minified if any lines are longer than 1000c
lines.any? { |l| l.length > 1000 }
@@ -166,6 +170,42 @@ module Linguist
end
end
# Internal: Is the blob JS generated by CoffeeScript?
#
# Requires Blob#data
#
# CoffeScript is meant to output JS that would be difficult to
# tell if it was generated or not. Look for a number of patterns
# outputed by the CS compiler.
#
# Return true or false
def generated_coffeescript?
return unless pathname.extname == '.js'
if lines[0] == '(function() {' && # First line is module closure opening
lines[-2] == '}).call(this);' && # Second to last line closes module closure
lines[-1] == '' # Last line is blank
score = 0
lines.each do |line|
if line =~ /var /
# Underscored temp vars are likely to be Coffee
score += 1 * line.gsub(/(_fn|_i|_len|_ref|_results)/).count
# bind and extend functions are very Coffee specific
score += 3 * line.gsub(/(__bind|__extends|__hasProp|__indexOf|__slice)/).count
end
end
# Require a score of 3. This is fairly arbitrary. Consider
# tweaking later.
score >= 3
else
false
end
end
# Public: Should the blob be indexed for searching?
#
# Excluded: