Babel plugin that removes numeric separators from Decimal, Binary, Hex and Octal literals
npx @tessl/cli install tessl/npm-babel--plugin-transform-numeric-separator@7.27.0@babel/plugin-transform-numeric-separator is a Babel plugin that removes numeric separators (underscores) from numeric literals during JavaScript/TypeScript compilation. It transforms ES2021 numeric separator syntax to ensure compatibility with older JavaScript environments while preserving numeric values.
npm install --save-dev @babel/plugin-transform-numeric-separatorThis plugin is not imported directly in your code - it's used as part of your Babel configuration:
{
"plugins": ["@babel/plugin-transform-numeric-separator"]
}Or with options (none required for this plugin):
{
"plugins": [["@babel/plugin-transform-numeric-separator", {}]]
}Add the plugin to your Babel configuration file:
.babelrc.json:
{
"plugins": ["@babel/plugin-transform-numeric-separator"]
}babel.config.js:
module.exports = {
plugins: ["@babel/plugin-transform-numeric-separator"]
};Input code with numeric separators:
// Decimal numbers
const million = 1_000_000;
const large = 1_000;
// Binary literals
const binary = 0b1010_0001_1000_0101;
// Hexadecimal literals
const hex = 0xA0_B0_C0;
// Octal literals
const octal = 0o0_6_6_6;
// BigInt literals
const bigint = 9_007_199_254_740_993n;Output after transformation:
// Decimal numbers
const million = 1000000;
const large = 1000;
// Binary literals
const binary = 0b1010000110000101;
// Hexadecimal literals
const hex = 0xA0B0C0;
// Octal literals
const octal = 0o0666;
// BigInt literals
const bigint = 9007199254740993n;The plugin operates as a Babel AST visitor that:
The main export is a Babel plugin created using the declare helper utility from @babel/helper-plugin-utils.
/**
* Default export: Babel plugin that transforms numeric separators
* Created using declare() from @babel/helper-plugin-utils
*/
const plugin: BabelPlugin;
export default plugin;
// Plugin configuration returned by the factory
interface BabelPlugin {
/** Plugin identifier name */
name: "transform-numeric-separator";
/** AST visitor configuration */
visitor: {
NumericLiteral: (path: NodePath<NumericLiteral>) => void;
BigIntLiteral: (path: NodePath<BigIntLiteral>) => void;
};
/** Parser plugin configuration (conditional based on Babel version) */
manipulateOptions?: (options: any, parser: { plugins: string[] }) => void;
}The plugin works with standard Babel AST node types for numeric literals.
// Import types from @babel/types or @babel/core
import type { NodePath } from "@babel/core";
import type { NumericLiteral, BigIntLiteral } from "@babel/types";
// The plugin processes these AST node types:
type NumericLiteral = {
type: "NumericLiteral";
value: number;
extra?: { raw?: string };
};
type BigIntLiteral = {
type: "BigIntLiteral";
value: string;
extra?: { raw?: string };
};
type NodePath<T> = {
node: T;
// Additional Babel NodePath properties...
};The plugin transforms all JavaScript numeric literal formats:
1_000_000 → 10000000b1010_0001 → 0b101000010xAE_BE_CE → 0xAEBECE0o0_6_6_6 → 0o0666123_456n → 123456n{
"peerDependencies": {
"@babel/core": "^7.0.0-0"
},
"dependencies": {
"@babel/helper-plugin-utils": "^7.0.0"
}
}When using the plugin, ensure these are available in your project:
npm install --save-dev @babel/core @babel/plugin-transform-numeric-separatorThe plugin uses api.assertVersion(REQUIRED_VERSION(7)) to ensure compatibility:
^7.0.0-0^7.0.0-0 || >8.0.0-alpha <8.0.0-betaThis plugin requires no configuration options. It automatically processes all numeric literals with underscore separators found in the source code.
The plugin enables ES2021 numeric separator syntax to work in: