Babel plugin that compiles ES2015 unicode string and number literals to ES5
86
Create a Babel plugin that transforms hexadecimal numeric literals in JavaScript code to their decimal equivalents during the code generation phase. The plugin should leverage Babel's code generator behavior to produce the output, rather than directly manipulating the AST nodes.
Your plugin should handle hexadecimal literals (e.g., 0xFF, 0x1A2B) and ensure they are output as decimal numbers in the generated code.
Plugin Structure: Create a proper Babel plugin that exports a plugin function using the standard Babel plugin API.
Generator Integration: Use an indirect transformation approach by manipulating AST node metadata properties rather than directly rewriting the nodes. Let Babel's code generator handle the actual output formatting.
Hexadecimal Detection: Identify hexadecimal numeric literals in the source code (prefixes 0x or 0X).
Transformation: Ensure that when the code is generated, hexadecimal literals are output as their decimal equivalents (e.g., 0xFF becomes 255).
Member Expression Support: The plugin must correctly handle hexadecimal literals used in member expressions (e.g., 0xFF.toString()).
Input:
const value = 0xFF;
const another = 0x1A;Expected Output:
const value = 255;
const another = 26;@test
Input:
const upper = 0XAB;
const lower = 0xcd;Expected Output:
const upper = 171;
const lower = 205;@test
Input:
const str = 0xFF.toString();Expected Output:
const str = 255.toString();@test
src/
index.js # Main plugin implementation
src/
index.test.js # Test file with the above test cases
package.json # Package configurationProvides the Babel transformation system and APIs for creating plugins.
Provides utilities for declaring and managing Babel plugins across versions.
Install with Tessl CLI
npx tessl i tessl/npm-babel-plugin-transform-es2015-literalsdocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10