CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-babel-plugin-transform-eval

Babel plugin that transforms eval() calls containing string literals by parsing and compiling the string content at transform time

Overall
score

98%

Overview
Eval results
Files

task.mdevals/scenario-9/

Function Call Analyzer

Build a JavaScript code analyzer that counts and categorizes function calls in JavaScript source code. The analyzer should traverse the code's structure and provide statistics about function usage patterns.

Requirements

Your analyzer should:

  1. Parse JavaScript source code and analyze all function calls
  2. Count total function calls in the code
  3. Identify and list unique function names that are called
  4. Find the locations (line and column numbers) of each function call
  5. Export an analysis function that takes a string of JavaScript code and returns an analysis object

The analysis should handle:

  • Regular function calls like foo()
  • Method calls like obj.method()
  • Nested function calls like outer(inner())
  • Built-in functions like console.log()

Output Format

The analyzer should return an object with this structure:

{
  totalCalls: <number>,           // Total count of all function calls
  uniqueFunctions: [<string>],    // Array of unique function names called
  callLocations: [                // Array of objects with location info
    {
      name: <string>,             // Function name
      line: <number>,             // Line number where call appears
      column: <number>            // Column number where call starts
    }
  ]
}

Test Cases

  • Given code "foo(); bar(); foo();", the analyzer returns totalCalls of 3, uniqueFunctions of ["foo", "bar"], and callLocations with 3 entries @test
  • Given code "console.log('hello'); Math.max(1, 2);", the analyzer identifies method calls and returns correct counts and names @test
  • Given code "outer(inner());", the analyzer counts both the outer and inner function calls @test
  • Given an empty string, the analyzer returns totalCalls of 0, empty uniqueFunctions array, and empty callLocations array @test

Implementation

@generates

API

/**
 * Analyzes JavaScript code to count and locate function calls
 * @param {string} code - JavaScript source code to analyze
 * @returns {Object} Analysis object with totalCalls, uniqueFunctions, and callLocations
 */
function analyzeFunctionCalls(code) {
  // Implementation here
}

module.exports = { analyzeFunctionCalls };

Dependencies { .dependencies }

@babel/parser { .dependency }

Parses JavaScript code into an Abstract Syntax Tree (AST) for analysis.

@satisfied-by

@babel/traverse { .dependency }

Provides AST traversal capabilities using the visitor pattern to navigate and analyze the parsed code structure.

@satisfied-by

Install with Tessl CLI

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

tile.json