@blast-shield/parser
Version:
Parser utility for blast-shield
78 lines (73 loc) • 2.88 kB
TypeScript
/**
* Represents an extracted comment with both raw and cleaned versions
*/
interface ExtractedComment {
raw: string;
clean: string;
}
/**
* Options for file parsing and file finding utilities.
* - rootDir: The root directory to search from (defaults to process.cwd())
* - additionalIgnores: Extra ignore patterns beyond .gitignore
*/
interface ParserOptions {
rootDir?: string;
additionalIgnores?: string[];
}
declare const FILE_EXTENSION_TO_LANGUAGE: Record<string, string>;
/**
* Extracts all comments from code for a given language.
* @param code The code to extract comments from
* @param language The language name (e.g. 'javascript')
* @returns Array of objects with raw comment and cleaned content without markers
*/
declare function extractComments(code: string, language: string): ExtractedComment[];
/**
* Detects the language from a file path by its extension.
* @param filePath The file path
* @returns Language name or null if unsupported
*/
declare function getLanguageFromFilePath(filePath: string): string | null;
/**
* Parses a file and extracts all comments, language, and line count.
* @param filePath Path to the file
* @returns Object with filePath, comments, language, and linesOfCode
*/
declare function parseFileForComments(filePath: string): Promise<{
filePath: string;
comments: string[];
language: string | null;
linesOfCode: number;
}>;
/**
* Finds files matching glob patterns, respecting .gitignore and additional ignore patterns.
* @param patterns Glob pattern(s) to match files
* @param options ParserOptions (rootDir, additionalIgnores)
* @returns Array of absolute file paths
*/
declare function findFilesWithGitignore(patterns: string | string[], options?: ParserOptions): Promise<string[]>;
interface LanguageConfig {
name: string;
extensions: string[];
lineComments: Array<{
regex: RegExp;
marker: string;
}>;
blockComments: Array<{
start: {
regex: RegExp;
marker: string;
};
end: {
regex: RegExp;
marker: string;
};
}>;
}
declare const LANGUAGE_CONFIGS: LanguageConfig[];
declare function getLanguageConfigForFile(filePath: string): LanguageConfig | null;
declare function isComment(line: string, config: LanguageConfig): boolean;
declare function isBlockCommentStart(line: string, config: LanguageConfig): boolean;
declare function isBlockCommentEnd(line: string, config: LanguageConfig): boolean;
declare function isInBlockComment(line: string, config: LanguageConfig): boolean;
export { type ExtractedComment, FILE_EXTENSION_TO_LANGUAGE, LANGUAGE_CONFIGS, type LanguageConfig, extractComments, findFilesWithGitignore, getLanguageConfigForFile, getLanguageFromFilePath, isBlockCommentEnd, isBlockCommentStart, isComment, isInBlockComment, parseFileForComments };