Mmohebifar

javascript-eslint-strict-directive-normalization

Normalize 'use strict' directive prologues across JavaScript, TypeScript, JSX, and TSX files using configurable global, function, never, or conservative safe policies.

javascripttypescripteslintstrict-modedirective-prologue
Public
0 executions

Run locally

npx codemod javascript-eslint-strict-directive-normalization

javascript-eslint-strict-directive-normalization

Normalize JavaScript and TypeScript strict-mode directives so a repository follows one deterministic policy equivalent to ESLint's strict rule. The codemod inserts, removes, or deduplicates 'use strict' at script and function scope, skips ESM insertion, removes redundant module directives when enabled, and can clean up ineffective misplaced literals.

Installation

bash

Usage

Default behavior:

  • policy=global
  • cleanupMisplaced=true
  • removeRedundantInModules=true
  • preserveStyle=true
  • quoteStyle=single

Supported policies:

  • global: add one top-level strict directive to classic scripts, dedupe duplicates, and remove redundant function-level strict directives.
  • function: add one strict directive to each function-like block body in classic scripts unless the file is already globally strict. Functions with non-simple parameter lists are skipped because function-level strict mode would be invalid there.
  • never: remove strict directives from program and function directive prologues.
  • safe: conservatively add function-level strict directives only when the function avoids known strict-mode hazards (this, arguments, eval, with, legacy octal literals, duplicate parameters, and non-simple parameter lists). Existing strict directives are still deduplicated.

Important preserve and no-op behavior:

  • Does not insert strict directives into ESM files, including package-level module contexts such as .js files under a package.json with "type": "module".
  • Removes redundant ESM strict directives only when removeRedundantInModules=true.
  • Skips .d.ts files, dist, vendor, generated, and *.min.* inputs.
  • Does not wrap concise arrow functions just to add a directive.
  • Preserves the relative order of existing directives such as 'use client' or 'use asm' when adding 'use strict'.
  • Skips prologue rewrites when doing so would cross comment-sensitive directive regions.

Examples:

bash

Development

bash

License

MIT

Ready to contribute?

Build your own codemod and share it with the community.