Skip to content

Commit 2ee3186

Browse files
committed
Apply fallback detection when an interpreter is ambiguous
1 parent 41707e1 commit 2ee3186

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
- Fixed file extensions with multiple delimiters not being prioritised over basic extensions.
55
- Fixed modeline checking not trimming comments from the first line.
66
- Fixed modeline checking being applied to the first line even when it does not contain a modeline.
7+
- Fixed fallback language detection not being applied when an ambiguous interpreter is declared in a shebang line.
78

89
## 2.5.1
910
*2022-06-26*

src/index.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ async function analyse(input?: string | string[], opts: T.Options = {}): Promise
3030

3131
// Setup main variables
3232
const fileAssociations: Record<T.FilePath, T.LanguageResult[]> = {};
33-
const definiteness: Record<T.FilePath, true | undefined> = {};
3433
const extensions: Record<T.FilePath, string> = {};
3534
const overrides: Record<T.FilePath, T.LanguageResult> = {};
3635
const results: T.Results = {
@@ -164,6 +163,8 @@ async function analyse(input?: string | string[], opts: T.Options = {}): Promise
164163
};
165164
const overridesArray = Object.entries(overrides);
166165
// List all languages that could be associated with a given file
166+
const definiteness: Record<T.FilePath, true | undefined> = {};
167+
const fromShebang: Record<T.FilePath, true | undefined> = {};
167168
for (const file of files) {
168169
let firstLine: string | null;
169170
if (useRawContent) {
@@ -197,11 +198,13 @@ async function analyse(input?: string | string[], opts: T.Options = {}): Promise
197198
matches.push(lang);
198199
}
199200
}
201+
// Add identified language(s)
200202
if (matches.length) {
201-
// Add explicitly-identified language
202-
const forcedLang = matches[0];
203-
addResult(file, forcedLang);
204-
definiteness[file] = true;
203+
for (const match of matches)
204+
addResult(file, match);
205+
if (matches.length === 1)
206+
definiteness[file] = true;
207+
fromShebang[file] = true;
205208
continue;
206209
}
207210
}
@@ -268,9 +271,8 @@ async function analyse(input?: string | string[], opts: T.Options = {}): Promise
268271
// Parse heuristics if applicable
269272
if (opts.checkHeuristics) for (const heuristics of heuristicsData.disambiguations) {
270273
// Make sure the extension matches the current file
271-
if (!heuristics.extensions.includes(extensions[file])) {
274+
if (!fromShebang[file] && !heuristics.extensions.includes(extensions[file]))
272275
continue;
273-
}
274276
// Load heuristic rules
275277
for (const heuristic of heuristics.rules) {
276278
// Make sure the language is not an array
@@ -281,7 +283,8 @@ async function analyse(input?: string | string[], opts: T.Options = {}): Promise
281283
const languageGroup = langData[heuristic.language]?.group;
282284
const matchesLang = fileAssociations[file].includes(heuristic.language);
283285
const matchesParent = languageGroup && fileAssociations[file].includes(languageGroup);
284-
if (!matchesLang && !matchesParent) continue;
286+
if (!matchesLang && !matchesParent)
287+
continue;
285288
// Normalise heuristic data
286289
const patterns: string[] = [];
287290
const normalise = (contents: string | string[]) => patterns.push(...[contents].flat());

test/unit.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ async function unitTest() {
6060
await test(['ecl.ecl', 'var:=val'], ['files', 'ECL']);
6161
await test(['frege.fr', 'import package'], ['files', 'Frege']);
6262
await test(['forth.fr', 'new-device 1'], ['files', 'Forth']);
63+
await test(['raku','#!/usr/bin/env perl6\n module'], ['files', 'Raku']);
6364
desc('vendored');
6465
await test(['gradlew'], ['files', undefined]);
6566
await test(['decl.d.ts'], ['files', undefined]);

0 commit comments

Comments
 (0)