Add detection for Hack files with ".php" file extension

Based on top of PR#1447. Adds a simple heuristic check for Hack files vs PHP files (`<?hh` vs other `<?`).

Tested by verifying that the Hack example site was detected as 100% Hack and that Laravel was detected as 100% PHP. (Without the heuristic, Laravel gets detected as about 50% Hack, just by randomness in the classifier since PHP and Hack are very hard to distinguish unless you actually parse the file and look for specific language features.)
This commit is contained in:
Josh Watzman
2014-08-06 16:30:21 -07:00
parent b2cb74cabf
commit 9c044c5bd0
3 changed files with 43 additions and 0 deletions

View File

@@ -25,6 +25,9 @@ module Linguist
if languages.all? { |l| ["Common Lisp", "OpenCL"].include?(l) }
result = disambiguate_cl(data, languages)
end
if languages.all? { |l| ["Hack", "PHP"].include?(l) }
result = disambiguate_hack(data, languages)
end
return result
end
end
@@ -88,6 +91,13 @@ module Linguist
matches
end
def self.disambiguate_hack(data, languages)
matches = []
matches << Language["Hack"] if data.include?("<?hh")
matches << Language["PHP"] if /<?[^h]/.match(data)
matches
end
def self.active?
!!ACTIVE
end

View File

@@ -986,6 +986,7 @@ Hack:
ace_mode: php
extensions:
- .hh
- .php
Haml:
group: HTML

32
samples/Hack/funs.php Normal file
View File

@@ -0,0 +1,32 @@
<?hh
/**
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
/**
* This file contains a bunch of php stubs for functions that have been added
* to hhvm (though aren't in a release yet). These are important because the
* Hack typechecker can understand them
*/
class InvariantViolationException extends Exception {}
function invariant(mixed $test, string $message): void {
if (!$test) {
invariant_violation($message);
}
}
function invariant_violation(string $message): void {
throw new InvariantViolationException($message);
}
function class_meth(string $class, string $method) {
return array($class, $method);
}