or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.mdmigration-system.mdproject-configuration.mdproject-generation.mdutility-functions.mdworkspace-setup.md
tile.json

utility-functions.mddocs/

Utility Functions

This capability provides helper functions for custom schematics and ESLint configuration management. These utilities are primarily used internally by the schematics but are also available for use in custom schematics.

Capabilities

JSON File Operations

Utilities for reading and updating JSON files within the schematic tree.

/**
 * Read and parse JSON files from Tree with comment stripping
 * @param host - The file tree
 * @param path - Path to the JSON file
 * @returns Parsed JSON object
 */
function readJsonInTree<T>(host: Tree, path: string): T;

/**
 * Update JSON files in Tree using callback function
 * @param path - Path to the JSON file
 * @param callback - Function that receives JSON and returns updated content
 * @returns Schematic rule that performs the update
 */
function updateJsonInTree<T, O>(
  path: string, 
  callback: (json: T, context: SchematicContext) => O
): Rule;

Usage Examples:

import { readJsonInTree, updateJsonInTree } from "@angular-eslint/schematics";

// Read package.json
const packageJson = readJsonInTree(tree, 'package.json');

// Update angular.json
const updateRule = updateJsonInTree('angular.json', (angularJson) => {
  angularJson.projects = angularJson.projects || {};
  return angularJson;
});

ESLint Configuration Generation

Functions for creating and managing ESLint configuration files.

/**
 * Generate root ESLint configuration object (legacy format)
 * @param prefix - Component/directive prefix for Angular rules
 * @returns ESLint configuration object
 */
function createRootESLintConfig(prefix: string | null): Record<string, unknown>;

/**
 * Generate root ESLint flat config as string
 * @param prefix - Component/directive prefix for Angular rules  
 * @param isESM - Whether to generate ES module format
 * @returns ESLint flat config string
 */
function createStringifiedRootESLintConfig(prefix: string | null, isESM: boolean): string;

/**
 * Create ESLint config for specific project
 * @param projectName - Name of the project
 * @param setParserOptionsProject - Whether to enable parserOptions.project
 * @returns Schematic rule that creates the config
 */
function createESLintConfigForProject(
  projectName: string, 
  setParserOptionsProject: boolean
): Rule;

Project Management

Functions for managing Angular project configurations and targets.

/**
 * Add ESLint lint target to Angular project configuration
 * @param projectName - Name of the project
 * @param targetName - Name for the lint target
 * @returns Schematic rule that adds the target
 */
function addESLintTargetToProject(
  projectName: string, 
  targetName: 'eslint' | 'lint'
): Rule;

/**
 * Determine target project name when not explicitly specified
 * @param tree - The file tree
 * @param maybeProject - Optional project name hint
 * @returns Project name or null if not determinable
 */
function determineTargetProjectName(tree: Tree, maybeProject?: string): string | null;

/**
 * Extract targets/architect config from project configuration
 * @param projectConfig - Project configuration object with architect/targets
 * @returns TargetsConfig or null if not found
 */
function getTargetsConfigFromProject(projectConfig: any): TargetsConfig | null;

Configuration Detection

Functions for detecting and handling different ESLint configuration formats.

/**
 * Determine whether to use ESLint flat config or legacy eslintrc
 * @param tree - The file tree
 * @param existingJson - Optional existing configuration to check
 * @returns true if flat config should be used
 */
function shouldUseFlatConfig(tree: Tree, existingJson?: Record<string, unknown>): boolean;

/**
 * Find existing root ESLint config file path
 * @param tree - The file tree
 * @returns Config file path or null if not found
 */
function resolveRootESLintConfigPath(tree: Tree): string | null;

/**
 * Determine config format and extension for new project configs
 * @param tree - The file tree
 * @param rootConfigPath - Path to root configuration file
 * @param projectRoot - Root path of the project
 * @returns Configuration format details
 */
function determineNewProjectESLintConfigContentAndExtension(
  tree: Tree, 
  rootConfigPath: string, 
  projectRoot: string
): { isESM: boolean; ext: string };

File Processing

Utilities for processing files in the schematic tree.

/**
 * Apply visitor function to all non-gitignored files
 * @param visitor - Function to apply to each file
 * @param dir - Optional directory to start from
 * @returns Schematic rule that applies the visitor
 */
function visitNotIgnoredFiles(
  visitor: (file: Path, host: Tree, context: SchematicContext) => void | Rule,
  dir?: Path
): Rule;

Angular.json Management

Functions for updating Angular workspace configuration.

/**
 * Update schematicCollections in angular.json
 * @param angularJson - Angular workspace configuration
 * @param collectionName - Name of the schematic collection
 * @returns Updated angularJson object
 */
