Babel plugin that removes numeric separators from decimal, binary, hex and octal literals
npx @tessl/cli install tessl/npm-babel--plugin-proposal-numeric-separator@7.18.0@babel/plugin-proposal-numeric-separator is a Babel plugin that removes numeric separators from decimal, binary, hexadecimal, and octal literals. It enables developers to write more readable numeric constants using underscores (like 1_000_000 for one million) while ensuring compatibility with JavaScript engines that don't support this ES2021 feature.
npm install --save-dev @babel/plugin-proposal-numeric-separatorThis package exports a default Babel plugin function that should be configured in your Babel setup:
// babel.config.js
module.exports = {
plugins: ["@babel/plugin-proposal-numeric-separator"]
};The plugin automatically transforms numeric literals with separators during Babel's compilation process:
Input code:
// Decimal numbers
const million = 1_000_000;
const billion = 1_000_000_000;
// Hexadecimal numbers
const hex = 0xFF_EC_DE_5E;
// Binary numbers
const binary = 0b1010_0001_1000_0101;
// Octal numbers
const octal = 0o0_6_6_6;
// BigInt literals
const bigInt = 9_007_199_254_740_993n;Output code:
// Decimal numbers
const million = 1000000;
const billion = 1000000000;
// Hexadecimal numbers
const hex = 0xFFECDE5E;
// Binary numbers
const binary = 0b1010000110000101;
// Octal numbers
const octal = 0o0666;
// BigInt literals
const bigInt = 9007199254740993n;@babel/plugin-proposal-numeric-separator follows Babel's standard plugin architecture:
@babel/helper-plugin-utils declare() function for standardized plugin creation@babel/plugin-syntax-numeric-separator to enable parsing of numeric separator syntaxNumericLiteral and BigIntLiteral AST nodesextra.raw property by removing underscore characters while preserving the numeric valueThe plugin operates during Babel's transform phase, after parsing but before code generation, ensuring numeric separators are removed for broader JavaScript engine compatibility.
The package exports a default Babel plugin object created using the declare() helper function.
import { declare } from "@babel/helper-plugin-utils";
import syntaxNumericSeparator from "@babel/plugin-syntax-numeric-separator";
/**
* Default export: Babel plugin that removes numeric separators from literals
*/
const plugin: BabelPlugin;
export default plugin;
/**
* Plugin definition using Babel's declare helper
*/
interface BabelPlugin {
name: "proposal-numeric-separator";
inherits: typeof syntaxNumericSeparator.default;
visitor: {
NumericLiteral: RemoverFunction;
BigIntLiteral: RemoverFunction;
};
}
type RemoverFunction = (path: {
node: {
extra?: {
raw?: string;
};
};
}) => void;The plugin provides the following transformation capabilities:
1_000 → 1000)0xFF_FF → 0xFFFF)0b1010_0001 → 0b10100001)0o777_777 → 0o777777)123_456n → 123456n)The plugin uses Babel's standard plugin architecture:
@babel/plugin-syntax-numeric-separator for parsingNumericLiteral and BigIntLiteral nodesextra.raw property of literal nodesThe plugin requires the following dependencies:
Add to your Babel configuration file:
// babel.config.js
module.exports = {
plugins: ["@babel/plugin-proposal-numeric-separator"]
};
// Or with preset
module.exports = {
presets: ["@babel/preset-env"],
plugins: ["@babel/plugin-proposal-numeric-separator"]
};Webpack with babel-loader:
module.exports = {
module: {
rules: [
{
test: /\.js$/,
use: {
loader: 'babel-loader',
options: {
plugins: ['@babel/plugin-proposal-numeric-separator']
}
}
}
]
}
};Rollup with @rollup/plugin-babel:
import babel from '@rollup/plugin-babel';
export default {
plugins: [
babel({
plugins: ['@babel/plugin-proposal-numeric-separator']
})
]
};