CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-ts-api-utils

Utility functions for working with TypeScript's API, providing comprehensive tools for analyzing and manipulating TypeScript AST nodes, types, and compiler APIs.

79

1.97x
Overview
Eval results
Files

task.mdevals/scenario-4/

TypeScript Scope Analyzer

Build a tool that analyzes TypeScript source code to identify and report all scope boundaries in the code. The tool should parse TypeScript files and determine which AST nodes create new function scopes, helping developers understand the scoping structure of their code.

Requirements

Your tool should:

  1. Accept a TypeScript source file path as input
  2. Parse the TypeScript file and traverse its AST
  3. Identify all nodes that create function scope boundaries
  4. Output a report containing:
    • The line number of each scope boundary
    • The type of construct that creates the scope (e.g., "FunctionDeclaration", "ArrowFunction", "ClassDeclaration")
    • A snippet of the code at that location

Output Format

The output should be a JSON array where each entry contains:

  • line: The line number where the scope boundary starts
  • kind: The syntax kind name of the node
  • text: A brief excerpt of the code (first 50 characters)

Example output:

[
  {"line": 3, "kind": "FunctionDeclaration", "text": "function calculate(x: number): number {"},
  {"line": 8, "kind": "ArrowFunction", "text": "const handler = () => {"},
  {"line": 12, "kind": "ClassDeclaration", "text": "class MyClass {"}
]

Implementation

Create two files:

  1. src/analyzer.ts - Contains the main analysis logic
  2. test/analyzer.test.ts - Contains tests for the analyzer

Test Cases

Your implementation should pass the following tests:

  • Given a file with a function declaration, the analyzer identifies it as a scope boundary @test
  • Given a file with an arrow function, the analyzer identifies it as a scope boundary @test
  • Given a file with a class declaration, the analyzer identifies it as a scope boundary @test
  • Given a file with a method inside a class, the analyzer identifies it as a scope boundary @test
  • Given a file with an enum declaration, the analyzer identifies it as a scope boundary @test
  • Given a file with multiple nested scopes, the analyzer identifies all scope boundaries correctly @test

@generates

API

/**
 * Analyzes a TypeScript file and identifies all function scope boundaries.
 *
 * @param filePath - Path to the TypeScript file to analyze
 * @returns Array of scope boundary information
 */
export function analyzeScopeBoundaries(filePath: string): ScopeBoundary[];

/**
 * Information about a scope boundary in the source code.
 */
export interface ScopeBoundary {
  /** Line number where the scope boundary starts (1-indexed) */
  line: number;
  /** The TypeScript syntax kind name of the node */
  kind: string;
  /** A brief excerpt of the code at this location */
  text: string;
}

Dependencies { .dependencies }

TypeScript { .dependency }

Provides the TypeScript compiler API for parsing and analyzing TypeScript code.

@satisfied-by

ts-api-utils { .dependency }

Provides utility functions for working with TypeScript's AST, particularly for detecting scope boundaries.

@satisfied-by

Install with Tessl CLI

npx tessl i tessl/npm-ts-api-utils

tile.json