Mmohebifar

javascript-unicorn-empty-brace-spaces-enforcement

Normalizes whitespace-only empty brace pairs from `{ }` to `{}` in JS-family runtime syntax, with optional flags for empty blocks, type-space braces, and comment removal.

javascripttypescripteslintstyleunicorn
Public
0 executions

Run locally

npx codemod javascript-unicorn-empty-brace-spaces-enforcement

javascript-unicorn-empty-brace-spaces-enforcement

Normalizes whitespace-only empty brace pairs from { } to {} in JS-family source, aligned with the intent of eslint-plugin-unicorn's empty-brace-spaces rule.

Installation

bash

Usage

By default the codemod rewrites only runtime-syntax empty braces when the interior contains whitespace only:

  • Object literals: const value = { }; -> const value = {};
  • Object patterns: let { } = source; -> let {} = source;
  • Empty named imports: import { } from "pkg"; -> import {} from "pkg";
  • Empty named exports: export { }; -> export {};

The workflow scans .js, .jsx, .mjs, .cjs, .ts, .mts, .cts, and .tsx files and ignores node_modules, dist, build, and coverage.

Preserve and no-op behavior:

  • Non-empty braces stay unchanged.
  • JSX expression container braces such as <Comp prop={ } /> stay unchanged.
  • Type-space braces such as type T = { }; stay unchanged by default.
  • Braces containing comments stay unchanged by default.
  • Empty blocks are excluded by default.

Optional workflow parameters:

  • include-empty-blocks=true: also normalize same-line empty statement_block braces such as if (x) { }.
  • include-type-braces=true: also normalize empty TypeScript object_type braces.
  • remove-comments-in-empty-braces=true: unsafe mode that removes comment trivia inside otherwise-empty braces.

Run locally:

bash

Development

bash

License

MIT

Before

This is one example from the codemod's test cases. The codemod may handle many more cases.

Ready to contribute?

Build your own codemod and share it with the community.