npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

chainsafe

v1.0.3

Published

A CLI tool to automatically add optional chaining to TypeScript and JavaScript files

Downloads

258

Readme

chainsafe 🔗

A CLI tool to automatically add optional chaining to TypeScript and JavaScript files.

Installation

npm install -g chainsafe

Usage

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 

Examples

Process all files in a directory

chainsafe src/

Process only TypeScript files

chainsafe src/ --type ts

Process only JavaScript files

chainsafe src/ --type js

Skip certain globals

chainsafe src/ --skip axios lodash

Remove items from built-in globals list

chainsafe src/ --no-skip Array Object

Preview changes without writing

chainsafe src/ --preview

View current skip list

chainsafe src/ --skip-list

Apply optional chaining to everything

chainsafe src/ --skip-none

Only skip specific modules

chainsafe src/ --skip-only axios lodash

Only apply to specific modules

chainsafe src/ --apply-only axios process

Before and After

Before:

const user = getUser();
const name = user.profile.name;
const city = user.profile.address.city;
const apiData = axios.get('/api').data.items;

After:

const user = getUser();
const name = user?.profile?.name;
const city = user?.profile?.address?.city;
const apiData = axios?.get('/api')?.data?.items;  // With --apply-only axios

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

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

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

  • Uses @babel/parser for accurate TypeScript/JavaScript parsing
  • Analyzes AST (Abstract Syntax Tree) for safe transformations
  • Preserves source code formatting
  • Handles complex nested expressions
  • Supports .ts, .tsx, .js, and .jsx files
  • Smart module-specific transformation support
  • Configurable transformation scope

Error Handling

The tool will:

  • Skip files it can't process safely
  • Report parsing errors clearly
  • Maintain original file on transformation failure
  • Skip invalid global names in --skip/--no-skip
  • Validate mutually exclusive options
  • Ensure proper argument format for all options

Tips

  1. Always use --preview first when processing many files
  2. Back up important files before processing
  3. Use --skip-list to verify current globals configuration
  4. Process one directory at a time for better control
  5. Use --apply-only for targeted transformations
  6. Use --skip-none when maximum coverage is needed
  7. Combine with --preview to verify transformations

Advanced Usage

Targeted Transformations

# Only transform axios and lodash chains
chainsafe src/ --apply-only axios lodash --preview

# Transform everything except specific modules
chainsafe src/ --skip-only axios fetch

# Transform absolutely everything
chainsafe src/ --skip-none

Option Combinations

# Preview targeted transformations
chainsafe src/ --apply-only axios --preview

# Process only TypeScript files with targeted transformations
chainsafe src/ --type ts --apply-only axios

Release Notes

Version 1.0.2 🚀 (October 26, 2024)

New Features 🎉

  1. Apply-Only Mode (--apply-only)

    • Selectively apply optional chaining to specific modules
    • Target transformations to particular packages
    chainsafe src/ --apply-only axios lodash
  2. Skip-None Mode (--skip-none)

    • Bypass built-in globals protection
    • Apply optional chaining universally
    chainsafe src/ --skip-none
  3. Skip-Only Mode (--skip-only)

    • Custom control over skipped modules
    • Replace default built-in globals list
    chainsafe src/ --skip-only axios fetch

Improvements 🔨

  • Enhanced module-specific transformation logic
  • Better handling of nested member expressions
  • Improved validation for mutually exclusive options
  • Clearer error messages for invalid configurations

Breaking Changes ⚠️

None. All new features are backward compatible.

Known Issues 🚧

  • Multiple chained operations might require multiple passes
  • Computed property access with --apply-only may need review

Installation & Upgrade

# New installation
npm install -g chainsafe

# Upgrade existing installation
npm update -g chainsafe

Contributing

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)

Links