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.
npm install --save-dev babel-plugin-transform-es2015-literals// 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");{
"plugins": ["transform-es2015-literals"]
}babel --plugins transform-es2015-literals script.jsrequire("babel-core").transform("code", {
plugins: ["transform-es2015-literals"]
});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 tabOutput:
var b = 3; // binary integer literal
var o = 7; // octal integer literal
const u = 'Hello\n\t!'; // unicode string literals, newline and tabThe 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;
};
}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:
0b11 → 3, 0B101 → 50o7 → 7, 0O17 → 15Transforms 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:
\u{000A} → newline, \u{0009} → tab\u000A → newline, \u0009 → tab\u{41} and \U{41} both → Ainterface 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;
}This plugin requires no configuration options. It automatically detects and transforms all ES2015 literal syntax that needs conversion for ES5 compatibility.