or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

ast-utils.mdindex.mdpattern-matching.mdreference-tracking.mdstatic-analysis.mdtoken-predicates.md
tile.json

token-predicates.mddocs/

Token Predicates

Comprehensive collection of token type checking functions for identifying specific punctuation and syntax elements. These predicate functions are essential for rules that need precise token analysis and manipulation.

Capabilities

Positive Token Predicates

Functions that check if a token matches a specific type.

/**
 * Check if the given token is an arrow token (=>)
 * @param token - The token to check
 * @returns True if the token is an arrow token
 */
function isArrowToken(token: Token): boolean;

/**
 * Check if the given token is a comma token (,)
 * @param token - The token to check
 * @returns True if the token is a comma token
 */
function isCommaToken(token: Token): boolean;

/**
 * Check if the given token is a semicolon token (;)
 * @param token - The token to check
 * @returns True if the token is a semicolon token
 */
function isSemicolonToken(token: Token): boolean;

/**
 * Check if the given token is a colon token (:)
 * @param token - The token to check
 * @returns True if the token is a colon token
 */
function isColonToken(token: Token): boolean;

/**
 * Check if the given token is an opening parenthesis token (()
 * @param token - The token to check
 * @returns True if the token is an opening parenthesis token
 */
function isOpeningParenToken(token: Token): boolean;

/**
 * Check if the given token is a closing parenthesis token ())
 * @param token - The token to check
 * @returns True if the token is a closing parenthesis token
 */
function isClosingParenToken(token: Token): boolean;

/**
 * Check if the given token is an opening square bracket token ([)
 * @param token - The token to check
 * @returns True if the token is an opening square bracket token
 */
function isOpeningBracketToken(token: Token): boolean;

/**
 * Check if the given token is a closing square bracket token (])
 * @param token - The token to check
 * @returns True if the token is a closing square bracket token
 */
function isClosingBracketToken(token: Token): boolean;

/**
 * Check if the given token is an opening brace token ({)
 * @param token - The token to check
 * @returns True if the token is an opening brace token
 */
function isOpeningBraceToken(token: Token): boolean;

/**
 * Check if the given token is a closing brace token (})
 * @param token - The token to check
 * @returns True if the token is a closing brace token
 */
function isClosingBraceToken(token: Token): boolean;

/**
 * Check if the given token is a comment token (Block, Line, or Shebang)
 * @param token - The token to check
 * @returns True if the token is a comment token
 */
function isCommentToken(token: Token): boolean;

Negative Token Predicates

Functions that check if a token does NOT match a specific type.

/**
 * Check if the given token is NOT an arrow token
 * @param token - The token to check
 * @returns True if the token is not an arrow token
 */
function isNotArrowToken(token: Token): boolean;

/**
 * Check if the given token is NOT a comma token
 * @param token - The token to check
 * @returns True if the token is not a comma token
 */
function isNotCommaToken(token: Token): boolean;

/**
 * Check if the given token is NOT a semicolon token
 * @param token - The token to check
 * @returns True if the token is not a semicolon token
 */
function isNotSemicolonToken(token: Token): boolean;

/**
 * Check if the given token is NOT a colon token
 * @param token - The token to check
 * @returns True if the token is not a colon token
 */
function isNotColonToken(token: Token): boolean;

/**
 * Check if the given token is NOT an opening parenthesis token
 * @param token - The token to check
 * @returns True if the token is not an opening parenthesis token
 */
function isNotOpeningParenToken(token: Token): boolean;

/**
 * Check if the given token is NOT a closing parenthesis token
 * @param token - The token to check
 * @returns True if the token is not a closing parenthesis token
 */
function isNotClosingParenToken(token: Token): boolean;

/**
 * Check if the given token is NOT an opening square bracket token
 * @param token - The token to check
 * @returns True if the token is not an opening square bracket token
 */
function isNotOpeningBracketToken(token: Token): boolean;

