0.0.27 • Published 8 months ago

@blast-shield/parser v0.0.27

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

📋 @blast-shield/parser

Parser utility for Blast Shield. Provides file parsing, comment extraction, language detection, and language config helpers with no technical debt logic.

License: MIT npm version

ESM Ready TypeScript

Features

  • Parse files for comments and language
  • Extract comments from code in multiple languages
  • Find files respecting .gitignore and custom ignore patterns
  • Unified language config for both regex and string-based comment detection
  • Language config helpers for heuristics, UI, and fallback logic

Usage

import {
  parseFileForComments,
  findFilesWithGitignore,
  extractComments,
  getLanguageFromFilePath,
  LANGUAGE_CONFIGS,
  getLanguageConfigForFile,
  isComment,
  isBlockCommentStart,
  isBlockCommentEnd,
  isInBlockComment
} from '@blast-shield/parser';

// Find files
const files = await findFilesWithGitignore(['src/**/*.ts'], { rootDir: process.cwd(), additionalIgnores: ['dist/**'] });

// Parse a file for comments
const result = await parseFileForComments(files[0]);
console.log(result.comments, result.language, result.linesOfCode);

// Use language config helpers
const langConfig = getLanguageConfigForFile('foo.ts');
if (langConfig) {
  console.log(isComment('// TODO: refactor', langConfig)); // true
}

API

parseFileForComments(filePath: string)

Returns { filePath, comments, language, linesOfCode } for a given file.

findFilesWithGitignore(patterns, options)

Find files matching glob patterns, respecting .gitignore and additional ignore patterns.

  • patterns: string or string[]
  • options: { rootDir?: string, additionalIgnores?: string[] }

extractComments(code: string, language: string)

Extracts all comments from code for a given language (using regex-based config).

getLanguageFromFilePath(filePath: string)

Detects the language from a file path.

LANGUAGE_CONFIGS

Unified array of language configs, each with:

  • name: string
  • extensions: string[]
  • lineComments: { regex: RegExp, marker: string }[]
  • blockComments: { start: { regex, marker }, end: { regex, marker } }[]

getLanguageConfigForFile(filePath: string)

Returns the language config for a file, or null if unsupported.

isComment(line: string, config: LanguageConfig)

Returns true if the line is a comment (single-line or block) for the given language config.

isBlockCommentStart(line: string, config: LanguageConfig)

Returns true if the line starts a block comment.

isBlockCommentEnd(line: string, config: LanguageConfig)

Returns true if the line ends a block comment.

isInBlockComment(line: string, config: LanguageConfig)

Returns true if the line is inside a block comment.

Logging

Uses the logger package for warnings and errors.

License

MIT

0.0.27

8 months ago

0.0.26

8 months ago

0.0.25

8 months ago

0.0.24

8 months ago

0.0.23

8 months ago

0.0.13

8 months ago

0.0.12

8 months ago

0.0.11

8 months ago

0.0.10

8 months ago

0.0.6

8 months ago

0.0.5

8 months ago

0.0.4

8 months ago

0.0.3

8 months ago

0.0.2

8 months ago

0.0.1

8 months ago