import type { ValidationRule, ValidationContext } from '../interfaces'; import { ValidationSeverity } from '../interfaces'; /** * Configuration options for NoUserDefinedFunctionsRule */ export interface NoUserDefinedFunctionsOptions { /** * Whether to allow arrow functions * Default: true (allows arrow functions in safe contexts like array.map) */ allowArrowFunctions?: boolean; /** * Whether to allow function expressions * Default: false (blocks all function expressions in v1) */ allowFunctionExpressions?: boolean; /** * List of allowed function names (for internal/compiler use) * Default: ['__ag_main'] (the main wrapper function) */ allowedFunctionNames?: string[]; /** * Custom error message */ message?: string; } /** * NoUserDefinedFunctionsRule - Blocks user-defined functions (v1 restriction) * * In AgentScript v1, user-defined functions are not allowed to: * - Simplify the language surface area * - Prevent recursion (which complicates resource limits) * - Keep orchestration code linear and predictable * - Reduce complexity of static analysis * * **Blocked constructs:** * ```javascript * function helper() {} // ❌ BLOCKED: function declaration * const fn = function() {}; // ❌ BLOCKED: function expression * const obj = { method() {} }; // ❌ BLOCKED: method definition * class Foo { method() {} } // ❌ BLOCKED: class method * ``` * * **Allowed constructs (default):** * ```javascript * const fn = () => {}; // ✅ OK: arrow function (for callbacks) * array.map(x => x * 2); // ✅ OK: arrow in array method * array.filter(x => x > 0); // ✅ OK: arrow in array method * * // Internal compiler wrapper (whitelisted) * async function __ag_main() {} // ✅ OK: internal compiler function * ``` * * **Rationale:** * - Arrow functions are allowed because they're commonly used in array methods * (map, filter, reduce) which are essential for data manipulation * - Arrow functions don't have their own `this` binding, making them safer * - Full function declarations enable recursion and complex control flow, * which makes resource limiting harder * * @example * ```typescript * // Strict v1: No functions at all * const rule = new NoUserDefinedFunctionsRule({ * allowArrowFunctions: false, * }); * * // Permissive v1: Allow arrows for array methods * const rule = new NoUserDefinedFunctionsRule({ * allowArrowFunctions: true, * }); * ``` */ export declare class NoUserDefinedFunctionsRule implements ValidationRule { readonly name = "no-user-functions"; readonly description = "Blocks user-defined functions (AgentScript v1 restriction)"; readonly defaultSeverity = ValidationSeverity.ERROR; readonly enabledByDefault = true; private readonly allowArrowFunctions; private readonly allowFunctionExpressions; private readonly allowedFunctionNames; private readonly customMessage?; constructor(options?: NoUserDefinedFunctionsOptions); validate(context: ValidationContext): void; }