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-9/

Console Logger Transformer

Build a JavaScript code transformation tool that automatically instruments code with logging statements for debugging purposes.

Objective

Create a tool that transforms JavaScript source code by inserting console.log statements at key points in the code. The tool should parse JavaScript code, traverse its AST (Abstract Syntax Tree), and inject logging statements before function declarations and return statements.

@generates

Requirements

Your implementation should:

  1. Parse JavaScript source code into an AST
  2. Traverse the AST to find function declarations and return statements
  3. Insert console.log statements at the beginning of each function body to log "Entering function: [functionName]"
  4. Insert console.log statements before each return statement to log "Returning from [functionName]"
  5. Generate the transformed JavaScript code from the modified AST

API

/**
 * Transforms JavaScript source code by adding console.log statements
 * @param {string} sourceCode - The JavaScript source code to transform
 * @returns {string} The transformed JavaScript code with added logging
 */
function instrumentCode(sourceCode);

module.exports = { instrumentCode };

Test Cases

Basic Function Instrumentation

  • Given source code function add(a, b) { return a + b; }, the transformed code contains console.log('Entering function: add') as the first statement in the function body @test

  • Given source code function getValue() { return 42; }, the transformed code contains both console.log('Entering function: getValue') at the start and console.log('Returning from getValue') before the return statement @test

Multiple Functions

  • Given source code with two functions function foo() { return 1; } function bar() { return 2; }, both functions are instrumented with their respective entering and returning logs @test

Dependencies { .dependencies }

@babel/parser { .dependency }

Provides JavaScript parsing capabilities to convert source code into an AST.

@satisfied-by

@babel/traverse { .dependency }

Provides AST traversal capabilities using the visitor pattern to locate and modify specific node types.

@satisfied-by

@babel/generator { .dependency }

Converts the modified AST back into JavaScript source code.

@satisfied-by

@babel/types { .dependency }

Provides utilities for creating and manipulating AST nodes.

@satisfied-by

Install with Tessl CLI

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

tile.json