import type { TSESTree } from '@typescript-eslint/types'; import type { Value } from '../../sourcegen/index.js'; import { type Node, type ParsedSource, type SourceToken, type Splice } from './source-file.js'; /** * Literal edits of the options object of a construct call: where a path * (`['request', 'url']`) lands in the object, what may be replaced there, * and how a plain value is rendered in the file's own style. * * A position that exists and holds a plain literal may be replaced; a key * the innermost object lacks is inserted after the object's last member; * anything else — a variable, a spread, a missing parent — is refused, * because a rewrite that guesses what the code means is worse than none. * A value the construct spells with a helper (`Frequency.EVERY_5M`, * `RetryStrategyBuilder.fixedStrategy({ … })`) is `helper-edit.ts`'s job; * `apply-edits.ts` splices both kinds into the text, and `imports.ts` adds * the helper classes to the file's import. * * Rendering copies what surrounds the value: the quote character the object * already uses, its indentation unit, whether it ends members with a * trailing comma, and the file's line ending. Commas and comments are * located through the parser's tokens, so a comma or a line break inside a * comment cannot mislead a splice. (`src/sourcegen` renders construct * source too, but it orders keys and fixes the style, which is what a * splice into a user's file must not do.) */ export interface LiteralEdit { /** Property path inside the options object; a numeric segment indexes an array. */ path: string[]; /** The value to write. `null` and `undefined` are refused (see `renderValue`). */ value: unknown; /** Absent on a literal edit; a `HelperEdit` (`helper-edit.ts`) sets it. */ helper?: undefined; } export interface AppliedEdit extends LiteralEdit { /** The source text the edit replaced, or undefined when the property was added. */ previous?: string; /** The text written for the value. */ rendered: string; /** Whether the text is a plain literal or a helper expression (see `helper-edit.ts`). */ form: 'literal' | 'helper'; /** For the literal form, the value the written text evaluates to. */ expected?: unknown; /** For the helper form, the expression the text was rendered from, and the local name of each class it references. */ expression?: { value: Value; locals: ReadonlyMap; }; } /** An edit of either kind that was not applied, with the reason. */ export interface SkippedEdit { path: string[]; value: unknown; reason: string; } export interface EditResult { text: string; applied: AppliedEdit[]; skipped: SkippedEdit[]; /** Helper classes added to the file's `checkly/constructs` import. */ imports: string[]; } export interface SourceStyle { quote: '\'' | '"'; indentUnit: string; lineEnding: '\n' | '\r\n'; } type ObjectNode = TSESTree.ObjectExpression; export type Resolution = { kind: 'found'; node: Node; parent: ObjectNode | TSESTree.ArrayExpression; } | { kind: 'missing'; parent: ObjectNode; key: string; } | { kind: 'unsupported'; reason: string; }; /** * Whether a node is a value this module could have written itself: a * string, number or boolean literal, a template with no `${}`, a negated * number, or an array or object built only of those. Such a node can be * replaced wholesale without losing anything but the comments inside it. */ export declare function isPlainLiteral(node: Node): boolean; /** The value a plain literal node evaluates to; only meaningful when `isPlainLiteral` holds. */ export declare function evaluateLiteral(node: Node): unknown; export declare function describe(node: Node): string; export interface ResolveOptions { /** Whether an existing node at the path may be replaced; a plain literal by default. */ replaceable?: (node: Node) => boolean; /** What a refused node should have been, for the reason. */ expected?: string; /** * Whether a `null` in the code may be replaced. A literal edit never * writes null (`renderValue`), so by default a null is left for the user; * a helper edit writes an expression and may replace it like any literal. */ replaceNull?: boolean; } /** * Where `path` lands inside `options`: an existing node that may be * replaced, a key the innermost object lacks, or a position this module * must not touch. */ export declare function resolvePath(options: ObjectNode, path: readonly string[], resolve?: ResolveOptions): Resolution; /** * The conventions the edited object follows: the quote most of its strings * use (the whole file's when it has none), the indentation unit between it * and its members, and the file's line ending. Defaults (single quotes, two * spaces, LF) apply where there is no evidence. */ export declare function detectStyle(source: ParsedSource, options: ObjectNode): SourceStyle; /** The line ending a file uses: CRLF when it holds one, else LF. */ export declare function lineEndingOf(text: string): SourceStyle['lineEnding']; export declare function lineStart(text: string, offset: number): number; /** The leading whitespace of the line holding `offset`. */ export declare function indentationAt(text: string, offset: number): string; export declare function isMultiLine(text: string, node: Node): boolean; /** The comma token that follows a list's last member, if the list has one. */ export declare function trailingCommaOf(source: ParsedSource, list: ObjectNode | TSESTree.ArrayExpression): SourceToken | undefined; export declare function quoteString(value: string, quote: SourceStyle['quote']): string; /** * `value` as a template literal, for a multi-line string replacing one so * the author's form is kept; undefined when it cannot read back as the same * string, which a parser guarantees only for LF and TAB among the control * characters (CR and CRLF are normalised to LF inside a template). */ export declare function templateLiteral(value: string): string | undefined; export declare function renderKey(key: string, quote: SourceStyle['quote']): string; /** * The source text for a value. Arrays of scalars stay on one line; arrays * holding objects, and objects, put one member per line indented one unit * past `column` (the indentation of the line the value starts on), unless * `inline` asks for the one-line form an existing node used. `trailingComma` * ends the last member of a multi-line list with a comma, as the edited * object does. * * @throws WriteBackSkipped for a value the construct cannot hold as a * literal: null or undefined (Checkly has cleared the property, and removing * it from the code would hand it to a default that need not match), a * non-finite number, or anything that is not JSON data. */ export declare function renderValue(value: unknown, style: SourceStyle, layout: { column: string; inline: boolean; trailingComma: boolean; at?: string; }): string; /** * A list of rendered members between `open` and `close`: empty as `[]` or * `{}`, on one line when `inline`, otherwise one member per line indented one * unit past `column`, the last ending with a comma when `trailingComma`. */ export declare function layoutList(open: string, members: readonly string[], close: string, style: SourceStyle, layout: { column: string; inline: boolean; trailingComma: boolean; }): string; /** * The indentation a member added to `parent` gets: that of the last * member's line when it starts one, otherwise one unit past the object's * own line (which also covers an empty object). */ export declare function memberColumn(text: string, parent: ObjectNode, style: SourceStyle): string; /** * The single splice that adds `pending` members to `parent`, after its last * member. A multi-line object gets one line per member at the last member's * indentation, a trailing comma on the last new member only if the object * already used one, and a comma added right after the old last member if it * had none. The new lines go after the line the last member (or its comma) * ends on, so a comment on that line stays with that member — unless a * comment runs on past that line end, in which case they go right after the * member's comma rather than into the comment. A one-line object gets * `, key: value` before its closing brace, and an empty one is rewritten as * `{ key: value }`. */ export declare function insertionSplice(text: string, source: ParsedSource, parent: ObjectNode, pending: readonly { key: string; rendered: string; }[], style: SourceStyle): Splice; export {};