CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-typescript-eslint--typescript-estree

A parser that converts TypeScript source code into an ESTree compatible form

Pending
Overview
Eval results
Files

type-system.mddocs/

Type System Integration

Types and interfaces for working with ESTree-compatible TypeScript AST nodes and TypeScript integration. This module provides the complete type system for TypeScript ESTree, including node types, token types, and conversion utilities.

Capabilities

Core AST Types

Fundamental types for working with ESTree-compatible TypeScript AST nodes.

/**
 * Conditional AST type that includes optional comments and tokens based on parsing options
 */
type AST<T extends TSESTreeOptions> = 
  (T['comment'] extends true ? { comments: TSESTree.Comment[] } : {}) &
  (T['tokens'] extends true ? { tokens: TSESTree.Token[] } : {}) &
  TSESTree.Program;

/**
 * Union type of all TypeScript AST nodes  
 */
type TSNode = 
  | ts.ArrayBindingPattern
  | ts.ArrayLiteralExpression
  | ts.ArrowFunction
  | ts.AsExpression
  | ts.BinaryExpression
  | ts.Block
  | ts.CallExpression
  | ts.ClassDeclaration
  | ts.FunctionDeclaration
  | ts.Identifier
  | ts.IfStatement
  | ts.InterfaceDeclaration
  | ts.MethodDeclaration
  | ts.PropertyDeclaration
  | ts.TypeAliasDeclaration
  | ts.VariableDeclaration
  // ... and many more TypeScript node types
  ;

/**
 * TypeScript token type
 */
type TSToken = ts.Token<ts.SyntaxKind>;

/**
 * Mapping type from ESTree nodes to corresponding TypeScript nodes
 */
type TSESTreeToTSNode<T extends TSESTree.Node> = 
  T extends TSESTree.Program ? ts.SourceFile :
  T extends TSESTree.Identifier ? ts.Identifier :
  T extends TSESTree.FunctionDeclaration ? ts.FunctionDeclaration :
  // ... mappings for all node types
  ts.Node;

ESTree Node and Token Types

Complete ESTree-compatible type definitions for TypeScript AST nodes.

/**
 * ESTree AST node types enumeration
 */
enum AST_NODE_TYPES {
  ArrayExpression = 'ArrayExpression',
  ArrayPattern = 'ArrayPattern',
  ArrowFunctionExpression = 'ArrowFunctionExpression',
  AssignmentExpression = 'AssignmentExpression',
  BinaryExpression = 'BinaryExpression',
  BlockStatement = 'BlockStatement',
  CallExpression = 'CallExpression',
  ClassDeclaration = 'ClassDeclaration',
  FunctionDeclaration = 'FunctionDeclaration',
  Identifier = 'Identifier',
  IfStatement = 'IfStatement',
  Literal = 'Literal',
  Program = 'Program',
  VariableDeclaration = 'VariableDeclaration',
  // ... all ESTree node types
}

/**
 * ESTree token types enumeration
 */
enum AST_TOKEN_TYPES {
  Boolean = 'Boolean',
  Identifier = 'Identifier',
  JSXIdentifier = 'JSXIdentifier',
  JSXText = 'JSXText',
  Keyword = 'Keyword',
  Null = 'Null',
  Numeric = 'Numeric',
  Punctuator = 'Punctuator',
  RegularExpression = 'RegularExpression',
  String = 'String',
  Template = 'Template',
  Block = 'Block',
  Line = 'Line',
  PrivateIdentifier = 'PrivateIdentifier',
}

Token Conversion and Classification

Functions and types for converting and classifying tokens between TypeScript and ESTree formats.

/**
 * Converts a TypeScript token to ESTree-compatible token
 * @param token - TypeScript token to convert
 * @param ast - Source file for location calculation
 * @returns ESTree-compatible token
 */
function convertToken(token: ts.Token<ts.TokenSyntaxKind>, ast: ts.SourceFile): TSESTree.Token;

/**
 * Converts all tokens in a TypeScript source file to ESTree format
 * @param ast - TypeScript source file
 * @returns Array of ESTree-compatible tokens
 */
function convertTokens(ast: ts.SourceFile): TSESTree.Token[];

/**
 * Gets the ESTree token type for a TypeScript token
 * @param token - TypeScript token or identifier
 * @returns ESTree token type
 */
function getTokenType(token: ts.Identifier | ts.Token<ts.SyntaxKind>): AST_TOKEN_TYPES;

Usage Examples:

import { 
  parseAndGenerateServices,
  convertTokens,
  AST_NODE_TYPES,
  AST_TOKEN_TYPES
} from "@typescript-eslint/typescript-estree";

// Parse with tokens
const result = parseAndGenerateServices('const x = 42;', {
  tokens: true,
  loc: true,
  range: true
});

// Access ESTree tokens
if ('tokens' in result.ast) {
  result.ast.tokens.forEach(token => {
    console.log(`Token: ${token.type} = "${token.value}"`);
  });
}

// Check node types
result.ast.body.forEach(node => {
  if (node.type === AST_NODE_TYPES.VariableDeclaration) {
    console.log('Found variable declaration');
  }
});

TypeScript Integration Types

Types specifically for integration with TypeScript compiler API and services.

/**
 * ESTree namespace containing all TypeScript-compatible AST node interfaces
 */
declare namespace TSESTree {
  interface BaseNode {
    type: string;
    loc?: SourceLocation;
    range?: Range;
    parent?: Node;
  }
  
  interface Program extends BaseNode {
    type: AST_NODE_TYPES.Program;
    body: Statement[];
    sourceType: 'module' | 'script';
    comments?: Comment[];
    tokens?: Token[];
  }
  