function updateSchematicCollections(
  angularJson: Record<string, any>,
  collectionName: '@angular-eslint/schematics' | 'angular-eslint'
): Record<string, any>;

/**
 * Update schematic default values in angular.json
 * @param angularJson - Angular workspace configuration
 * @param schematicFullName - Full name of the schematic
 * @param defaultValues - Default values to set
 * @returns Updated angularJson object
 */
function updateSchematicDefaults(
  angularJson: Record<string, any>,
  schematicFullName: string,
  defaultValues: Record<string, unknown>
): Record<string, any>;

Utility Helpers

General utility functions for data manipulation.

/**
 * Sort object properties alphabetically by key
 * @param obj - Object to sort
 * @returns New object with sorted properties
 */
function sortObjectByKeys(obj: Record<string, unknown>): Record<string, unknown>;

Root Configuration Generation

Functions for generating workspace-level ESLint configurations.

/**
 * Create root ESLint configuration object (legacy format)
 * @param prefix - Component/directive prefix for Angular rules, null for no prefix rules
 * @returns ESLint configuration object for legacy .eslintrc.json format
 */
function createRootESLintConfig(prefix: string | null): Record<string, unknown>;

/**
 * Create stringified root ESLint flat config
 * @param prefix - Component/directive prefix for Angular rules, null for no prefix rules  
 * @param isESM - Whether to generate ES module format (import/export) or CommonJS (require/module.exports)
 * @returns Complete flat config file content as string
 */
function createStringifiedRootESLintConfig(prefix: string | null, isESM: boolean): string;

Project Target Management

Functions for extracting and working with Angular project targets.

/**
 * Extract targets (architect) configuration from Angular project config
 * @param projectConfig - Project configuration object that may have architect or targets
 * @returns TargetsConfig object or null if neither architect nor targets found
 */
function getTargetsConfigFromProject(
  projectConfig: { architect?: TargetsConfig } & { targets?: TargetsConfig }
): TargetsConfig | null;

DevKit Imports

Re-exported functions and types from Angular DevKit and Nx DevKit for convenience.

// Nx DevKit re-exports
/**
 * Convert an Nx generator to an Angular DevKit schematic
 * @param generator - The Nx generator to convert  
 * @returns Angular DevKit schematic
 */
function convertNxGenerator(generator: any): any;

/**
 * Calculate relative path from project root to workspace root
 * @param projectRoot - Path to project root
 * @returns Relative path string with trailing slashes
 */
function offsetFromRoot(projectRoot: string): string;

/**
 * Read and parse JSON file from Nx DevKit tree
 * @param tree - The Nx DevKit tree
 * @param path - Path to JSON file
 * @returns Parsed JSON object
 */
function readJson<T = any>(tree: Tree, path: string): T;

/**
 * Write JSON object to file in Nx DevKit tree
 * @param tree - The Nx DevKit tree
 * @param path - Path where to write the JSON file
 * @param value - JSON object to write
 */
function writeJson<T = object>(tree: Tree, path: string, value: T): void;

/**
 * Wrap Angular DevKit schematic for use with Nx
 * @param collectionName - Name of the schematic collection
 * @param generatorName - Name of the generator/schematic
 * @returns Wrapped schematic function
 */
function wrapAngularDevkitSchematic(collectionName: string, generatorName: string): any;

// Type re-exports
type ProjectConfiguration = import("@nx/devkit").ProjectConfiguration;
type Tree = import("@nx/devkit").Tree;

Migration Utilities

Functions specifically for handling package migrations.

/**
 * Update package.json dependencies during migrations
 * @param dependencies - Array of package and version updates
 * @returns Schematic rule that updates dependencies
 */
function updateDependencies(
  dependencies: Array<{packageName: string, version: string}>
): Rule;

Usage Example:

import { updateDependencies } from "@angular-eslint/schematics";

const rule = updateDependencies([
  { packageName: '@angular-eslint/eslint-plugin', version: '^20.0.0' },
  { packageName: 'eslint', version: '^9.0.0' }
]);

Constants

const supportedFlatConfigNames: string[] = [
  'eslint.config.js',
  'eslint.config.mjs', 
  'eslint.config.cjs',
  'eslint.config.ts',
  'eslint.config.mts',
  'eslint.config.cts'
];

Types

type TargetsConfig = Record<string, { builder: string; options: unknown }>;
type ProjectType = 'application' | 'library';
type Rule = import("@angular-devkit/schematics").Rule;
type Tree = import("@angular-devkit/schematics").Tree;
type SchematicContext = import("@angular-devkit/schematics").SchematicContext;
type Path = import("@angular-devkit/core").Path;
type ProjectConfiguration = import("@nx/devkit").ProjectConfiguration;