UNPKG

@drmhse/remove-comments

Version:

A robust, zero-dependency CLI tool for removing comments from source code using token-based state machine parsing. Supports JavaScript, TypeScript, Java, Kotlin, Python, Vue, React, and Dart with advanced template literal processing.

110 lines (76 loc) 3.76 kB
# @drmhse/remove-comments [![npm version](https://badge.fury.io/js/%40drmhse%2Fremove-comments.svg)](https://badge.fury.io/js/%40drmhse%2Fremove-comments) A robust, zero-dependency CLI tool for removing comments from source code across multiple programming languages. Implements token-based state machine parsing to safely handle complex language constructs while preserving code functionality. ## Features - **Advanced Parsing Engine:** Uses token-based state machine with context awareness for strings, template literals, regular expressions, and escape sequences - **Template Literal Support:** Correctly processes JavaScript template expressions `${...}` while preserving nested string content - **Comprehensive Language Support:** - JavaScript/TypeScript (.js, .ts, .jsx, .tsx) - Full ES6+ support including template literals - Java/Kotlin (.java, .kt, .kts) - C-style comments with proper escape handling - Python (.py) - Hash comments and docstring processing - Vue (.vue) - Script tag processing with component-aware parsing - Dart (.dart) - C-style comments with raw string support - **Zero Dependencies:** Self-contained executable requiring no additional packages - **Git Integration:** Respects .gitignore patterns and monorepo structures - **Safe Operation:** Dry-run mode with detailed preview of all modifications ## Usage Execute directly using npx without requiring project installation. ### Preview Changes (Recommended) Perform a dry run to preview modifications before execution: ```bash npx @drmhse/remove-comments@latest --dry-run ``` ### Directory Processing Process all supported files in the current directory: ```bash npx @drmhse/remove-comments@latest ``` Target a specific subdirectory: ```bash npx @drmhse/remove-comments@latest src/ ``` ### Single File Processing Process an individual file: ```bash npx @drmhse/remove-comments@latest src/main.js ``` ### Command Line Options ``` Usage: remove-comments [options] [path] Options: --dry-run Preview changes without modifying files --help Display usage information --version Display version information Arguments: path Target file or directory (default: current directory) ``` ## Implementation Details ### Parsing Architecture The tool employs a token-based state machine parser that maintains context awareness across different language constructs: - **State Management:** Tracks current parsing context (code, strings, comments, template literals, escape sequences) - **Context Preservation:** Maintains nested state information for accurate context restoration - **Token Classification:** Distinguishes between comment markers and similar patterns within strings or other contexts ### Language-Specific Processing **JavaScript/TypeScript Family:** - Template literal expressions (`${...}`) are processed as JavaScript contexts - Comments within template expressions are removed while preserving nested strings - Regular expression literals are protected from comment-like patterns - Full ES6+ syntax support including arrow functions and destructuring **Java/Kotlin:** - C-style comment processing with proper escape sequence handling - Multi-line string and text block support - Raw string literal protection in Kotlin **Vue Components:** - Script tag content extraction and processing - Template and style blocks remain unmodified - Component-aware parsing maintains Vue.js compatibility **Python:** - Hash-style comment removal - Docstring processing with triple-quote detection - String literal preservation with proper escaping **React/JSX:** - JSX comment syntax (`{/* ... */}`) preprocessing - Standard JavaScript comment processing - JSX attribute and expression context awareness ## License MIT