Skip to content

TypeScriptコードからフィーチャーフラグを自動的に削除するCLIツールのPoC

Notifications You must be signed in to change notification settings

hirokinoue/remove-feat-flag

Repository files navigation

remove-feat-flag

A CLI tool to automatically remove feature flags from TypeScript code.

Overview

This tool detects code related to the specified feature flag and automatically rewrites the code to keep only the code when the flag is enabled (the then branch).

Main Features

  • Recursively traverse directories to scan TypeScript code
  • Detect calls to feature flag functions (default: isFeatureReleased('flag-name'))
  • Support custom function names via --function option
  • Remove variable declarations
  • Replace if statements with the content of the thenStatement

Installation

yarn install

yarn build

Usage

Basic Syntax

yarn dev <target-directory> <flag-name> [--write | --suffix] [--function <function-name>]

Examples

# Preview changes (dry-run mode, default)
yarn dev ./src AWESOME_FEATURE

# Remove AWESOME_FEATURE flag (directly update source files)
yarn dev ./src AWESOME_FEATURE --write

# Suffix mode (preserve original files, create .fixed files)
yarn dev ./src AWESOME_FEATURE --suffix

# Use a custom function name
yarn dev ./src AWESOME_FEATURE --function checkFeature

Options

By default, the tool runs in dry-run mode without modifying any files. To actually modify files, use one of the following options:

  • --write: Overwrite the original files directly with the modified content.
  • --suffix: Create new files with a .fixed suffix containing the modified content, leaving the original files unchanged.
  • --function <function-name>: Specify a custom function name to detect feature flags. Default is isFeatureReleased.

Note: --write and --suffix cannot be used together.

Examples

Pattern 1: Using via Variable

Before:

const isReleased = isFeatureReleased('AWESOME_FEATURE');

if (isReleased) {
  console.log('Feature is enabled');
} else {
  console.log('Feature is disabled');
}

After:

console.log('Feature is enabled');

Pattern 2: Direct Call

Before:

if (isFeatureReleased('AWESOME_FEATURE')) {
  console.log('Feature is enabled');
}

After:

console.log('Feature is enabled');

Important Notes

File Modifications

  • Default (dry-run mode): Files are not modified (preview only)
  • --write mode: Files are directly overwritten
  • --suffix mode: Original files are preserved, and .fixed files are created

Note: By default, the tool runs in dry-run mode for safety. Use --write or --suffix to actually modify files.

Code Formatting

This tool removes feature flag code as-is, preserving the original indentation and whitespace. After running this tool, it is recommended to use a code formatter (e.g., Prettier) to clean up any formatting inconsistencies that may result from the removal of code blocks.

Development

Scripts

# Run tests
yarn test

# Run tests in watch mode
yarn test:watch

# Run tests with UI
yarn test:ui

# Run tests with coverage
yarn test:coverage

# Format code
yarn format

# Check formatting
yarn format:check

# Lint
yarn lint

# Fix linting issues automatically
yarn lint:fix

Project Structure

remove-feat-flag/
├── src/
│   ├── lib/                      # Core library (AST parsing, TypeScript file discovery, replacement processing, type definitions, etc.)
│   └── remove-feat-flag.ts       # Main script
├── test-fixtures/
│   └── input/                    # Test sample files
├── dist/                         # Build output
├── package.json
├── tsconfig.json
├── tsconfig.eslint.json          # TypeScript configuration for ESLint
├── vitest.config.ts              # Vitest test configuration
├── eslint.config.mjs
└── README.md

Testing

Tests are executed using vitest. Test files (.test.ts) are placed in the same directory as the modules being tested. Test data is located in test-fixtures/input/.

How It Works

  1. AST Parsing: Converts code to AST using TypeScript Compiler
  2. Flag Detection: Detects calls to the specified function (default: isFeatureReleased('flag-name'))
  3. Pattern Detection:
    • If assigned to a variable → Remove the variable declaration + Replace if statements using that variable
    • If used directly in an if statement → Replace the if statement
  4. Replacement Execution: Replace the entire if statement with the content of the thenStatement (excluding braces)
  5. File Saving: Write changes to files

License

MIT

About

TypeScriptコードからフィーチャーフラグを自動的に削除するCLIツールのPoC

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published