| 1 | import { |
| 2 | languageForAlias, |
| 3 | languageForPath, |
| 4 | languageType, |
| 5 | } from "./language-for-path.js"; |
| 6 | import { DOCUMENTATION, GENERATED |
| 7 | |
| 8 | /** The `.gitattributes` keys that override what counts, as GitHub's Linguist reads them. */ |
| 9 | export const LINGUIST_ATTRIBUTES = |
| 10 | "linguist-vendored", |
| 11 | "linguist-generated", |
| 12 | "linguist-documentation", |
| 13 | "linguist-detectable", |
| 14 | "linguist-language", |
| 15 | ] as const; |
| 16 | |
| 17 | /** Values as `git check-attr` prints them: "set", "unset", "unspecified" or the assigned string. */ |
| 18 | export type LinguistAttributes |
| 19 | Record<( |
| 20 | >; |
| 21 | |
| 22 | const anyOf = ( |
| 23 | new RegExp(patterns |
| 24 | |
| 25 | const IS_VENDORED = anyOf |
| 26 | const IS_DOCUMENTATION = anyOf |
| 27 | const IS_GENERATED = anyOf |
| 28 | |
| 29 | /** |
| 30 | * The language a file adds its bytes to in the repository's stats, or null when it adds none. |
| 31 | * |
| 32 | * Mirrors Linguist's `include_in_language_stats?`: vendored, documentation and generated files are left out, and of the rest only programming and markup languages count. Each rule can be overridden per path from `.gitattributes`. |
| 33 | */ |
| 34 | export function statsLanguage( |
| 35 | path: string, |
| 36 | attributes: LinguistAttributes |
| 37 | ): string | null |
| 38 | if (flag(attributes |
| 39 | return null; |
| 40 | if (flag(attributes |
| 41 | return null; |
| 42 | if (flag(attributes |
| 43 | return null; |
| 44 | |
| 45 | const named = |
| 46 | const language = |
| 47 | named === undefined || |
| 48 | ? languageForPath(path) |
| 49 | : languageForAlias(named) |
| 50 | if (!language) |
| 51 | |
| 52 | const detectable = |
| 53 | flag(attributes["linguist-detectable |
| 54 | ["programming", "markup" |
| 55 | return detectable ? |
| 56 | } |
| 57 | |
| 58 | /** Linguist's `boolean_attribute`: anything set counts as true except the string "false". */ |
| 59 | function flag(value: |
| 60 | if (value === |
| 61 | return value !== |
| 62 | } |