Skip to content

Commit b2287cc

Browse files
committed
Fix multi-delimiter extensions not taking priority
Extensions like `.antlers.php` were being parsed with equal weight to a basic `.php` extension.
1 parent 97b4925 commit b2287cc

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
## Next
4+
- Fixed file extensions with multiple delimiters not being prioritised over basic extensions.
5+
36
## 2.5.1
47
*2022-06-26*
58
- Fixed heuristics not being applied to files.

src/index.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -215,21 +215,31 @@ async function analyse(input?: string | string[], opts: T.Options = {}): Promise
215215
}
216216
// Search each language
217217
let skipExts = false;
218+
// Check if filename is a match
218219
for (const lang in langData) {
219-
// Check if filename is a match
220220
const matchesName = langData[lang].filenames?.some(name => paths.basename(file.toLowerCase()) === name.toLowerCase());
221221
if (matchesName) {
222222
addResult(file, lang);
223223
skipExts = true;
224224
}
225225
}
226+
// Check if extension is a match
227+
const possibleExts: { ext: string, lang: T.Language }[] = [];
226228
if (!skipExts) for (const lang in langData) {
227-
// Check if extension is a match
228-
const matchesExt = langData[lang].extensions?.some(ext => file.toLowerCase().endsWith(ext.toLowerCase()));
229-
if (matchesExt) {
230-
addResult(file, lang);
229+
const extMatches = langData[lang].extensions?.filter(ext => file.toLowerCase().endsWith(ext.toLowerCase()));
230+
if (extMatches?.length) {
231+
for (const ext of extMatches)
232+
possibleExts.push({ ext, lang });
231233
}
232234
}
235+
// Apply more specific extension if available
236+
const isComplexExt = (ext: string) => /\..+\./.test(ext);
237+
const hasComplexExt = possibleExts.some(data => isComplexExt(data.ext));
238+
for (const { ext, lang } of possibleExts) {
239+
if (hasComplexExt && !isComplexExt(ext)) continue;
240+
if (!hasComplexExt && isComplexExt(ext)) continue;
241+
addResult(file, lang);
242+
}
233243
// Fallback to null if no language matches
234244
if (!fileAssociations[file]) {
235245
addResult(file, null);

test/unit.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ async function unitTest() {
4040
await test(['Dockerfile'], ['files', 'Dockerfile']);
4141
await test(['CMakeLists.txt'], ['files', 'CMake']);
4242
await test(['tsconfig.json'], ['files', 'JSON with Comments']);
43+
await test(['index.tsx'], ['files', 'TSX'])
44+
await test(['file.antlers.php'], ['files', 'Antlers'])
45+
await test(['file.other.php', '<?php?>'], ['files', 'PHP'])
4346
desc('shebangs');
4447
await test(['node_js', '#!/usr/bin/env node'], ['files', 'JavaScript']);
4548
await test(['rake_ruby', '#!/usr/bin/env rake'], ['files', 'Ruby']);

0 commit comments

Comments
 (0)