or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-babel--plugin-transform-strict-mode

Babel plugin that automatically adds 'use strict' directive to JavaScript files to enable strict mode

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/@babel/plugin-transform-strict-mode@7.27.x

To install, run

npx @tessl/cli install tessl/npm-babel--plugin-transform-strict-mode@7.27.0

index.mddocs/

@babel/plugin-transform-strict-mode

A Babel plugin that automatically adds the 'use strict' directive to JavaScript files to enable strict mode execution. This plugin ensures all code runs in strict mode by detecting missing directives and adding them at the beginning of program files.

Package Information

  • Package Name: @babel/plugin-transform-strict-mode
  • Package Type: npm (Babel plugin)
  • Language: TypeScript (compiled to JavaScript)
  • Installation: npm install --save-dev @babel/plugin-transform-strict-mode

Core Imports

ES Module:

import strictModePlugin from "@babel/plugin-transform-strict-mode";

CommonJS:

const strictModePlugin = require("@babel/plugin-transform-strict-mode");

Note: The plugin internally uses:

  • declare from @babel/helper-plugin-utils for plugin creation
  • types as t from @babel/core for AST node creation
  • REQUIRED_VERSION utility for version compatibility

Basic Usage

Babel Configuration

Add the plugin to your Babel configuration:

{
  "plugins": ["@babel/plugin-transform-strict-mode"]
}

With Babel API:

import { transform } from "@babel/core";
import strictModePlugin from "@babel/plugin-transform-strict-mode";

const result = await transform(code, {
  plugins: [strictModePlugin]
});

Transformation Examples

Basic function (adds 'use strict'):

Input:

function greet(name) {
  return "Hello, " + name;
}

Output:

"use strict";

function greet(name) {
  return "Hello, " + name;
}

Preserves existing 'use strict' (no duplication):

Input:

"use strict";
foo();

Output:

"use strict";

foo();

Preserves leading comments:

Input:

// comments

module.exports = {};

Output:

"use strict";

// comments

module.exports = {};

Capabilities

Plugin Factory Function

Creates a Babel plugin instance that adds 'use strict' directives to JavaScript programs.

/**
 * Default export function created via @babel/helper-plugin-utils declare wrapper
 * Automatically handles Babel version compatibility and plugin initialization
 */
export default declare(api => {
  api.assertVersion(REQUIRED_VERSION(7));
  return {
    name: "transform-strict-mode";
    visitor: {
      Program(path: NodePath<Program>): void;
    };
  };
});

interface BabelPlugin {
  /** Plugin identifier used by Babel */
  name: "transform-strict-mode";
  /** AST visitor object containing transformation logic */
  visitor: {
    Program(path: NodePath<Program>): void;
  };
}

interface BabelAPI {
  /** Asserts minimum Babel version compatibility (requires version 7+) */
  assertVersion(version: number): void;
}

/** Helper function from @babel/helper-plugin-utils for creating Babel plugins */
declare function declare(pluginFactory: (api: BabelAPI) => BabelPlugin): BabelPlugin;

/** Version compatibility utility function */
declare function REQUIRED_VERSION(version: number): number;

Plugin Behavior

The plugin operates as follows:

  1. Version Check: Asserts Babel version 7+ compatibility via api.assertVersion(REQUIRED_VERSION(7))
  2. Directive Detection: Iterates through existing directives in the Program AST node
  3. Duplicate Prevention: Returns early if 'use strict' directive already exists (directive.value.value === "use strict")
  4. Directive Creation: Creates new directive using t.directive(t.directiveLiteral("use strict"))
  5. AST Manipulation: Uses path.unshiftContainer("directives", directive) to prepend the directive

Supported Scenarios

The plugin handles various code patterns:

  • Basic files: Adds 'use strict' to function declarations and expressions
  • Existing directives: Preserves existing 'use strict' without duplication
  • Leading comments: Maintains comment positioning while adding directive
  • Interpreter directives: Respects shebang lines and other interpreter directives

Types

/** AST node representing a JavaScript directive like 'use strict' */
interface Directive {
  type: "Directive";
  value: DirectiveLiteral;
}

/** AST node representing the literal value of a directive */
interface DirectiveLiteral {
  type: "DirectiveLiteral";
  value: string;
}

/** AST node representing a JavaScript program (top-level scope) */
interface Program {
  type: "Program";
  directives: Directive[];
  body: Statement[];
}

/** Babel NodePath wrapper providing transformation utilities */
interface NodePath<T> {
  /** AST node being visited */
  node: T;
  /** Adds nodes to the beginning of a container (like directives array) */
  unshiftContainer(listKey: "directives", nodes: Directive): void;
}

/** Babel AST types for creating directive nodes */
interface BabelTypes {
  /** Creates a directive AST node */
  directive(value: DirectiveLiteral): Directive;
  /** Creates a directive literal AST node */
  directiveLiteral(value: string): DirectiveLiteral;
}

Dependencies

The plugin requires these peer and runtime dependencies:

  • @babel/core: ^7.0.0-0 (peer dependency for types and AST utilities)
  • @babel/helper-plugin-utils: Provides the declare function wrapper and REQUIRED_VERSION utility
  • Node.js: >=6.9.0 (>=20.19.0 for Babel 8)

Error Handling

The plugin is designed to be fail-safe:

  • No Configuration Errors: Plugin accepts no options, eliminating configuration issues
  • AST Safety: Uses Babel's built-in AST manipulation methods
  • Idempotent Operation: Safe to run multiple times on the same code
  • Non-destructive: Preserves all existing code and directives