  interface Identifier extends BaseNode {
    type: AST_NODE_TYPES.Identifier;
    name: string;
    typeAnnotation?: TSTypeAnnotation;
    optional?: boolean;
  }
  
  interface FunctionDeclaration extends BaseNode {
    type: AST_NODE_TYPES.FunctionDeclaration;
    id: Identifier | null;
    params: Parameter[];
    body: BlockStatement;
    generator: boolean;
    async: boolean;
    returnType?: TSTypeAnnotation;
    typeParameters?: TSTypeParameterDeclaration;
  }
  
  // ... all ESTree node interfaces with TypeScript extensions
  
  interface Token {
    type: AST_TOKEN_TYPES;
    value: string;
    range: Range;
    loc: SourceLocation;
  }
  
  interface Comment {
    type: 'Block' | 'Line';
    value: string;
    range: Range;
    loc: SourceLocation;
  }
}

Utility Types for Type Mapping

Utility types for mapping between different AST representations and type safety.

/**
 * Utility type for creating branded types (internal use)
 */
type Brand<T, B> = T & { __brand: B };

/**
 * Maps TypeScript operators to their text representations
 */
interface BinaryOperatorToText {
  [ts.SyntaxKind.AmpersandAmpersandToken]: '&&';
  [ts.SyntaxKind.BarBarToken]: '||';
  [ts.SyntaxKind.QuestionQuestionToken]: '??';
  [ts.SyntaxKind.PlusToken]: '+';
  [ts.SyntaxKind.MinusToken]: '-';
  [ts.SyntaxKind.AsteriskToken]: '*';
  [ts.SyntaxKind.SlashToken]: '/';
  // ... all binary operators
}

/**
 * Maps TypeScript assignment operators to their text representations
 */
interface AssignmentOperatorToText {
  [ts.SyntaxKind.EqualsToken]: '=';
  [ts.SyntaxKind.PlusEqualsToken]: '+=';
  [ts.SyntaxKind.MinusEqualsToken]: '-=';
  [ts.SyntaxKind.AsteriskEqualsToken]: '*=';
  // ... all assignment operators
}

/**
 * Maps TypeScript punctuator tokens to their text representations  
 */
interface PunctuatorTokenToText {
  [ts.SyntaxKind.OpenBraceToken]: '{';
  [ts.SyntaxKind.CloseBraceToken]: '}';
  [ts.SyntaxKind.OpenParenToken]: '(';
  [ts.SyntaxKind.CloseParenToken]: ')';
  // ... all punctuator tokens
}

Advanced Type Operations

Advanced types for working with optional chaining, type assertion, and other TypeScript-specific features.

/**
 * Determines if an ESTree node is a chain expression (optional chaining)
 * @param node - ESTree node to check
 * @returns True if chain expression
 */
function isChainExpression(node: TSESTree.Node): node is TSESTree.ChainExpression;

/**
 * Determines if a child node should be unwrapped from optional chain
 * @param node - Parent TypeScript node
 * @param child - Child ESTree node  
 * @returns True if should unwrap optional chain
 */
function isChildUnwrappableOptionalChain(
  node: ts.CallExpression | ts.ElementAccessExpression | ts.NonNullExpression | ts.PropertyAccessExpression,
  child: TSESTree.Node
): boolean;

/**
 * Gets TypeScript node accessibility level
 * @param node - TypeScript node to inspect
 * @returns Accessibility modifier or undefined
 */
function getTSNodeAccessibility(node: ts.Node): 'private' | 'protected' | 'public' | undefined;

/**
 * Gets variable declaration kind from TypeScript flags
 * @param node - TypeScript variable declaration list
 * @returns Declaration kind (const, let, var, using, await using)
 */
function getDeclarationKind(node: ts.VariableDeclarationList): 'const' | 'let' | 'var' | 'using' | 'await using';

Usage Examples:

import { 
  parseAndGenerateServices,
  isChainExpression,
  getTSNodeAccessibility,
  getDeclarationKind
} from "@typescript-eslint/typescript-estree";

const result = parseAndGenerateServices(`
  class Example {
    private x = 42;
    public method?(): void {}
  }
  const y = obj?.prop?.method?.();
`, { 
  project: './tsconfig.json',
  loc: true 
});

// Check for optional chaining
result.ast.body.forEach(node => {
  // Traverse to find chain expressions
  if (node.type === 'ExpressionStatement' && isChainExpression(node.expression)) {
    console.log('Found optional chaining');
  }
});

// Access TypeScript-specific information via services
if (result.services.program) {
  const classNode = result.ast.body[0];
  const tsClassNode = result.services.esTreeNodeToTSNodeMap.get(classNode);
  
  if (tsClassNode && ts.isClassDeclaration(tsClassNode)) {
    tsClassNode.members.forEach(member => {
      const accessibility = getTSNodeAccessibility(member);
      console.log(`Member accessibility: ${accessibility}`);
    });
  }
}

Version String

/**
 * Current package version string
 */
const version: string;

Usage Example:

import { version } from "@typescript-eslint/typescript-estree";

console.log(`Using @typescript-eslint/typescript-estree version ${version}`);

Complete Type Definitions

// Export all types from @typescript-eslint/types
export {
  AST_NODE_TYPES,
  AST_TOKEN_TYPES,
  TSESTree
} from '@typescript-eslint/types';

// Export enhanced types with TypeScript compiler integration
export type {
  TSNode,
  TSToken,
  TSESTreeToTSNode
};

// Re-export visitor keys
export { visitorKeys } from '@typescript-eslint/visitor-keys';

Install with Tessl CLI

npx tessl i tessl/npm-typescript-eslint--typescript-estree

docs

ast-utilities.md

configuration.md

index.md

parsing.md

program-management.md

type-system.md

tile.json