Utility functions for working with TypeScript's API, providing comprehensive tools for analyzing and manipulating TypeScript AST nodes, types, and compiler APIs.
79
A utility that analyzes TypeScript source code to identify specific patterns related to property definitions and const contexts.
Analyzes TypeScript code and finds all Object.defineProperty() calls that can be statically analyzed (bindable calls).
Object.defineProperty(obj, 'prop', { value: 42 }), the analyzer returns information indicating this is a bindable property definition call @testObject.defineProperty(obj, computedName, descriptor), the analyzer correctly identifies this is not a bindable call @testObject.defineProperty() calls, the analyzer returns an empty result @testAnalyzes TypeScript code to detect expressions that are in const contexts (e.g., as const assertions, const type parameters).
const obj = { x: 10 } as const, the analyzer identifies the object literal as being in a const context @testconst arr = [1, 2, 3] (without as const), the analyzer correctly identifies the array literal is not in a const context @testfunction f<T extends readonly string[]>(x: T), the analyzer identifies expressions bound to T as being in a const context @test@generates
/**
* Result of analyzing a bindable Object.defineProperty call
*/
export interface BindablePropertyInfo {
/** The line number where the call occurs */
line: number;
/** The property name being defined (if statically determinable) */
propertyName?: string;
/** Whether this is a bindable (statically analyzable) call */
isBindable: boolean;
}
/**
* Result of analyzing a const context
*/
export interface ConstContextInfo {
/** The line number where the expression occurs */
line: number;
/** The type of expression (e.g., 'ObjectLiteral', 'ArrayLiteral') */
expressionType: string;
/** Whether the expression is in a const context */
isConst: boolean;
}
/**
* Overall analysis result
*/
export interface AnalysisResult {
/** Information about Object.defineProperty calls found */
propertyDefinitions: BindablePropertyInfo[];
/** Information about const contexts found */
constContexts: ConstContextInfo[];
}
/**
* Analyzes TypeScript source code to identify bindable property definitions
* and const assertion contexts.
*
* @param sourceCode - The TypeScript source code to analyze as a string
* @returns An AnalysisResult containing information about property definitions and const contexts
*/
export function analyzeCode(sourceCode: string): AnalysisResult;Provides TypeScript API utilities for AST analysis, specifically for detecting bindable Object.defineProperty calls and const contexts.
Provides the TypeScript compiler API for parsing and analyzing TypeScript source code.
Install with Tessl CLI
npx tessl i tessl/npm-ts-api-utilsevals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10