An open API service indexing awesome lists of open source software.

https://github.com/bentimor/salts

Detect unused functions and methods in TypeScript projects
https://github.com/bentimor/salts

Last synced: about 17 hours ago
JSON representation

Detect unused functions and methods in TypeScript projects

Awesome Lists containing this project

README

          

# Salts

A TypeScript dead code detector that finds unused functions and methods using Babel AST analysis.

![Example Screenshot](assets/example-screenshot.png "Example Screenshot")

## Features

- Detects unused functions, methods, and arrow functions
- Handles alias resolution (variable assignments, imports, exports)
- Supports JSX callback detection
- Optional React function component detection (`--jsx-components`)
- Tracks transitive usage through assignment chains
- Configurable export handling

## Installation

```bash
npm install salts
```

### From Source

```bash
npm install
npm run build
```

## Usage

```bash
# Analyze a directory
npx salts ./src

# Include exported functions in the report
npx salts ./src --include-exports

# Count React component usage as function usage
npx salts ./src --jsx-components

# Ignore additional patterns
npx salts ./src --ignore "**/*.test.ts" --ignore "**/fixtures/**"
```

### Options

| Option | Description |
|--------|-------------|
| `--include-exports` | Include exported functions in dead code report (by default, exports are considered "used") |
| `--jsx-components` | Count JSX component usage (e.g., ``) as function usage |
| `--ignore ` | Additional glob patterns to exclude from analysis |

## How It Works

### What's Detected as a Function Declaration

- Named functions: `function foo() {}`
- Class methods: `class X { method() {} }`
- Arrow functions: `const foo = () => {}`
- Function expressions: `const foo = function() {}`

### What Counts as Usage

A function is considered "used" if it is:

1. Called directly: `myFunc()`
2. Called as a method: `obj.myFunc()`, `this.myFunc()`, `obj['myFunc']()`
3. Passed as a callback: `arr.map(myFunc)`
4. Used in JSX: ``
5. Assigned and the assignment is used: `const x = myFunc; x()`
6. Imported with alias: `import { func as alias }` where `alias` is used
7. Exported with alias: `export { func as alias }` where `alias` is used
8. Used as a JSX component: `` (requires `--jsx-components` flag)

### Alias Resolution

Assignment chains are followed transitively:

```typescript
const x = myFunc;
const y = x;
y(); // myFunc is marked as used
```

## Default Ignored Patterns

- `**/node_modules/**`
- `**/dist/**`
- `**/build/**`
- `**/*.d.ts`
- `**/.git/**`

## Development

```bash
# Build the project
npm run build

# Watch mode
npm run dev

# Run tests
npm test

# Run tests with coverage
npm run test:coverage
```

## License

MIT