From c7b226683964be3b8ed459a285fda2d4d5892fcf Mon Sep 17 00:00:00 2001 From: Samuel Bodin <1637651+bodinsamuel@users.noreply.github.com> Date: Tue, 24 Oct 2023 14:26:04 +0200 Subject: [PATCH] fix: expose language detection --- src/common/languages.ts | 31 +++++++++++++++++++++++++++++-- src/index.ts | 11 +++++++++++ src/payload/index.ts | 21 +++------------------ 3 files changed, 43 insertions(+), 20 deletions(-) diff --git a/src/common/languages.ts b/src/common/languages.ts index f73d8f22..36d4414c 100644 --- a/src/common/languages.ts +++ b/src/common/languages.ts @@ -1,4 +1,6 @@ -type ListItem = { +import path from 'node:path'; + +export type LangListItem = { extensions: string[]; group: string | null; name: string; @@ -6,7 +8,7 @@ type ListItem = { }; // Source: https://github.com/github/linguist/blob/5a0c74277548122267d84283910abd5e0b89380e/lib/linguist/languages.yml#L1528 -export const rawList: ListItem[] = [ +export const rawList: LangListItem[] = [ { extensions: ['.bsl', '.os'], group: null, @@ -4428,3 +4430,28 @@ export const rawList: ListItem[] = [ export const languages = rawList.filter((l) => l.type === 'programming'); export const others = rawList.filter((l) => l.type !== 'programming'); + +/** + * Detect language of a file at this level. + */ +export function detectLang(filename: string): LangListItem | null { + const ext = path.extname(filename); + + for (const lang of languages) { + if (!lang.extensions.includes(ext)) { + continue; + } + + return lang; + } + + for (const lang of others) { + if (!lang.extensions.includes(ext)) { + continue; + } + + return lang; + } + + return null; +} diff --git a/src/index.ts b/src/index.ts index 290966f6..b83e65f4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,10 @@ /* eslint-disable import/extensions */ import { analyser } from './analyser/index.js'; +import { + LangListItem, + detectLang, + rawList as languageList, +} from './common/languages.js'; import { listIndexed, listTech } from './common/techs.generated.js'; import { rawList, @@ -55,6 +60,7 @@ export { TechItem, TechMatcher, TechType, + LangListItem, }; export { @@ -82,3 +88,8 @@ export const tech = { list: listTech, keys: registeredTech, }; + +export const lang = { + detect: detectLang, + list: languageList, +}; diff --git a/src/payload/index.ts b/src/payload/index.ts index ad78b18b..a98c1d1f 100644 --- a/src/payload/index.ts +++ b/src/payload/index.ts @@ -1,6 +1,6 @@ import path from 'node:path'; -import { languages, others } from '../common/languages.js'; +import { detectLang } from '../common/languages.js'; import { nid } from '../common/nid.js'; import { rulesComponents, rulesTechs } from '../loader.js'; import type { BaseProvider } from '../provider/base.js'; @@ -231,24 +231,9 @@ export class Payload implements Analyser { * Detect language of a file at this level. */ detectLang(filename: string) { - const ext = path.extname(filename); - - for (const lang of languages) { - if (!lang.extensions.includes(ext)) { - continue; - } - - this.addLang(lang.group || lang.name); - return; - } - - for (const lang of others) { - if (!lang.extensions.includes(ext)) { - continue; - } - + const lang = detectLang(filename); + if (lang) { this.addLang(lang.group || lang.name); - return; } }