Skip to content

Commit

Permalink
Address PR comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
edoardopirovano committed Jun 23, 2021
1 parent 32d6f98 commit 600ba0e
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions src/languages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ interface ExtensionsTypes {

export interface DetectorOptions {}

/**
* The extension map can contain multiple languages with the same extension,
* but we only want a single one. For the moment, these clashes are resolved
* by the simple heuristic below listing high-priority languages. We may want
* to consider smarter heuristics to correctly identify languages in cases
* where the extension is ambiguous. The ordering of the list matters and
* languages earlier on will get a higher priority when resolving clashes.
*/
const importantLanguages = ["javascript", "typescript", "ruby", "python", "java", "c", "c++", "c#", "rust", "scala", "perl", "go"];


/**
* detecte program language through file extension
*
Expand All @@ -35,30 +46,22 @@ export class Languages {
private loadExtensionMap = () => {
const extensions: ExtensionsTypes = {};

/**
* The extension map can contain multiple languages with the same extension,
* but we only want a single one. For the moment, these clashes are resolved
* by the simple heuristic below listing high-priority languages. We may want
* to consider smarter heuristics to correctly identify languages in cases
* where the extension is ambiguous. The ordering of the list matters and
* languages earlier on will get a higher priority when resolving clashes.
*/
const importantLanguages = ["javascript", "typescript", "ruby", "python", "java", "c", "c++", "c#", "rust", "scala", "perl", "go"];

Object.keys(languageMap).forEach((language) => {
const languageMode = languageMap[language];
const languageExtensions = (languageMode && languageMode.extensions) || [];
languageExtensions.forEach((extension: string) => {
if (!extensions[extension.toLowerCase()]) {
extensions[extension.toLowerCase()] = language.toLowerCase();
const lowerCaseExtension = extension.toLowerCase();
const lowerCaseLanguage = language.toLowerCase()
if (!extensions[lowerCaseExtension]) {
extensions[lowerCaseExtension] = lowerCaseLanguage;
} else {
const currentLanguagePriority = importantLanguages.indexOf(extensions[extension.toLowerCase()]);
if (currentLanguagePriority == -1) {
extensions[extension.toLowerCase()] = language.toLowerCase();
const currentLanguagePriority = importantLanguages.indexOf(extensions[lowerCaseExtension]);
if (currentLanguagePriority === -1) {
extensions[lowerCaseExtension] = lowerCaseLanguage;
} else {
const otherPriority = importantLanguages.indexOf(language.toLowerCase());
if (otherPriority != -1 && otherPriority < currentLanguagePriority)
extensions[extension.toLowerCase()] = language.toLowerCase();
const otherPriority = importantLanguages.indexOf(lowerCaseLanguage);
if (otherPriority !== -1 && otherPriority < currentLanguagePriority)
extensions[lowerCaseExtension] = lowerCaseLanguage;
}
}
});
Expand Down

0 comments on commit 600ba0e

Please sign in to comment.