Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195/** Conditional mutations: forcing boolean contexts to constants. Mirrors Stryker's ConditionalExpression mutator: comparison and logical expressions plus if/ternary tests become `true` and `false`; loop tests become `false` only, since a forced-true loop can only end in timeout. @example ```ts conditionalReplacements({ node, parent: undefined, source }); ``` */
import { childNode, hasChildNode,} from '../node-access.ts';import type { EstreeNode, Replacement,} from '../types.ts';
/** Comparison operator tokens whose expressions get boolean forcing. */const COMPARISON_TOKENS: ReadonlySet<string> = new Set([ '==', '!=', '===', '!==', '<', '<=', '>', '>=',],);
/** Logical operator tokens whose expressions get boolean forcing. */const LOGICAL_TOKENS: ReadonlySet<string> = new Set([ '&&', '||',],);
/** Statement types whose `test` child is forced to `false` only. */const LOOP_TYPES: ReadonlySet<string> = new Set([ 'WhileStatement', 'DoWhileStatement', 'ForStatement',],);
/** Builds boolean-forcing replacements over one span. @param options - Span to force and constants to force it to. @returns One replacement per constant. @example ```ts forceBoolean({ start: 3, end: 8, texts: ['true', 'false'] }); ``` */function forceBoolean(options: { readonly start: number; readonly end: number; readonly texts: readonly string[];},): readonly Replacement[] { return options.texts .map(function toReplacement(text,): Replacement { return { start: options.start, end: options.end, text, operator: 'conditional', description: `forced condition to ${text}`, }; },);}
/** Emits boolean-forcing replacements for one node. @param options - Node under inspection with parent and source. @returns Replacements, possibly empty. @example ```ts conditionalReplacements({ node: ifStatement, parent: undefined, source }); ``` */export function conditionalReplacements(options: { readonly node: EstreeNode; readonly parent?: EstreeNode; readonly source: string;},): readonly Replacement[] { /** Operator token for expression nodes, absent on statements. */ const token = options.node .operator; /** Operator token narrowed to text for set membership checks. */ const tokenText = (typeof token) === 'string' ? token : '';
if ((options.node .type === 'BinaryExpression') && COMPARISON_TOKENS.has(tokenText,)) return forceBoolean({ start: options.node .start, end: options.node .end, texts: [ 'true', 'false', ], },);
if ((options.node .type === 'LogicalExpression') && LOGICAL_TOKENS.has(tokenText,)) return forceBoolean({ start: options.node .start, end: options.node .end, texts: [ 'true', 'false', ], },);
if ((options.node .type === 'IfStatement') || (options.node .type === 'ConditionalExpression')) { if (!hasChildNode({ node: options.node, key: 'test', },)) return [];
/** Condition expression under the statement or ternary. */ const test = childNode({ node: options.node, key: 'test', },);
return forceBoolean({ start: test.start, end: test.end, texts: [ 'true', 'false', ], },); }
if (LOOP_TYPES.has(options.node .type,)) { if (!hasChildNode({ node: options.node, key: 'test', },)) return [];
/** Loop condition expression; `for (;;)` has none. */ const test = childNode({ node: options.node, key: 'test', },);
return forceBoolean({ start: test.start, end: test.end, texts: ['false',], },); }
return [];}