Skip to content

Commit

Permalink
Fix manual language classification applying
Browse files Browse the repository at this point in the history
  • Loading branch information
Nixinova committed Mar 1, 2024
1 parent 52e2f29 commit 6861683
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 25 deletions.
48 changes: 23 additions & 25 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,17 @@ async function analyse(paths?: string[], opts?: T.Options): Promise<T.Results>
async function analyse(input?: string | string[], opts: T.Options = {}): Promise<T.Results> {
const useRawContent = opts.fileContent !== undefined;
input = [input ?? []].flat();
opts.fileContent = [opts.fileContent ?? []].flat();

// Normalise input option arguments
opts = {
checkIgnored: !opts.quick,
checkAttributes: !opts.quick,
checkHeuristics: !opts.quick,
checkShebang: !opts.quick,
checkModeline: !opts.quick,
fileContent: [opts.fileContent ?? []].flat(),
...opts,
};

// Load data from github-linguist web repo
const langData = <S.LanguagesScema>await loadFile('languages.yml', opts.offline).then(yaml.load);
Expand Down Expand Up @@ -123,22 +133,13 @@ async function analyse(input?: string | string[], opts: T.Options = {}): Promise
}

// Filter out binary files
// TODO FIX doesnt work
if (!opts.keepBinary) {
const binaryIgnored = ignore();
binaryIgnored.add(getFlaggedGlobs('binary', true));
files = binaryIgnored.filter(files.map(relPath)).map(unRelPath);
}

// Apply aliases
opts = {
checkIgnored: !opts.quick,
checkAttributes: !opts.quick,
checkHeuristics: !opts.quick,
checkShebang: !opts.quick,
checkModeline: !opts.quick,
...opts,
};

// Ignore specific languages
for (const lang of opts.ignoredLanguages ?? []) {
for (const key in langData) {
Expand All @@ -149,8 +150,6 @@ async function analyse(input?: string | string[], opts: T.Options = {}): Promise
}
}

// TODO: FIX linguist-language=

// Establish language overrides taken from gitattributes
const forcedLangs = Object.entries(manualAttributes).filter(([, attrs]) => attrs.language);
for (const [path, attrs] of forcedLangs) {
Expand Down Expand Up @@ -182,11 +181,19 @@ async function analyse(input?: string | string[], opts: T.Options = {}): Promise
fileAssociations[file].push(finalResult);
extensions[file] = paths.extname(file).toLowerCase();
};
const overridesArray = Object.entries(overrides);

// List all languages that could be associated with a given file
const definiteness: Record<T.FilePath, true | undefined> = {};
const fromShebang: Record<T.FilePath, true | undefined> = {};
for (const file of files) {
// Check manual override
if (file in overrides) {
addResult(file, overrides[file]);
definiteness[file] = true;
continue;
}

// Check first line for readability
let firstLine: string | null;
if (useRawContent) {
firstLine = opts.fileContent?.[files.indexOf(file)]?.split('\n')[0] ?? null;
Expand All @@ -195,8 +202,10 @@ async function analyse(input?: string | string[], opts: T.Options = {}): Promise
firstLine = await readFile(file, true).catch(() => null);
}
else continue;

// Skip if file is unreadable
if (firstLine === null) continue;

// Check first line for explicit classification
const hasShebang = opts.checkShebang && /^#!/.test(firstLine);
const hasModeline = opts.checkModeline && /-\*-|(syntax|filetype|ft)\s*=/.test(firstLine);
Expand Down Expand Up @@ -229,17 +238,6 @@ async function analyse(input?: string | string[], opts: T.Options = {}): Promise
continue;
}
}
// Check override for manual language classification
if (!useRawContent && !opts.quick && opts.checkAttributes) {
const isOverridden = (path: string) => ignore().add(path).ignores(relPath(file));
const match = overridesArray.find(item => isOverridden(item[0]));
if (match) {
const forcedLang = match[1];
addResult(file, forcedLang);
definiteness[file] = true;
continue;
}
}
// Search each language
let skipExts = false;
// Check if filename is a match
Expand Down
1 change: 1 addition & 0 deletions test/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"~/al.al": "Perl",
"~/alternatives.asc": "AGS Script",
"~/file.txt": "JavaScript",
"~/folder/file.txt": "JavaScript",
"~/folder/sub.txt": "Text",
"~/hashbang": "JavaScript",
"~/modeline.txt": "C++",
Expand Down

0 comments on commit 6861683

Please sign in to comment.