Skip to content

Commit

Permalink
Add unsure output object
Browse files Browse the repository at this point in the history
Contains files that are classified using the fallback language and lists what languages it could be.
  • Loading branch information
Nixinova committed Jan 10, 2023
1 parent f5d59a8 commit 31a44cd
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 3 deletions.
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ Running LinguistJS on this folder will return the following JSON:
"/src/cli.js": "JavaScript",
"/readme.md": "Markdown",
"/no-lang": null,
}
},
"unsure": {},
},
"languages": {
"count": 3,
Expand Down
9 changes: 7 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ async function analyse(input?: string | string[], opts: T.Options = {}): Promise
const extensions: Record<T.FilePath, string> = {};
const overrides: Record<T.FilePath, T.LanguageResult> = {};
const results: T.Results = {
files: { count: 0, bytes: 0, results: {} },
files: { count: 0, bytes: 0, results: {}, unsure: {} },
languages: { count: 0, bytes: 0, results: {} },
unknown: { count: 0, bytes: 0, extensions: {}, filenames: {} },
};
Expand Down Expand Up @@ -306,7 +306,12 @@ async function analyse(input?: string | string[], opts: T.Options = {}): Promise
}
}
// If no heuristics, assign a language
results.files.results[file] ??= fileAssociations[file][0];
if (!results.files.results[file]) {
const possibleLangs = fileAssociations[file];
results.files.results[file] = possibleLangs[0];
if (possibleLangs.length > 1)
results.files.unsure[file] = possibleLangs;
}
}

// Skip specified categories
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface Results {
bytes: Bytes
/** Note: Results use slashes as delimiters even on Windows. */
results: Record<FilePath, LanguageResult>
unsure: Record<FilePath, LanguageResult[]>
}
languages: {
count: Integer
Expand Down
2 changes: 2 additions & 0 deletions test/unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ async function test([filename, fileContent = ''], [type, testVal]) {
'files': actual.files.results[filename],
'size': actual.files.bytes,
'count': actual.files.count,
'unsure_count': Object.entries(actual.files.unsure).length,
}[type];
const result = testContent === testVal;
i = `${+i + 1}`.padStart(2, '0');
Expand All @@ -36,6 +37,7 @@ async function unitTest() {
await test(['x.cpp'], ['files', 'C++']);
await test(['x.c'], ['files', 'C']);
await test(['x.R'], ['files', 'R']);
await test(['.m'], ['unsure_count', 1])
desc('filenames');
await test(['Dockerfile'], ['files', 'Dockerfile']);
await test(['CMakeLists.txt'], ['files', 'CMake']);
Expand Down

0 comments on commit 31a44cd

Please sign in to comment.