UNPKG

cspell

Version:

A Spelling Checker for Code!

83 lines 2.89 kB
import * as path from 'node:path'; import { unknownWordsChoices } from '@cspell/cspell-types'; import { calcExcludeGlobInfo } from '../util/glob.js'; const defaultContextRange = 20; export class LintRequest { fileGlobs; options; reporter; locale; configFile; excludes; root; showContext; enableGlobDot; fileLists; files; cspellSettingsFromCliOptions; constructor(fileGlobs, options, reporter) { this.fileGlobs = fileGlobs; this.options = options; this.reporter = reporter; this.root = path.resolve(options.root || process.cwd()); this.configFile = options.config; this.excludes = calcExcludeGlobInfo(this.root, options.exclude); this.locale = options.locale ?? options.local ?? ''; this.enableGlobDot = options.dot; this.showContext = Math.max(options.showContext === true ? defaultContextRange : options.showContext ? options.showContext : 0, 0); this.fileLists = (options.fileList ?? options.fileLists) || []; this.files = mergeFiles(options.file, options.files); const noConfigSearch = options.configSearch === false ? true : options.configSearch === true ? false : undefined; const dictionaries = [ ...(options.disableDictionary ?? []).map((d) => `!${d}`), // first disable dictionaries ...(options.dictionary ?? []).map((d) => `!!${d}`), // Use `!!` to ensure dictionaries are enabled ]; const languageSettings = [ // Use `*` to match all languages and locales { languageId: '*', locale: '*', dictionaries }, ]; this.cspellSettingsFromCliOptions = { ...(noConfigSearch !== undefined ? { noConfigSearch } : {}), ...extractUnknownWordsConfig(options), languageSettings, }; } } function mergeFiles(a, b) { const files = merge(a, b); if (!files) return undefined; return [...new Set(files.flatMap((a) => a.split('\n').map((a) => a.trim())).filter((a) => !!a))]; } function merge(a, b) { if (!a) return b; if (!b) return a; return [...a, ...b]; } export function extractUnknownWordsConfig(options) { const config = {}; if (!options.report) return config; switch (options.report) { case 'all': { config.unknownWords = unknownWordsChoices.ReportAll; break; } case 'simple': { config.unknownWords = unknownWordsChoices.ReportSimple; break; } case 'typos': { config.unknownWords = unknownWordsChoices.ReportCommonTypos; break; } case 'flagged': { config.unknownWords = unknownWordsChoices.ReportFlagged; break; } } return config; } //# sourceMappingURL=LintRequest.js.map