or run

npx @tessl/cli init
Log in

Version

Files

docs

index.md
tile.json

task.mdevals/scenario-8/

CommonJS Module Analyzer

Build a tool that analyzes CommonJS module source code and reports on the module's exported names and any module reexports it contains.

Requirements

Your tool should provide a function that:

  1. Accepts JavaScript source code as a string input
  2. Analyzes the source code to extract information about exports
  3. Returns an object containing:
    • An array of named export identifiers found in the module
    • An array of module specifiers that are being reexported (e.g., from patterns like module.exports = require('./other'))

The tool should handle various CommonJS export patterns including:

  • Direct property assignments: exports.foo = ...
  • Module.exports assignments: module.exports.bar = ...
  • Object literal exports: module.exports = { a, b, c }
  • Reexport patterns: module.exports = require('./another-module')

Test Cases

  • Given source code "exports.foo = 'bar';", the function returns { exports: ['foo'], reexports: [] } @test
  • Given source code "module.exports = { a: 1, b: 2 };", the function returns an object with exports containing 'a' and 'b' @test
  • Given source code "module.exports = require('./other');", the function returns an object with reexports containing './other' @test
  • Given invalid JavaScript source code (syntax errors), the function handles the error gracefully @test

Implementation

@generates

API

/**
 * Analyzes CommonJS module source code and extracts export information
 * @param {string} sourceCode - The JavaScript source code to analyze
 * @returns {Promise<{exports: string[], reexports: string[]}>} Object containing arrays of exports and reexports
 */
async function analyzeModule(sourceCode) {
  // Implementation here
}

module.exports = { analyzeModule };

Dependencies { .dependencies }

cjs-module-lexer { .dependency }

Provides static analysis capabilities for CommonJS modules to detect named exports and reexports.

@satisfied-by