DA
dasariumamahesh/chainsafe
A CLI tool to automatically add optional chaining to TypeScript and JavaScript
chainsafe ๐
A CLI tool to automatically add optional chaining to TypeScript and JavaScript files.
Installation
npm install -g chainsafeUsage
chainsafe <path> [options]Options
chainsafe --help
Options:
--type ts|js Process only TypeScript or JavaScript files
--skip <names> Add names to built-in globals to be skipped
--no-skip <names> Remove names from built-in globals skip list
--skip-list Print current built-in globals list
--preview Preview changes without writing to files
--skip-none Ignore built-in globals and apply optional chaining to everything
--skip-only <names> Only skip the specified names, ignore built-in globals
--apply-only <names> Only apply optional chaining to specified modules/variables
--help Show help information Features
- โจ Automatic optional chaining transformation
- ๐ Supports both TypeScript and JavaScript files
- ๐ฏ Smart detection of potentially nullable expressions
- ๐ก๏ธ Preserves existing optional chaining
- โก Fast and efficient processing
- ๐ Safe transformation with built-in globals protection
- ๐ Preview mode for safe checking
- ๐ Process single files or entire directories
- ๐ซ Skip specific globals as needed
- ๐ฏ Selective application to specific modules
- ๐ Global application with skip-none mode
- ๐ Fine-grained control over transformations
- ๐ Multiple pass transformations for nested chains
- ๐ Detailed statistics and error reporting
- ๐ ๏ธ Advanced configuration options
Examples
Process all files in a directory
chainsafe src/Process only TypeScript files
chainsafe src/ --type tsProcess only JavaScript files
chainsafe src/ --type jsSkip certain globals
chainsafe src/ --skip axios,lodashRemove items from built-in globals list
chainsafe src/ --no-skip Array,ObjectPreview changes without writing
chainsafe src/ --previewView current skip list
chainsafe src/ --skip-listApply optional chaining to everything
chainsafe src/ --skip-noneOnly skip specific modules
chainsafe src/ --skip-only axios,lodashOnly apply to specific modules
chainsafe src/ --apply-only axios,processMultiple Pass Transformations
The tool performs multiple passes (default: 5) to handle deeply nested chains:
Before:
const data = service.getData().process().validate();
const nested = obj.deeply.nested.property.access;
const mixed = api.get().data.process().validate().result;After:
const data = service?.getData()?.process()?.validate();
const nested = obj?.deeply?.nested?.property?.access;
const mixed = api?.get()?.data?.process()?.validate()?.result;Built-in Globals
The following globals are protected by default and won't receive optional chaining:
- Array
- Object
- String
- Number
- Boolean
- Date
- Math
- JSON
- RegExp
- Error
- Map
- Set
- Promise
- Function
- console
- Buffer
- process
You can:
- Add to this list using
--skip - Remove from this list using
--no-skip - Replace entirely using
--skip-only - Ignore this list using
--skip-none - Target specific modules using
--apply-only
Technical Details
Parser Features
- Uses @babel/parser for accurate TypeScript/JavaScript parsing
- Intelligent AST (Abstract Syntax Tree) analysis
- Preserves source code formatting
- Handles complex nested expressions
- Supports .ts, .tsx, .js, and .jsx files
Configuration
- Maximum file size limit (default: 10MB)
- Configurable ignored directories
- Customizable file extensions
- Binary file detection and skipping
- Multiple pass iterations for nested chains
TypeScript Support
- Enhanced enum handling
- Improved type reference detection
- Optimized plugin configuration
- Support for TypeScript-specific constructs
- Proper handling of type assertions
Error Handling
The tool provides:
- Detailed error context (phase, expression, location)
- Clear parsing error messages
- File processing statistics
- Warning detection and reporting
- Safe file transformation
- Validation of skip/apply options
- Original file preservation on failure
Statistics and Reporting
Each run provides:
- Number of files processed
- Number of files modified
- Errors encountered
- Warnings detected
- Processing time
- Transformation details
Advanced Usage
Targeted Transformations
# Transform specific modules with preview
chainsafe src/ --apply-only axios,lodash --preview
# Exclude specific modules
chainsafe src/ --skip-only axios,fetch
# Transform everything
chainsafe src/ --skip-noneOption Combinations
# Preview TypeScript transformations
chainsafe src/ --type ts --preview
# Target specific modules in TypeScript
chainsafe src/ --type ts --apply-only axiosBest Practices
- Always use
--previewfirst for safety - Back up important files before processing
- Check
--skip-listbefore modifications - Process directories individually
- Use
--apply-onlyfor targeted changes - Test with sample files first
- Review statistics after processing
- Check warnings and error messages
Release Notes
Version 1.0.4 ๐ (January 20, 2025)
New Features ๐
-
Multiple Pass Transformations
- Support for deeply nested chains
- Configurable iteration count
- Improved nested property handling
-
Enhanced Configuration
- File size limits
- Directory exclusions
- Binary file handling
- Customizable extensions
-
Improved Error Handling
- Detailed error context
- Better error messages
- Warning detection
- Statistics tracking
-
TypeScript Improvements
- Better enum handling
- Enhanced type support
- Optimized plugins
Improvements ๐จ
- Optimized AST traversal
- Better skip logic
- Improved performance
- Enhanced error reporting
- Better TypeScript support
- Clearer documentation
Breaking Changes โ ๏ธ
None. All new features are backward compatible.
Known Issues ๐ง
- Complex computed properties might need review
- Some edge cases in TypeScript type assertions
- Very large files might need multiple runs
Installation & Upgrade
# New installation
npm install -g chainsafe
# Upgrade existing installation
npm update -g chainsafeContributing
Please submit issues and pull requests on GitHub at https://github.com/dasariumamahesh/chainsafe.
License
MIT ยฉ [Dasari Uma Mahesh (Mahesh)]
Author
Dasari Uma Mahesh (Mahesh)