import * as t from '@babel/types'; /** * Strips leading whitespace characters from a string literal type. * Used so that indented templates still infer the correct return type. * * TrimWS<' if (x) {}'> → 'if (x) {}' */ type TrimWS = S extends ` ${infer R}` | `\n${infer R}` | `\t${infer R}` | `\r${infer R}` ? TrimWS : S; /** * Infers the precise Babel statement node type from the first keyword in a * template string. This is what makes js`if (...) {}` return t.IfStatement * instead of the broad t.Statement. * * The pattern match runs after stripping leading whitespace so indented * multi-line templates work identically. */ type InferStatement = TrimWS extends `if${string}` ? t.IfStatement : TrimWS extends `var ${string}` | `let ${string}` | `const ${string}` ? t.VariableDeclaration : TrimWS extends `return${string}` ? t.ReturnStatement : TrimWS extends `for${string}` ? t.ForStatement : TrimWS extends `while${string}` ? t.WhileStatement : TrimWS extends `do${string}` ? t.DoWhileStatement : TrimWS extends `continue${string}` ? t.ContinueStatement : TrimWS extends `break${string}` ? t.BreakStatement : TrimWS extends `throw ${string}` ? t.ThrowStatement : TrimWS extends `switch${string}` ? t.SwitchStatement : TrimWS extends `try${string}` ? t.TryStatement : TrimWS extends `function${string}` ? t.FunctionDeclaration : TrimWS extends `class${string}` ? t.ClassDeclaration : TrimWS extends `{${string}` ? t.BlockStatement : t.Statement; /** Top-level nodes that may appear in a parsed module body. */ type ModuleNode = t.Statement | t.ModuleDeclaration; /** * Optional parse context for statement templates that need extra surrounding * syntax to be valid during parsing. */ interface ParseContext { breakLabels?: readonly string[]; continueLabels?: readonly string[]; } /** Fragment node kinds that need wrapper-specific parse contexts. */ type FragmentKind = 'ImportDeclaration' | 'ExportNamedDeclaration' | 'ExportDefaultDeclaration' | 'ExportAllDeclaration' | 'ClassMethod' | 'ClassProperty' | 'ClassPrivateMethod' | 'ClassPrivateProperty' | 'ObjectMethod' | 'ObjectProperty' | 'VariableDeclarator' | 'Pattern' | 'ObjectPattern' | 'ArrayPattern' | 'AssignmentPattern' | 'RestElement'; /** Maps a fragment kind to the Babel node type it returns. */ interface FragmentNodeMap { ImportDeclaration: t.ImportDeclaration; ExportNamedDeclaration: t.ExportNamedDeclaration; ExportDefaultDeclaration: t.ExportDefaultDeclaration; ExportAllDeclaration: t.ExportAllDeclaration; ClassMethod: t.ClassMethod; ClassProperty: t.ClassProperty; ClassPrivateMethod: t.ClassPrivateMethod; ClassPrivateProperty: t.ClassPrivateProperty; ObjectMethod: t.ObjectMethod; ObjectProperty: t.ObjectProperty; VariableDeclarator: t.VariableDeclarator; Pattern: t.LVal; ObjectPattern: t.ObjectPattern; ArrayPattern: t.ArrayPattern; AssignmentPattern: t.AssignmentPattern; RestElement: t.RestElement; } /** Callable template builder for fragment-parsing helpers. */ interface FragmentBuilder { (template: string, ...holes: HoleValue[]): FragmentNodeMap[K]; (strings: TemplateStringsArray, ...holes: HoleValue[]): FragmentNodeMap[K]; } /** Fragment collection kinds that return a list extracted from a wrapper. */ type FragmentCollectionKind = 'ModuleBody' | 'ClassBody' | 'ObjectBody' | 'Params' | 'ArrayElements'; /** Maps a collection kind to the Babel node list it returns. */ interface FragmentCollectionNodeMap { ModuleBody: ModuleNode[]; ClassBody: t.ClassBody['body']; ObjectBody: t.ObjectExpression['properties']; Params: t.FunctionDeclaration['params']; ArrayElements: t.ArrayExpression['elements']; } /** Callable template builder for collection fragment helpers. */ interface FragmentCollectionBuilder { (template: string, ...holes: HoleValue[]): FragmentCollectionNodeMap[K]; (strings: TemplateStringsArray, ...holes: HoleValue[]): FragmentCollectionNodeMap[K]; } /** Lightweight subtree rewrite visitor. */ type RewriteVisitor = Partial<{ [K in t.Node['type']]: (node: Extract) => t.Node | void; }> & { enter?: (node: t.Node) => t.Node | void; exit?: (node: t.Node) => t.Node | void; }; /** * Accepted types for template holes. * * Primitive values are automatically coerced to their AST literal equivalents: * string → t.StringLiteral * number → t.NumericLiteral * boolean → t.BooleanLiteral * null → t.NullLiteral * * This means you can write js`this.$(${'.root'})` instead of js`this.$(${str('.root')})`. */ type HoleValue = t.Node | string | number | boolean | null; /** * Build a t.Identifier, preserving the name as a literal type so TypeScript * can track it through the AST. * * @example * const node = id('__container') * // node: t.Identifier & { name: '__container' } */ declare function id(name: N): t.Identifier & { name: N; }; /** * Build a t.StringLiteral, preserving the value as a literal type. * * Use this inside template holes when you need a JS string literal rather * than an identifier. Without it, bare strings are treated as identifiers. * * @example * js`this.$({ str('.container') })` // → this.$('.container') * js`this.$({ id('x') })` // → this.$(x) */ declare function str(value: V): t.StringLiteral & { value: V; }; /** * Build a t.NumericLiteral, preserving the value as a literal type. */ declare function num(value: V): t.NumericLiteral & { value: V; }; /** * Build a t.BooleanLiteral, preserving the value as a literal type. */ declare function bool(value: V): t.BooleanLiteral & { value: V; }; /** Build a t.NullLiteral. */ declare function nil(): t.NullLiteral; /** * Deep-clone a Babel AST node. Thin alias for t.cloneNode that avoids * importing @babel/types at every call site. */ declare function clone(node: T): T; type ReplaceInput = T | ((node: T) => T); type BodyStatementsInput = readonly t.Statement[] | ((body: readonly t.Statement[]) => readonly t.Statement[]); type BodyOwner = t.FunctionDeclaration | t.FunctionExpression | t.ArrowFunctionExpression | t.ClassMethod | t.ClassPrivateMethod | t.ObjectMethod; declare function replaceExpr(node: t.Expression, next: ReplaceInput): t.Expression; declare function replaceStmt(node: T, next: ReplaceInput): T; declare function replaceMany(node: T, next: readonly t.Statement[] | ((node: T) => readonly t.Statement[])): t.Statement[]; declare function wrapExpr(node: t.Expression, wrap: (node: t.Expression) => t.Expression): t.Expression; declare function wrapStmt(node: T, wrap: (node: T) => t.Statement): t.Statement; declare function withBody(node: T, edit: (body: readonly t.Statement[]) => readonly t.Statement[]): T; declare function replaceBody(node: T, body: BodyStatementsInput): T; declare function appendToBody(node: T, ...statements: readonly t.Statement[]): T; declare function prependToBody(node: T, ...statements: readonly t.Statement[]): T; declare function appendToBlock(block: t.BlockStatement, ...statements: readonly t.Statement[]): t.BlockStatement; declare function prependToBlock(block: t.BlockStatement, ...statements: readonly t.Statement[]): t.BlockStatement; declare function insertBefore(block: t.BlockStatement, target: t.Statement, ...statements: readonly t.Statement[]): t.BlockStatement; declare function insertAfter(block: t.BlockStatement, target: t.Statement, ...statements: readonly t.Statement[]): t.BlockStatement; declare function removeNode(block: t.BlockStatement, target: t.Statement): t.BlockStatement; declare function rewrite(node: T, visitor: RewriteVisitor): T; declare function renameIdentifier(node: T, from: string, to: string): T; declare function replaceIdentifier(node: T, name: string, expr: t.Expression): T; interface ContextualJs { (template: T, ...holes: HoleValue[]): InferStatement; (strings: TemplateStringsArray, ...holes: HoleValue[]): R; } interface ContextualJsAll { (template: string, ...holes: HoleValue[]): t.Statement[]; (strings: TemplateStringsArray, ...holes: HoleValue[]): t.Statement[]; } interface ContextualClass { (template: string, ...holes: HoleValue[]): t.ClassDeclaration; (strings: TemplateStringsArray, ...holes: HoleValue[]): t.ClassDeclaration; } interface ContextualFunction { (template: string, ...holes: HoleValue[]): t.FunctionDeclaration; (strings: TemplateStringsArray, ...holes: HoleValue[]): t.FunctionDeclaration; } /** * Build a **single** AST statement from a template string. * * Supports both tagged-template and string-call forms: * * - Tagged template: ergonomic `${...}` hole syntax. * - String call: exact literal-type inference via `InferStatement`. * * @example * const stmt = js('if (!%%) { init(); }', cond) * // stmt: t.IfStatement * * @example * const loop = js`for (var i = 0; i < ${arr}.length; i++) {}` * // loop: t.Statement by default, but can still narrow via context/generic * * @throws if the template does not produce exactly one statement. */ declare function js(template: T, ...holes: HoleValue[]): InferStatement; declare function js(strings: TemplateStringsArray, ...holes: HoleValue[]): R; /** * Build a single statement with explicit parse context for labeled * `break`/`continue` targets. */ declare function jsInContext(context: ParseContext): ContextualJs; /** * Build **multiple** AST statements from a template string. * * @example * const stmts = jsAll` * var ${id(parentVar)} = ${id(elVar)}.parentElement; * var ${id(tmpVar)} = ${id(parentVar)} * ? ${id(parentVar)}.cloneNode(false) * : document.createElement('div'); * ${id(tmpVar)}.innerHTML = this.${id(renderMethod)}(${id(itemVar)}); * ` * // stmts: t.Statement[] */ declare function jsAll(template: string, ...holes: HoleValue[]): t.Statement[]; declare function jsAll(strings: TemplateStringsArray, ...holes: HoleValue[]): t.Statement[]; /** Alias for jsAll when the statements are intended for a block or method body. */ declare const jsBlockBody: typeof jsAll; /** Build multiple statements with explicit parse context for labeled control flow. */ declare function jsAllInContext(context: ParseContext): ContextualJsAll; /** Build top-level module statements and declarations, including import/export. */ declare function jsModule(template: string, ...holes: HoleValue[]): ModuleNode[]; declare function jsModule(strings: TemplateStringsArray, ...holes: HoleValue[]): ModuleNode[]; /** Build a fragment node that needs a wrapper-specific parse context. */ declare function jsAs(kind: K): FragmentBuilder; /** Alias for jsAs(...) when you want a more generic name. */ declare const parseAs: typeof jsAs; /** Build a fragment collection that needs a wrapper-specific parse context. */ declare function jsAsMany(kind: K): FragmentCollectionBuilder; /** Alias for jsAsMany(...) when you want a more generic name. */ declare const parseAsMany: typeof jsAsMany; /** Build a single class method fragment. */ declare const jsMethod: FragmentBuilder<"ClassMethod">; /** Build a single import declaration fragment. */ declare const jsImport: FragmentBuilder<"ImportDeclaration">; /** Build a single named export declaration fragment. */ declare const jsExport: FragmentBuilder<"ExportNamedDeclaration">; /** Build a single default export declaration fragment. */ declare const jsExportDefault: FragmentBuilder<"ExportDefaultDeclaration">; /** Build a single export-all declaration fragment. */ declare const jsExportAll: FragmentBuilder<"ExportAllDeclaration">; /** Build a single class property fragment. */ declare const jsClassProp: FragmentBuilder<"ClassProperty">; /** Build a single private class method fragment. */ declare const jsPrivateMethod: FragmentBuilder<"ClassPrivateMethod">; /** Build a single private class property fragment. */ declare const jsPrivateProp: FragmentBuilder<"ClassPrivateProperty">; /** Build a single object method fragment. */ declare const jsObjectMethod: FragmentBuilder<"ObjectMethod">; /** Build a single object property fragment. */ declare const jsProp: FragmentBuilder<"ObjectProperty">; /** Alias for jsProp when object-property naming reads better at the call site. */ declare const jsObjectProp: FragmentBuilder<"ObjectProperty">; /** Build a single variable declarator fragment. */ declare const jsDeclarator: FragmentBuilder<"VariableDeclarator">; /** Build a single parameter/pattern fragment. */ declare const jsPattern: FragmentBuilder<"Pattern">; /** Build a single object-pattern fragment. */ declare const jsObjectPattern: FragmentBuilder<"ObjectPattern">; /** Build a single array-pattern fragment. */ declare const jsArrayPattern: FragmentBuilder<"ArrayPattern">; /** Build a single assignment-pattern fragment. */ declare const jsAssignmentPattern: FragmentBuilder<"AssignmentPattern">; /** Build a single rest-element fragment. */ declare const jsRest: FragmentBuilder<"RestElement">; /** Alias for jsPattern when parameter naming reads better at the call site. */ declare const jsParam: FragmentBuilder<"Pattern">; /** Build a class-body member list. */ declare const jsClassBody: FragmentCollectionBuilder<"ClassBody">; /** Build a module-body statement/declaration list. */ declare const jsModuleBody: FragmentCollectionBuilder<"ModuleBody">; /** Build an object-member list. */ declare const jsObjectBody: FragmentCollectionBuilder<"ObjectBody">; /** Build a function parameter list. */ declare const jsParams: FragmentCollectionBuilder<"Params">; /** Build an array element list. */ declare const jsArrayElements: FragmentCollectionBuilder<"ArrayElements">; /** * Build a single AST **expression** from a template string. * * The template must be a single expression (no trailing semicolon required, * but it must produce exactly one expression statement when parsed). * * @example * const expr = jsExpr`${el}.textContent !== ${value}` * // expr: t.Expression (t.BinaryExpression at runtime) * * @example * const call = jsExpr`${id(arr)}.find(__t => __t.${id(idProp)} === ${idExpr})` * // call: t.Expression (t.CallExpression at runtime) * * @throws if the template is not a single expression statement. */ declare function jsExpr(template: string, ...holes: HoleValue[]): t.Expression; declare function jsExpr(strings: TemplateStringsArray, ...holes: HoleValue[]): t.Expression; /** Build a single object expression with an explicit object-focused name. */ declare function jsObjectExpr(template: string, ...holes: HoleValue[]): t.ObjectExpression; declare function jsObjectExpr(strings: TemplateStringsArray, ...holes: HoleValue[]): t.ObjectExpression; /** Build a single array expression with an explicit array-focused name. */ declare function jsArrayExpr(template: string, ...holes: HoleValue[]): t.ArrayExpression; declare function jsArrayExpr(strings: TemplateStringsArray, ...holes: HoleValue[]): t.ArrayExpression; /** Build a single class declaration with an explicit class-focused name. */ declare function jsClass(template: string, ...holes: HoleValue[]): t.ClassDeclaration; declare function jsClass(strings: TemplateStringsArray, ...holes: HoleValue[]): t.ClassDeclaration; /** Build a single function declaration with an explicit function-focused name. */ declare function jsFunction(template: string, ...holes: HoleValue[]): t.FunctionDeclaration; declare function jsFunction(strings: TemplateStringsArray, ...holes: HoleValue[]): t.FunctionDeclaration; /** * Build a `t.TemplateLiteral` node from a tagged template string. * * This solves the template-literal-in-generated-code problem: instead of * escaping backticks inside `js\`...\``, use `tpl\`...\`` as a hole. * * Holes may be AST nodes or raw primitives (auto-coerced to literals). * * @example * // Generate: container.querySelector(`[key="${id}"]`) * const selector = tpl`[key="${idExpr}"]` * const call = jsExpr`${containerRef}.querySelector(${selector})` * * @example * // Generate: const msg = `Hello, ${name}!` * const decl = js`const msg = ${tpl`Hello, ${nameExpr}!`};` */ declare function tpl(template: string, ...holes: HoleValue[]): t.TemplateLiteral; declare function tpl(strings: TemplateStringsArray, ...holes: HoleValue[]): t.TemplateLiteral; export { type ContextualClass, type ContextualFunction, type ContextualJs, type ContextualJsAll, type FragmentBuilder, type FragmentCollectionBuilder, type FragmentCollectionKind, type FragmentKind, type HoleValue, type InferStatement, type ModuleNode, type ParseContext, type RewriteVisitor, appendToBlock, appendToBody, bool, clone, id, insertAfter, insertBefore, js, jsAll, jsAllInContext, jsArrayElements, jsArrayExpr, jsArrayPattern, jsAs, jsAsMany, jsAssignmentPattern, jsBlockBody, jsClass, jsClassBody, jsClassProp, jsDeclarator, jsExport, jsExportAll, jsExportDefault, jsExpr, jsFunction, jsImport, jsInContext, jsMethod, jsModule, jsModuleBody, jsObjectBody, jsObjectExpr, jsObjectMethod, jsObjectPattern, jsObjectProp, jsParam, jsParams, jsPattern, jsPrivateMethod, jsPrivateProp, jsProp, jsRest, nil, num, parseAs, parseAsMany, prependToBlock, prependToBody, removeNode, renameIdentifier, replaceBody, replaceExpr, replaceIdentifier, replaceMany, replaceStmt, rewrite, str, tpl, withBody, wrapExpr, wrapStmt };