Comprehensive helper functions for analyzing ESLint rule code, extracting metadata, working with AST nodes, and understanding rule structure. These utilities power the linting rules and can be used by other ESLint plugin developers.
Core functions for analyzing and extracting information from ESLint rule files.
/**
* Performs static analysis on an AST to determine rule metadata
* @param sourceCode - Object containing Program AST node and scopeManager
* @returns Rule information or null if no valid rule found
*/
declare function getRuleInfo(sourceCode: {
ast: Program;
scopeManager: Scope.ScopeManager;
}): RuleInfo | null;
/**
* Gets all identifiers referring to the context variable in a rule
* @param scopeManager - ESLint scope manager
* @param ast - Program node for the file
* @returns Set of all Identifier nodes that reference the rule context
*/
declare function getContextIdentifiers(
scopeManager: Scope.ScopeManager,
ast: Program
): Set<Identifier>;
/**
* Gets all identifiers referring to SourceCode objects
* @param scopeManager - ESLint scope manager
* @param ast - Program AST node (must have parent properties)
* @returns Set of all identifiers referring to SourceCode objects
*/
declare function getSourceCodeIdentifiers(
scopeManager: Scope.ScopeManager,
ast: Program
): Set<Identifier>;Usage Examples:
import { getRuleInfo, getContextIdentifiers } from 'eslint-plugin-eslint-plugin/lib/utils';
// Analyze a rule file
const ruleInfo = getRuleInfo({ ast: program, scopeManager });
if (ruleInfo) {
console.log('Rule type:', ruleInfo.isNewStyle ? 'object' : 'function');
console.log('Has meta:', !!ruleInfo.meta);
}
// Find all context references
const contextIds = getContextIdentifiers(scopeManager, program);
contextIds.forEach(id => {
console.log('Context used at:', id.loc);
});Functions for analyzing ESLint test files and RuleTester usage.
/**
* Performs static analysis to find test cases in RuleTester.run() calls
* @param context - Rule context for the source file
* @param ast - Program node for the file
* @returns Array of test information objects
*/
declare function getTestInfo(
context: Rule.RuleContext,
ast: Program
): TestInfo[];
interface TestInfo {
invalid: (Expression | SpreadElement | null)[];
valid: (Expression | SpreadElement | null)[];
}Usage Examples:
// In a rule implementation
create(context) {
return {
Program(node) {
const testInfo = getTestInfo(context, node);
testInfo.forEach(test => {
console.log(`Found ${test.valid.length} valid tests`);
console.log(`Found ${test.invalid.length} invalid tests`);
});
}
};
}Functions for analyzing context.report() calls and message handling.
/**
* Gets information about a context.report() call
* @param node - CallExpression AST node of context.report()
* @param context - Rule context
* @returns Report information object or null
*/
declare function getReportInfo(
node: CallExpression,
context: Rule.RuleContext
): Record<string, Property['value']> | Record<string, Expression | SpreadElement> | null;
/**
* Collects violation and suggestion data from report info
* @param reportInfo - Result from getReportInfo()
* @returns Array of violation and suggestion data
*/
declare function collectReportViolationAndSuggestionData(
reportInfo: NonNullable<ReturnType<typeof getReportInfo>>
): ViolationAndSuppressionData[];
/**
* Gets the meta.messages node from a rule
* @param ruleInfo - Rule information object
* @param scopeManager - ESLint scope manager
* @returns ObjectExpression for meta.messages or undefined
*/
declare function getMessagesNode(
ruleInfo: RuleInfo | null,
scopeManager: Scope.ScopeManager
): ObjectExpression | undefined;
/**
* Gets messageId properties from meta.messages
* @param ruleInfo - Rule information object
* @param scopeManager - ESLint scope manager
* @returns Array of messageId properties or undefined
*/
declare function getMessageIdNodes(
ruleInfo: RuleInfo,
scopeManager: Scope.ScopeManager
): (Property | SpreadElement)[] | undefined;
/**
* Gets specific messageId property by ID
* @param messageId - The messageId to find
* @param ruleInfo - Rule information object
* @param scopeManager - ESLint scope manager
* @param scope - Current scope
* @returns Matching messageId property or undefined
*/
declare function getMessageIdNodeById(
messageId: string,
ruleInfo: RuleInfo,
scopeManager: Scope.ScopeManager,
scope: Scope.Scope
): Property | undefined;Usage Examples:
// Analyze a context.report() call
if (node.type === 'CallExpression' && isContextReport(node)) {
const reportInfo = getReportInfo(node, context);
if (reportInfo) {
const violations = collectReportViolationAndSuggestionData(reportInfo);
violations.forEach(violation => {
console.log('Message ID:', violation.messageId);
console.log('Message data:', violation.data);
});
}
}
// Check rule messages
const ruleInfo = getRuleInfo({ ast, scopeManager });
const messagesNode = getMessagesNode(ruleInfo, scopeManager);
if (messagesNode) {
const messageIds = getMessageIdNodes(ruleInfo, scopeManager);
console.log(`Rule has ${messageIds?.length || 0} message IDs`);
}Functions for analyzing rule schema definitions.
/**
* Gets meta.schema property from a rule
* @param metaNode - Meta object AST node
* @param scopeManager - ESLint scope manager
* @returns Schema property or undefined
*/
declare function getMetaSchemaNode(
metaNode: Node | undefined,
scopeManager: Scope.ScopeManager
): Property | undefined;
/**
* Gets resolved schema property value
* @param schemaNode - Schema property node
* @param scopeManager - ESLint scope manager
* @returns Resolved schema node or null
*/
declare function getMetaSchemaNodeProperty(
schemaNode: AssignmentProperty | Property | undefined,
scopeManager: Scope.ScopeManager
): Node | null;Functions for analyzing meta.docs and other meta properties.
/**
* Gets meta.docs property by name
* @param propertyName - Name of the docs property to find
* @param ruleInfo - Rule information object
* @param scopeManager - ESLint scope manager
* @returns Meta docs property information
*/
declare function getMetaDocsProperty(
propertyName: string,
ruleInfo: RuleInfo,
scopeManager: Scope.ScopeManager
): MetaDocsProperty;
interface MetaDocsProperty {
docsNode: Property | undefined;
metaNode: Node | undefined;
metaPropertyNode: Property | undefined;
}
/**
* Lists all properties in an object, resolving spreads
* @param objectNode - Object expression AST node
* @param scopeManager - ESLint scope manager
* @returns Array of all properties found
*/
declare function evaluateObjectProperties(
objectNode: Node | undefined,
scopeManager: Scope.ScopeManager
): (Property | SpreadElement)[];Functions for analyzing fixer functions and auto-fix patterns.
/**
* Determines if a node represents an autofixer function
* @param node - AST node to check
* @param contextIdentifiers - Set of context identifier nodes
* @param context - Rule context
* @returns True if node is an autofixer function
*/
declare function isAutoFixerFunction(
node: Node,
contextIdentifiers: Set<Identifier>,
context: Rule.RuleContext
): node is FunctionExpression | ArrowFunctionExpression;
/**
* Determines if a node represents a suggestion fixer function
* @param node - AST node to check
* @param contextIdentifiers - Set of context identifier nodes
* @param context - Rule context
* @returns True if node is a suggestion fixer function
*/
declare function isSuggestionFixerFunction(
node: Node,
contextIdentifiers: Set<Identifier>,
context: Rule.RuleContext
): boolean;
/**
* Helper to insert properties into object literals
* @param fixer - ESLint rule fixer
* @param node - ObjectExpression to modify
* @param propertyText - Property code to insert
* @param sourceCode - ESLint SourceCode instance
* @returns Fix operation
*/
declare function insertProperty(
fixer: Rule.RuleFixer,
node: ObjectExpression,
propertyText: string,
sourceCode: SourceCode
): Rule.Fix;General utility functions for AST analysis and variable resolution.
/**
* Gets property key name if determinable statically
* @param property - Property or SpreadElement node
* @param scope - Optional scope for variable resolution
* @returns Key name string or null if not determinable
*/
declare function getKeyName(
property: Property | SpreadElement,
scope?: Scope.Scope
): string | null;
/**
* Gets the first value a variable is initialized to
* @param node - Identifier node for the variable
* @param scopeManager - ESLint scope manager
* @returns Initial value expression/function or undefined
*/
declare function findVariableValue(
node: Identifier,
scopeManager: Scope.ScopeManager
): Expression | FunctionDeclaration | undefined;
/**
* Gets all possible values a variable could be initialized to
* @param node - Identifier node for the variable
* @param scopeManager - ESLint scope manager
* @returns Array of possible initialization values
*/
declare function findPossibleVariableValues(
node: Identifier,
scopeManager: Scope.ScopeManager
): Node[];
/**
* Checks if node is an 'undefined' identifier
* @param node - AST node to check
* @returns True if node is identifier with name 'undefined'
*/
declare function isUndefinedIdentifier(node: Node): boolean;
/**
* Checks if variable comes from a function parameter
* @param node - Identifier node for the variable
* @param scopeManager - ESLint scope manager
* @returns True if variable is from function parameter
*/
declare function isVariableFromParameter(
node: Identifier,
scopeManager: Scope.ScopeManager
): boolean;Usage Examples:
// Property key analysis
const keyName = getKeyName(property, scope);
if (keyName === 'meta') {
console.log('Found meta property');
}
// Variable value resolution
const variable = findVariableValue(identifier, scopeManager);
if (variable?.type === 'ObjectExpression') {
console.log('Variable is initialized to object');
}
// Check for undefined
if (isUndefinedIdentifier(node)) {
console.log('Found undefined identifier');
}
// Parameter detection
if (isVariableFromParameter(identifier, scopeManager)) {
console.log('Variable comes from function parameter');
}Core type definitions used by utility functions:
interface RuleInfo {
create: FunctionExpression | ArrowFunctionExpression | FunctionDeclaration;
isNewStyle: boolean;
meta?: Expression | Pattern | FunctionDeclaration;
}
interface PartialRuleInfo {
create?: Node | MaybeNamedFunctionDeclaration | MaybeNamedClassDeclaration | null;
isNewStyle?: boolean;
meta?: Expression | Pattern | FunctionDeclaration;
}
interface FunctionInfo {
codePath: Rule.CodePath | null;
hasReturnWithFixer?: boolean;
hasYieldWithFixer?: boolean;
node: Node | null;
shouldCheck: boolean;
upper: FunctionInfo | null;
}
interface ViolationAndSuppressionData {
messageId?: Expression | SpreadElement | ObjectPattern | ArrayPattern | RestElement | AssignmentPattern;
message?: Expression | SpreadElement | ObjectPattern | ArrayPattern | RestElement | AssignmentPattern;
data?: Expression | SpreadElement | ObjectPattern | ArrayPattern | RestElement | AssignmentPattern;
fix?: Expression | SpreadElement | ObjectPattern | ArrayPattern | RestElement | AssignmentPattern;
}