From 37ffdb9020cf6a12aefd8ffbdf6ec4e8c72f7622 Mon Sep 17 00:00:00 2001 From: Paul Chaignon Date: Tue, 2 Dec 2014 16:41:39 -0500 Subject: [PATCH 1/5] Fix error when matching languages against heuristics: if no language, no heuristic rule should be used --- lib/linguist/heuristics.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/linguist/heuristics.rb b/lib/linguist/heuristics.rb index 13033579..dd1585f8 100644 --- a/lib/linguist/heuristics.rb +++ b/lib/linguist/heuristics.rb @@ -53,7 +53,7 @@ module Linguist # Internal: Check if this heuristic matches the candidate languages. def matches?(candidates) - candidates.all? { |l| @languages.include?(l.name) } + candidates.any? && candidates.all? { |l| @languages.include?(l.name) } end # Internal: Perform the heuristic From 4ddd8d9d2bd29f80c9e9e1928324ad955ee783a4 Mon Sep 17 00:00:00 2001 From: Paul Chaignon Date: Tue, 2 Dec 2014 20:06:12 -0500 Subject: [PATCH 2/5] Unit test for fix #1809 on heuristics --- test/test_heuristics.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/test_heuristics.rb b/test/test_heuristics.rb index 0bd01bb5..92047e1c 100644 --- a/test/test_heuristics.rb +++ b/test/test_heuristics.rb @@ -20,6 +20,11 @@ class TestHeuristcs < Test::Unit::TestCase Dir.glob("#{samples_path}/#{language_name}/#{file}") end + def test_no_language + results = Heuristics.call(file_blob("C++/render_adapter.cpp"), []) + assert_equal [], results + end + # Candidate languages = ["C++", "Objective-C"] def test_obj_c_by_heuristics # Only calling out '.h' filenames as these are the ones causing issues From e5bc2845cd43b35abb5c7cc31990453005c5e477 Mon Sep 17 00:00:00 2001 From: Paul Chaignon Date: Tue, 2 Dec 2014 20:26:15 -0500 Subject: [PATCH 3/5] Fix for fixture tests: fixture files were not tested at all --- test/test_blob.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/test_blob.rb b/test/test_blob.rb index 4fe6b152..b59fbec7 100644 --- a/test/test_blob.rb +++ b/test/test_blob.rb @@ -469,16 +469,16 @@ class TestBlob < Test::Unit::TestCase # Test language detection for files which shouldn't be used as samples root = File.expand_path('../fixtures', __FILE__) Dir.entries(root).each do |language| - next unless File.file?(language) + next if language == '.' || language == '..' # Each directory contains test files of a language dirname = File.join(root, language) Dir.entries(dirname).each do |filename| - next unless File.file?(filename) - # By default blob search the file in the samples; # thus, we need to give it the absolute path filepath = File.join(dirname, filename) + next unless File.file?(filepath) + blob = blob(filepath) assert blob.language, "No language for #{filepath}" assert_equal language, blob.language.name, blob.name From 7dd318ca76ff960cbed31df1f34515c347b40b75 Mon Sep 17 00:00:00 2001 From: Paul Chaignon Date: Tue, 2 Dec 2014 20:36:18 -0500 Subject: [PATCH 4/5] Use namespace.js for the heuristic test with no match --- samples/JavaScript/namespace.js | 93 +++++++++++++++++++++++++++++++++ test/test_heuristics.rb | 5 +- 2 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 samples/JavaScript/namespace.js diff --git a/samples/JavaScript/namespace.js b/samples/JavaScript/namespace.js new file mode 100644 index 00000000..26d9acbd --- /dev/null +++ b/samples/JavaScript/namespace.js @@ -0,0 +1,93 @@ +(function(root, factory) { + if (typeof define === 'function' && define.amd) { + define(['lodash'], factory); + } else if (typeof exports !== 'undefined') { + module.exports = factory(require('lodash')); + } else { + root.Namespace = factory(root._); + } +})(this, function(_) { + 'use strict'; + + /** + * @module namespace + * @class namespace + */ + function Namespace() {} + + /** + * Regex for splitting keypaths into arrays. + * + * @private + * @const {RegExp} + * @type + */ + var KEYPATH_SPLITTER = /\./g; + + /** + * An internal cache to avoid calculating a keypath more than once. + * + * @private + * @type {Object} + */ + var _keypaths = {}; + + _.extend(Namespace.prototype, { + + /** + * Adds a definition to the namespace object. + * + * @public + * @instance + * @method add + * @param {String} keypath - The keypath for the definition to be added at. + * @param {Function|Object} definition - The definition to be added. + * @return {Function|Object} - The definition. + */ + add: function(keypath, definition) { + return this._walk(keypath, function(memo, name, index, keypath) { + if (index + 1 === keypath.length) { + memo[name] = _.extend(definition, memo[name]); + } + return memo[name] || (memo[name] = {}); + }); + }, + + /** + * Retrieves a definition from the namespace safely. + * + * @public + * @instance + * @method get + * @param {String} keypath - The keypath to lookup a definition for. + * @returns {Function|Object|undefined} - The definition if it exists, otherwise `undefined`. + */ + get: function(keypath) { + return this._walk(keypath); + }, + + /** + * An internal function for walking a keypath. + * + * @private + * @instance + * @method _walk + * @param {String} keypath - The keypath to walk through. + * @param {Function} [callback] - An optional callback to be called at each item in the path. + * @returns {function|Object|undefined} - The reduced keypath. + */ + _walk: function(keypath, callback) { + return _.reduce( + _keypaths[keypath] || (_keypaths[keypath] = keypath.split(KEYPATH_SPLITTER)), + callback || function(memo, name) { + return memo && memo[name]; + }, + this + ); + } + }); + + return Namespace; +}); + +//# sourceMappingURL=namespace.js.map \ No newline at end of file diff --git a/test/test_heuristics.rb b/test/test_heuristics.rb index 92047e1c..f6de6b13 100644 --- a/test/test_heuristics.rb +++ b/test/test_heuristics.rb @@ -20,8 +20,9 @@ class TestHeuristcs < Test::Unit::TestCase Dir.glob("#{samples_path}/#{language_name}/#{file}") end - def test_no_language - results = Heuristics.call(file_blob("C++/render_adapter.cpp"), []) + def test_no_match + language = [] + results = Heuristics.call(file_blob("JavaScript/namespace.js"), language) assert_equal [], results end From 7625c92307fe287eff3f321588f80f3e5c547bf4 Mon Sep 17 00:00:00 2001 From: Paul Chaignon Date: Tue, 2 Dec 2014 20:37:09 -0500 Subject: [PATCH 5/5] Remove .module extension for PHP --- lib/linguist/languages.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/linguist/languages.yml b/lib/linguist/languages.yml index 71a1474c..6dd8f1e9 100644 --- a/lib/linguist/languages.yml +++ b/lib/linguist/languages.yml @@ -1889,7 +1889,6 @@ PHP: - .aw - .ctp - .fcgi - - .module - .php3 - .php4 - .php5