mirror of
				https://github.com/KevinMidboe/linguist.git
				synced 2025-10-29 17:50:22 +00:00 
			
		
		
		
	JS is minified if its average line length is greater than 100c
Fixes #27
This commit is contained in:
		| @@ -151,6 +151,17 @@ module Linguist | ||||
|       lines.grep(/\S/).size | ||||
|     end | ||||
|  | ||||
|     # Internal: Compute average line length. | ||||
|     # | ||||
|     # Returns Integer. | ||||
|     def average_line_length | ||||
|       if lines.any? | ||||
|         lines.inject(0) { |n, l| n += l.length } / lines.length | ||||
|       else | ||||
|         0 | ||||
|       end | ||||
|     end | ||||
|  | ||||
|     # Public: Is the blob a generated file? | ||||
|     # | ||||
|     # Generated source code is supressed in diffs and is ignored by | ||||
| @@ -166,16 +177,24 @@ module Linguist | ||||
|     def generated? | ||||
|       if ['.xib', '.nib', '.pbxproj'].include?(extname) | ||||
|         true | ||||
|       elsif generated_coffeescript? | ||||
|       elsif generated_coffeescript? || minified_javascript? | ||||
|         true | ||||
|       elsif extname == '.js' | ||||
|         # JS is minified if any lines are longer than 500c | ||||
|         lines.any? { |l| l.length > 500 } | ||||
|       else | ||||
|         false | ||||
|       end | ||||
|     end | ||||
|  | ||||
|     # Internal: Is the blob minified JS? | ||||
|     # | ||||
|     # Consider JS minified if the average line length is | ||||
|     # greater then 100c. | ||||
|     # | ||||
|     # Returns true or false. | ||||
|     def minified_javascript? | ||||
|       return unless extname == '.js' | ||||
|       average_line_length > 100 | ||||
|     end | ||||
|  | ||||
|     # Internal: Is the blob JS generated by CoffeeScript? | ||||
|     # | ||||
|     # Requires Blob#data | ||||
|   | ||||
							
								
								
									
										1152
									
								
								test/fixtures/json2_backbone.js
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										1152
									
								
								test/fixtures/json2_backbone.js
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										1320
									
								
								test/fixtures/uglify.js
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										1320
									
								
								test/fixtures/uglify.js
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							| @@ -123,6 +123,12 @@ class TestBlob < Test::Unit::TestCase | ||||
|     assert blob("MainMenu.nib").generated? | ||||
|     assert blob("project.pbxproj").generated? | ||||
|  | ||||
|     # Long line | ||||
|     assert !blob("uglify.js").generated? | ||||
|  | ||||
|     # Inlined JS, but mostly code | ||||
|     assert !blob("json2_backbone.js").generated? | ||||
|  | ||||
|     # Minified JS | ||||
|     assert !blob("jquery-1.6.1.js").generated? | ||||
|     assert blob("jquery-1.6.1.min.js").generated? | ||||
|   | ||||
| @@ -19,7 +19,7 @@ class TestRepository < Test::Unit::TestCase | ||||
|   end | ||||
|  | ||||
|   def test_linguist_language | ||||
|     assert_equal Language['Ruby'], linguist_repo.language | ||||
|     assert_equal Language['JavaScript'], linguist_repo.language | ||||
|   end | ||||
|  | ||||
|   def test_linguist_languages | ||||
|   | ||||
		Reference in New Issue
	
	Block a user