/**
 * Check if the given token is NOT a closing square bracket token
 * @param token - The token to check
 * @returns True if the token is not a closing square bracket token
 */
function isNotClosingBracketToken(token: Token): boolean;

/**
 * Check if the given token is NOT an opening brace token
 * @param token - The token to check
 * @returns True if the token is not an opening brace token
 */
function isNotOpeningBraceToken(token: Token): boolean;

/**
 * Check if the given token is NOT a closing brace token
 * @param token - The token to check
 * @returns True if the token is not a closing brace token
 */
function isNotClosingBraceToken(token: Token): boolean;

/**
 * Check if the given token is NOT a comment token
 * @param token - The token to check
 * @returns True if the token is not a comment token
 */
function isNotCommentToken(token: Token): boolean;

Usage Examples

Basic Token Filtering

Use predicates to find specific tokens in source code.

import { 
  isArrowToken, 
  isCommaToken, 
  isSemicolonToken,
  isOpeningParenToken,
  isClosingParenToken
} from "eslint-utils";

create(context) {
  const sourceCode = context.getSourceCode();
  
  return {
    ArrowFunctionExpression(node) {
      // Find the arrow token
      const arrowToken = sourceCode.getTokenBefore(node.body, isArrowToken);
      if (arrowToken) {
        console.log('Arrow token found at:', arrowToken.loc);
      }
    },
    
    FunctionDeclaration(node) {
      // Find opening and closing parentheses
      const openParen = sourceCode.getFirstToken(node, isOpeningParenToken);
      const closeParen = sourceCode.getTokenAfter(openParen, isClosingParenToken);
      
      console.log('Function parentheses:', openParen.loc, closeParen.loc);
    }
  };
}

Token Manipulation with Negated Predicates

Use negative predicates to skip certain tokens.

import { 
  isNotCommentToken, 
  isNotCommaToken,
  isCommaToken 
} from "eslint-utils";

create(context) {
  const sourceCode = context.getSourceCode();
  
  return {
    ArrayExpression(node) {
      // Get all non-comment tokens in the array
      const tokens = sourceCode.getTokens(node, isNotCommentToken);
      
      // Find commas, skipping comments
      const commas = sourceCode.getTokens(node, isCommaToken);
      
      if (commas.length !== node.elements.length - 1) {
        context.report(node, 'Unexpected comma count in array');
      }
    },
    
    ObjectExpression(node) {
      // Get the first non-comment token after opening brace
      const firstToken = sourceCode.getTokenAfter(
        sourceCode.getFirstToken(node), 
        isNotCommentToken
      );
      
      console.log('First non-comment token:', firstToken?.value);
    }
  };
}

Advanced Token Analysis

Combine multiple predicates for complex token analysis.

import { 
  isOpeningParenToken,
  isClosingParenToken,
  isCommaToken,
  isSemicolonToken,
  isNotCommentToken
} from "eslint-utils";

create(context) {
  const sourceCode = context.getSourceCode();
  
  function analyzeParameterList(node) {
    const openParen = sourceCode.getFirstToken(node, isOpeningParenToken);
    const closeParen = sourceCode.getTokenAfter(openParen, isClosingParenToken);
    
    if (!openParen || !closeParen) return;
    
    // Get all tokens between parentheses, excluding comments
    const innerTokens = sourceCode.getTokensBetween(
      openParen, 
      closeParen, 
      isNotCommentToken
    );
    
    // Count commas to determine parameter count
    const commas = innerTokens.filter(isCommaToken);
    const paramCount = commas.length > 0 ? commas.length + 1 : 
                     innerTokens.length > 0 ? 1 : 0;
    
    return {
      paramCount,
      hasTrailingComma: commas.length > 0 && 
                       isCommaToken(innerTokens[innerTokens.length - 1])
    };
  }
  
  return {
    FunctionDeclaration(node) {
      const analysis = analyzeParameterList(node);
      if (analysis?.hasTrailingComma) {
        context.report(node, 'Trailing comma in parameter list');
      }
    }
  };
}

