CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-babel-plugin-transform-es2015-literals

Babel plugin that compiles ES2015 unicode string and number literals to ES5

86

0.97x
Overview
Eval results
Files

task.mdevals/scenario-4/

Babel AST Property Cleaner

Overview

Create a Babel plugin that transforms source code by manipulating AST node metadata. The plugin should handle numeric and string literals by removing their original source representation metadata, allowing Babel's code generator to output normalized values.

Requirements

Core Functionality

Your plugin must:

  1. Process Numeric Literals: Identify numeric literals that use non-standard prefixes (binary with 0b/0B prefix, octal with 0o/0O prefix) and remove their source metadata so they output as decimal numbers.

  2. Process String Literals: Identify string literals containing unicode escape sequences (any string with \u in its raw representation) and remove their source metadata to allow normalized output.

  3. Use Metadata Manipulation: Transform the AST by removing the metadata property that stores original source representations, rather than directly modifying node values.

  4. Preserve Semantic Values: Ensure that the computed values of all literals remain unchanged - only the output format should change.

Input/Output Examples

Binary Literals

// Input
const a = 0b1010;
const b = 0B11111111;

// Output
const a = 10;
const b = 255;

Octal Literals

// Input
const perms = 0o755;
const other = 0O644;

// Output
const perms = 493;
const other = 420;

Unicode Strings

// Input
const letter = "\u0041";
const emoji = "\u{1F600}";

// Output
const letter = "A";
const emoji = "😀";

Mixed Usage

// Input
const binary = 0b111110111;
const octal = 0o767;
const hex = 0xFF; // Should NOT be transformed
const unicode = "\u0048\u0069";

// Output
const binary = 503;
const octal = 503;
const hex = 0xFF;
const unicode = "Hi";

Implementation Guidelines

  • Your plugin should export a default function that returns a Babel plugin object
  • The plugin should declare its name as "transform-literals-test"
  • Use Babel's visitor pattern to traverse AST nodes
  • Focus on NumericLiteral and StringLiteral node types
  • Manipulate node metadata rather than rewriting nodes directly
  • Let Babel's code generator handle the actual output formatting

Test Cases

Create test cases in transform.test.js that verify:

@test Binary literal transformation

// Input code
const x = 0b1010;

// Expected output after transformation
const x = 10;

@test Octal literal transformation

// Input code
const mode = 0o755;

// Expected output after transformation
const mode = 493;

@test Unicode string normalization

// Input code
const char = "\u0041\u0042";

// Expected output after transformation
const char = "AB";

@test Hexadecimal literals remain unchanged

// Input code
const hex = 0xFF;

// Expected output after transformation (unchanged)
const hex = 0xFF;

Constraints

  • Hexadecimal literals (0x/0X prefix) should NOT be transformed as they are already ES5-compatible
  • Only transform string literals that actually contain unicode escapes (check for \u in the raw representation)
  • The transformation should not create new AST nodes - only modify existing ones
  • Maintain compatibility with Babel 7.x

Dependencies { .dependencies }

@babel/core { .dependency }

Provides the core Babel transformation and AST traversal capabilities.

@babel/helper-plugin-utils { .dependency }

Provides the declare helper for creating Babel plugins with proper API versioning.

Deliverables

  • transform-literals-plugin.js - Your Babel plugin implementation
  • transform.test.js - Test suite validating all transformation scenarios

Install with Tessl CLI

npx tessl i tessl/npm-babel-plugin-transform-es2015-literals

tile.json