or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

index.mddocs/

Babel Plugin Transform ES2015 Literals

Babel plugin that compiles ES2015 unicode string and number literals to ES5-compatible code. It transforms binary integer literals (0b), octal integer literals (0o), and unicode string literals with escape sequences (\u{}) into their computed values.

Package Information

  • Package Name: babel-plugin-transform-es2015-literals
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install --save-dev babel-plugin-transform-es2015-literals

Core Imports

// The plugin is imported by Babel configuration, not directly in code
// Plugin name for Babel configuration: "transform-es2015-literals"

For direct import (not typical usage):

import transformLiterals from "babel-plugin-transform-es2015-literals";

CommonJS:

const transformLiterals = require("babel-plugin-transform-es2015-literals");

Basic Usage

Via .babelrc (Recommended)

{
  "plugins": ["transform-es2015-literals"]
}

Via CLI

babel --plugins transform-es2015-literals script.js

Via Node API

require("babel-core").transform("code", {
  plugins: ["transform-es2015-literals"]
});

Transformation Examples

Input:

var b = 0b11; // binary integer literal
var o = 0o7; // octal integer literal  
const u = 'Hello\u{000A}\u{0009}!'; // unicode string literals, newline and tab

Output:

var b = 3; // binary integer literal
var o = 7; // octal integer literal
const u = 'Hello\n\t!'; // unicode string literals, newline and tab

Capabilities

Plugin Factory Function

The main export is a function that returns a Babel plugin configuration object.

/**
 * Creates a Babel plugin that transforms ES2015 literals
 * @returns {Object} Babel plugin configuration with visitor methods
 */
function transformES2015Literals(): BabelPlugin;

interface BabelPlugin {
  visitor: {
    NumericLiteral: ({ node }: { node: NumericLiteral }) => void;
    StringLiteral: ({ node }: { node: StringLiteral }) => void;
  };
}

Numeric Literal Transformation

Transforms ES2015 binary (0b) and octal (0o) number literals by removing the raw representation, allowing Babel's code generator to output the computed values.

/**
 * Visitor method for numeric literal AST nodes
 * Transforms binary (0b) and octal (0o) literals to their computed values
 * @param {Object} params - Destructured parameters object
 * @param {NumericLiteral} params.node - The numeric literal AST node
 */
NumericLiteral({ node }: { node: NumericLiteral }): void;

Supported formats:

  • Binary literals: 0b113, 0B1015
  • Octal literals: 0o77, 0O1715

String Literal Transformation

Transforms ES2015 unicode string literals containing unicode escape sequences by removing the raw representation, allowing Babel's code generator to output the interpreted unicode characters.

/**
 * Visitor method for string literal AST nodes
 * Transforms unicode escape sequences to their character representations
 * @param {Object} params - Destructured parameters object
 * @param {StringLiteral} params.node - The string literal AST node
 */
StringLiteral({ node }: { node: StringLiteral }): void;

Supported formats:

  • Unicode code point escapes: \u{000A} → newline, \u{0009} → tab
  • Traditional unicode escapes: \u000A → newline, \u0009 → tab
  • Case insensitive: \u{41} and \U{41} both → A

Types

interface NumericLiteral {
  type: "NumericLiteral";
  value: number;
  extra?: {
    raw: string;
    rawValue: number;
  };
}

interface StringLiteral {
  type: "StringLiteral";
  value: string;
  extra?: {
    raw: string;
    rawValue: string;
  };
}

interface NodePath<T> {
  node: T;
}

Configuration

This plugin requires no configuration options. It automatically detects and transforms all ES2015 literal syntax that needs conversion for ES5 compatibility.

Dependencies

  • babel-runtime: ^6.22.0 (runtime dependency)