Stylistic Rules with Token Predicates

Create rules for code style enforcement.

import { 
  isSemicolonToken,
  isNotSemicolonToken,
  isCommaToken,
  isArrowToken
} from "eslint-utils";

create(context) {
  const sourceCode = context.getSourceCode();
  
  return {
    ExpressionStatement(node) {
      // Check for missing semicolons
      const lastToken = sourceCode.getLastToken(node);
      if (!isSemicolonToken(lastToken)) {
        context.report({
          node,
          message: 'Missing semicolon',
          fix(fixer) {
            return fixer.insertTextAfter(node, ';');
          }
        });
      }
    },
    
    ArrowFunctionExpression(node) {
      // Ensure space around arrow token
      const arrowToken = sourceCode.getTokenBefore(node.body, isArrowToken);
      const beforeArrow = sourceCode.getTokenBefore(arrowToken);
      const afterArrow = sourceCode.getTokenAfter(arrowToken);
      
      if (beforeArrow && beforeArrow.range[1] === arrowToken.range[0]) {
        context.report({
          node: arrowToken,
          message: 'Missing space before arrow',
          fix(fixer) {
            return fixer.insertTextBefore(arrowToken, ' ');
          }
        });
      }
      
      if (afterArrow && arrowToken.range[1] === afterArrow.range[0]) {
        context.report({
          node: arrowToken,
          message: 'Missing space after arrow',
          fix(fixer) {
            return fixer.insertTextAfter(arrowToken, ' ');
          }
        });
      }
    }
  };
}

Token-based AST Traversal

Use predicates to navigate between related tokens.

import { 
  isOpeningBraceToken,
  isClosingBraceToken,
  isOpeningParenToken,
  isClosingParenToken,
  isCommentToken
} from "eslint-utils";

create(context) {
  const sourceCode = context.getSourceCode();
  
  function findMatchingBrace(openBrace) {
    let depth = 1;
    let current = openBrace;
    
    while (depth > 0 && current) {
      current = sourceCode.getTokenAfter(current);
      if (!current) break;
      
      if (isOpeningBraceToken(current)) {
        depth++;
      } else if (isClosingBraceToken(current)) {
        depth--;
      }
    }
    
    return current;
  }
  
  function findMatchingParen(openParen) {
    let depth = 1;
    let current = openParen;
    
    while (depth > 0 && current) {
      current = sourceCode.getTokenAfter(current);
      if (!current) break;
      
      if (isOpeningParenToken(current)) {
        depth++;
      } else if (isClosingParenToken(current)) {
        depth--;
      }
    }
    
    return current;
  }
  
  return {
    BlockStatement(node) {
      const openBrace = sourceCode.getFirstToken(node);
      const closeBrace = findMatchingBrace(openBrace);
      
      console.log('Block spans from', openBrace.loc, 'to', closeBrace?.loc);
    },
    
    CallExpression(node) {
      const openParen = sourceCode.getTokenAfter(node.callee, isOpeningParenToken);
      if (openParen) {
        const closeParen = findMatchingParen(openParen);
        console.log('Call parentheses:', openParen.loc, closeParen?.loc);
      }
    }
  };
}

Token Type Reference

The predicate functions work with ESLint's token objects:

interface Token {
  type: "Punctuator" | "Identifier" | "Literal" | "Keyword" | "Block" | "Line" | "Shebang";
  value: string;
  loc: SourceLocation;
  range: [number, number];
}

Supported Punctuator Values

  • => - Arrow token
  • , - Comma token
  • ; - Semicolon token
  • : - Colon token
  • ( - Opening parenthesis token
  • ) - Closing parenthesis token
  • [ - Opening bracket token
  • ] - Closing bracket token
  • { - Opening brace token
  • } - Closing brace token

Comment Token Types

  • Block - Block comments (/* */)
  • Line - Line comments (//)
  • Shebang - Shebang comments (#!/usr/bin/env node)