CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-istanbul-lib-source-maps

Source maps support for Istanbul code coverage toolkit, enabling accurate coverage reporting for transpiled JavaScript code

Overall
score

98%

Overview
Eval results
Files

task.mdevals/scenario-10/

Coverage Path Normalizer

Build a utility that normalizes and resolves file paths from coverage data to ensure consistent file identification across different operating systems and environments.

Background

When processing code coverage data from different environments (Windows vs Unix, containerized vs local, relative vs absolute paths), file paths can vary significantly. Your task is to create a path normalization utility that handles these variations correctly.

Requirements

Create a module that exports a PathNormalizer class with the following functionality:

1. Path Conversion

The normalizer should convert relative paths to absolute paths based on a configurable base directory:

  • Accept a base directory in the constructor (defaults to current working directory)
  • Provide a method to convert relative paths to absolute paths
  • Handle paths that are already absolute (return them unchanged)

2. Path Validation

The normalizer should determine if a given path is absolute:

  • Support both Windows-style paths (e.g., C:\path\file.js)
  • Support Unix-style paths (e.g., /path/file.js)
  • Return boolean indicating whether the path is absolute

3. Relative Path Resolution

The normalizer should resolve paths relative to source files:

  • Accept a file path and a reference file path
  • Resolve the first path relative to the directory of the reference file
  • Handle both relative and absolute input paths correctly

4. Unique Key Generation

The normalizer should generate consistent keys for file paths:

  • Normalize path separators across platforms
  • Ensure the same file always produces the same key
  • Handle Windows and Unix path conventions

Testing

  • Converting a relative path src/utils.js with base /home/user/project produces /home/user/project/src/utils.js @test
  • Converting an absolute path /home/user/file.js with any base returns /home/user/file.js unchanged @test
  • Checking if /home/user/file.js is absolute returns true @test
  • Checking if C:\Users\file.js is absolute returns true @test
  • Checking if src/file.js is absolute returns false @test
  • Resolving ../lib/helper.js relative to /home/project/src/main.js produces /home/project/lib/helper.js @test
  • Generating unique keys for C:\project\src\file.js and /project/src/file.js produces normalized, consistent keys @test

Implementation

@generates

API

/**
 * PathNormalizer class for handling file path operations
 */
class PathNormalizer {
  /**
   * Creates a new PathNormalizer instance
   * @param {string} [baseDir] - Base directory for resolving relative paths (defaults to process.cwd())
   */
  constructor(baseDir) {
    // IMPLEMENTATION HERE
  }

  /**
   * Converts a relative path to an absolute path using the configured base directory
   * @param {string} file - The file path to convert
   * @returns {string} The absolute path
   */
  toAbsolute(file) {
    // IMPLEMENTATION HERE
  }

  /**
   * Checks if a path is absolute
   * @param {string} file - The file path to check
   * @returns {boolean} True if the path is absolute, false otherwise
   */
  isAbsolute(file) {
    // IMPLEMENTATION HERE
  }

  /**
   * Resolves a file path relative to another file
   * @param {string} file - The file path to resolve
   * @param {string} referenceFile - The reference file path
   * @returns {string} The resolved absolute path
   */
  resolveRelativeTo(file, referenceFile) {
    // IMPLEMENTATION HERE
  }

  /**
   * Generates a normalized unique key for a pathname
   * @param {string} pathname - The file path
   * @returns {string} A normalized key for deduplication
   */
  getUniqueKey(pathname) {
    // IMPLEMENTATION HERE
  }
}

module.exports = { PathNormalizer };

Dependencies { .dependencies }

istanbul-lib-source-maps { .dependency }

Provides path resolution utilities for handling file paths across different platforms and environments.

Install with Tessl CLI

npx tessl i tessl/npm-istanbul-lib-source-maps

tile.json