CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-babel--plugin-transform-parameters

Babel plugin that compiles ES2015 default and rest parameters to ES5-compatible code

91

1.02x
Overview
Eval results
Files

task.mdevals/scenario-4/

Console Statement Transformer

A Babel transformation tool that automatically adds context information to console.log statements in JavaScript code.

Objective

Create a code transformation tool that processes JavaScript files and enriches console.log statements with contextual information. When the tool encounters a console.log statement, it should prepend the function name (or "global" if not inside a function) to the logged message.

Requirements

Input Processing

The tool should accept JavaScript source code as input and return the transformed code as output.

Transformation Logic

  • Locate all console.log statements in the source code
  • Determine the enclosing function name for each console.log statement
  • If the console.log is inside a named function, prepend the function name to the first argument
  • If the console.log is at the top level (not inside any function), prepend "global" to the first argument
  • Preserve the original arguments and functionality of the console.log

Output Generation

The tool should return valid JavaScript code that can be executed. The transformed code should maintain the same behavior as the original, with the addition of context information in console.log statements.

Examples

Example 1: Function-level logging

Input:

function calculateSum(a, b) {
  console.log("Adding numbers");
  return a + b;
}

Output:

function calculateSum(a, b) {
  console.log("[calculateSum]", "Adding numbers");
  return a + b;
}

Example 2: Global-level logging

Input:

console.log("Application started");

function init() {
  console.log("Initializing");
}

Output:

console.log("[global]", "Application started");

function init() {
  console.log("[init]", "Initializing");
}

Test Cases

  • Transform a console.log inside a named function declaration and verify the function name is prepended @test
  • Transform a console.log at the global scope and verify "global" is prepended @test
  • Handle multiple console.log statements within the same function correctly @test
  • Preserve the original arguments after the prepended context @test

Implementation

@generates

API

/**
 * Transforms JavaScript source code to add context to console.log statements.
 *
 * @param {string} sourceCode - The JavaScript source code to transform
 * @returns {string} The transformed JavaScript code with enhanced console.log statements
 */
function transform(sourceCode) {
  // IMPLEMENTATION HERE
}

module.exports = { transform };

Dependencies { .dependencies }

@babel/core { .dependency }

Provides the core Babel transformation API for compiling JavaScript code.

@babel/types { .dependency }

Provides utilities for AST node manipulation and creation.

Install with Tessl CLI

npx tessl i tessl/npm-babel--plugin-transform-parameters

tile.json