Skip to content

Commit

Permalink
Merge pull request #1 from edoardopirovano/resolve-clash
Browse files Browse the repository at this point in the history
Handle multiple languages for an extension
  • Loading branch information
aeisenberg authored Jun 23, 2021
2 parents 86d4428 + 600ba0e commit a49cee0
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 4 deletions.
26 changes: 25 additions & 1 deletion 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 Down Expand Up @@ -39,7 +50,20 @@ export class Languages {
const languageMode = languageMap[language];
const languageExtensions = (languageMode && languageMode.extensions) || [];
languageExtensions.forEach((extension: string) => {
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[lowerCaseExtension]);
if (currentLanguagePriority === -1) {
extensions[lowerCaseExtension] = lowerCaseLanguage;
} else {
const otherPriority = importantLanguages.indexOf(lowerCaseLanguage);
if (otherPriority !== -1 && otherPriority < currentLanguagePriority)
extensions[lowerCaseExtension] = lowerCaseLanguage;
}
}
});
});

Expand Down
16 changes: 16 additions & 0 deletions test/data2/x.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;

// A comment
/*
* A multi-line comment
*/
namespace HelloWorldApp {
class Geeks {
static void Main(string[] args) {
if (true) {
Console.WriteLine("Hello World!");
}
}
}
}

13 changes: 10 additions & 3 deletions test/directory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,24 @@ describe('Directory', () => {
cwd: __dirname + '/data2'
}, {
files: [
'data2/x.cs',
'data2/x.html',
'data2/x.js',
'data2/x.py',
'data2/x.rb',
],
info: {
code: 23,
comment: 23,
total: 61,
code: 33,
comment: 27,
total: 78,
},
languages: {
"c#": {
code: 10,
comment: 4,
sum: 1,
total: 17,
},
html: {
code: 4,
comment: 4,
Expand Down
13 changes: 13 additions & 0 deletions test/file.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,19 @@ describe('File', () => {
});
});

it('should calculate info for a csharp file', async () => {
await doTest('x.cs', {
languages: 'c#',
lines: {
code: 10,
comment: 4,
total: 17
},
name: 'x.cs',
size: 253
});
});

async function doTest(fileName: string, expectedFileInfo: FileInfo) {
const fullPath = slash(path.join(__dirname, `/data2/${fileName}`));
const actualFileInfo = await new LocFile(fullPath).getFileInfo();
Expand Down

0 comments on commit a49cee0

Please sign in to comment.