or run

npx @tessl/cli init
Log in

Version

Files

docs

index.md
tile.json

task.mdevals/scenario-3/

CommonJS Export Analyzer

Build a utility that analyzes CommonJS module source code and reports information about its exports.

Requirements

Your utility should provide a function that takes CommonJS JavaScript source code as input and returns structured information about the module's exports.

The function should:

  1. Accept a string containing JavaScript source code as its first parameter
  2. Return an object with two properties:
    • namedExports: An array of strings representing the named exports found in the module
    • hasReexport: A boolean indicating whether the module re-exports another module

Expected Behavior

The analyzer should detect:

  • Direct property assignments like exports.foo = 123
  • Property assignments with bracket notation like exports['bar'] = 456
  • Module.exports property assignments like module.exports.baz = 789
  • Object literal assignments like module.exports = { x, y }
  • Re-export patterns where a module directly exports another required module

Test Cases

  • Given source code with exports.add = function() {}, the analyzer returns { namedExports: ['add'], hasReexport: false } @test
  • Given source code with exports['multiply'] = function() {}, the analyzer returns { namedExports: ['multiply'], hasReexport: false } @test
  • Given source code with module.exports.subtract = function() {}, the analyzer returns { namedExports: ['subtract'], hasReexport: false } @test
  • Given source code with module.exports = { sum, product }, the analyzer returns { namedExports: ['sum', 'product'], hasReexport: false } @test
  • Given source code with module.exports = require('./other'), the analyzer returns { namedExports: [], hasReexport: true } @test

Implementation

@generates

API

/**
 * Analyzes CommonJS source code and extracts export information
 *
 * @param {string} source - The JavaScript source code to analyze
 * @returns {Object} An object with namedExports (array) and hasReexport (boolean)
 */
function analyzeExports(source) {
  // Implementation here
}

module.exports = { analyzeExports };

Dependencies { .dependencies }

cjs-module-lexer { .dependency }

Provides static analysis of CommonJS modules to detect exports and reexports.