diff --git a/lib/linguist/generated.rb b/lib/linguist/generated.rb index 7747406f..29b527b2 100644 --- a/lib/linguist/generated.rb +++ b/lib/linguist/generated.rb @@ -59,6 +59,7 @@ module Linguist godeps? || generated_by_zephir? || minified_files? || + has_source_map? || source_map? || compiled_coffeescript? || generated_parser? || @@ -104,6 +105,21 @@ module Linguist end end + # Internal: Does the blob contain a source map reference? + # + # We assume that if one of the last 2 lines starts with a source map + # reference, then the current file was generated from other files. + # + # We use the last 2 lines because the last line might be empty. + # + # We only handle JavaScript, no CSS support yet. + # + # Returns true or false. + def has_source_map? + return false unless extname.downcase == '.js' + lines.last(2).any? { |line| line.start_with?('//# sourceMappingURL') } + end + # Internal: Is the blob a generated source map? # # Source Maps usually have .css.map or .js.map extensions. In case they diff --git a/test/test_generated.rb b/test/test_generated.rb index 591f50ed..3a32e8fc 100644 --- a/test/test_generated.rb +++ b/test/test_generated.rb @@ -17,6 +17,7 @@ class TestGenerated < Minitest::Test assert_raises(DataLoadedError, "Data wasn't loaded when calling generated? on #{blob}") do Generated.generated?(blob, lambda { raise DataLoadedError.new }) end + assert Generated.generated?(blob, lambda { IO.read(blob) }), "#{blob} was not recognized as a generated file" end def generated_fixture_without_loading_data(name) @@ -62,6 +63,9 @@ class TestGenerated < Minitest::Test # Minified files generated_sample_loading_data("JavaScript/jquery-1.6.1.min.js") + # JS files with source map reference + generated_sample_loading_data("JavaScript/namespace.js") + # Source Map generated_fixture_without_loading_data("Data/bootstrap.css.map") generated_fixture_loading_data("Data/sourcemap.v3.map")