Mmohebifar

javascript-no-extra-bind-cleanup

Remove redundant inline .bind(...) wrappers from arrow functions and function expressions when rebinding and partial application have no semantic effect.

javascripttypescripteslintno-extra-bindcleanup
Public
0 executions

Run locally

npx codemod javascript-no-extra-bind-cleanup

javascript-no-extra-bind-cleanup

Remove redundant inline .bind(...) calls from arrow functions and function expressions when the bind wrapper has no semantic effect.

What It Rewrites

The codemod targets inline function-like expressions:

  • (() => doWork()).bind(obj)
  • (function () { return 1; }).bind(ctx)
  • Inline safe cases nested in arguments, returns, conditionals, and JSX/TSX expressions

It removes .bind(...) only when the callee is syntactically an inline arrow function or function expression and there is no partial application.

Safety Rules

The codemod skips cases that can change behavior or observable metadata:

  • Function expressions that reference dynamic this
  • Function expressions that reference arguments
  • Any .bind(...) call with more than one argument
  • Bound results used with new
  • Bound results immediately accessed via .name, .length, or .prototype
  • Bound results immediately accessed via computed metadata reads such as ["name"]
  • Optional chaining bind calls such as fn?.bind(obj)
  • Dynamic property access such as fn["bind"](obj)
  • Non-inline callees such as const g = f.bind(obj)
  • Function expressions with a TypeScript this parameter

File Types

The workflow runs over:

  • **/*.{js,jsx,mjs,cjs}
  • **/*.{ts,tsx,mts,cts}

Examples

Before:

js

After:

js

Before:

tsx

After:

tsx

Development

bash

The test suite includes JavaScript and TSX fixtures covering safe removals, preserved reflective cases, and TypeScript-specific guardrails.

License

MIT

Ready to contribute?

Build your own codemod and share it with the community.