0.0.27 • Published 9 months ago

@blast-shield/parser v0.0.27

Weekly downloads
-
License
MIT
Repository
github
Last release
9 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

9 months ago

0.0.26

9 months ago

0.0.25

9 months ago

0.0.24

9 months ago

0.0.23

10 months ago

0.0.13

10 months ago

0.0.12

10 months ago

0.0.11

10 months ago

0.0.10

10 months ago

0.0.6

10 months ago

0.0.5

10 months ago

0.0.4

10 months ago

0.0.3

10 months ago

0.0.2

10 months ago

0.0.1

10 months ago