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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245/** Transform hook implementation that rewrites import attributes into query-parameter-tagged specifiers using AST parsing. Uses rolldown's built-in `parseSync` (backed by oxc) to parse source code and walk the AST, replacing fragile regex with proper syntax analysis. @module */
import { type ESTree, parseSync, Visitor,} from 'rolldown/utils';import type { ForeignBorrowed, } from '@monochromatic-dev/ownership-marker-foreign-borrowed/ts';
import { extractTypeFromAttributes, extractTypeFromOptions, getStringLiteralValue, NO_ATTR_TYPE, NON_STRING_NODE,} from './ast-extract.ts';import { ATTR_QUERY_KEY, } from './patterns.ts';import { collectStaticReplacements, type Replacement,} from './transform-helpers.ts';
/** Sentinel returned by {@link transformImportAttributes} when the source has no import-attribute clause to rewrite. A `unique symbol`; the plugin's `transform` hook converts it to `null` (Rolldown's "no change" signal) at the boundary. */export const NO_TRANSFORM: unique symbol = Symbol('import-attributes/source-has-no-attribute-clause-to-rewrite',);
/** Rewrites import/export statements that use `with { type: '...' }` attributes. Parses the source with oxc, walks the AST to find import attributes via {@link extractTypeFromAttributes}/{@link extractTypeFromOptions}, and applies precise span-based replacements collected by {@link collectStaticReplacements}. @param code - source code to transform @param id - module ID (filename) for the parser @returns transformed code object, or {@link NO_TRANSFORM} if no attributes were found @example ```ts const result = transformImportAttributes( "import x from './file.sql' with { type: 'text' }", 'main.ts', ); // result.code: "import x from './file.sql?attr=text'" ``` */export function transformImportAttributes({ code, id,}: { readonly code: string; readonly id: string;},): { code: string; } | typeof NO_TRANSFORM { if ((!code.includes(' with ',)) && (!code.includes(' with{',))) return NO_TRANSFORM;
/** Parsed AST root walked to collect attribute replacements. */ const result = parseSync( id, code, ); /** Accumulator of span replacements that the reducer below applies to the source code. */ const replacements: Replacement[] = [];
/** AST visitor that records replacements for each kind of attribute-bearing declaration. */ const visitor = new Visitor({ ImportDeclaration(node: ForeignBorrowed<ESTree.ImportDeclaration>,): void { if (node.attributes .length === 0) return; /** Attribute type on this static import; gates whether to emit a replacement. */ const attrType = extractTypeFromAttributes(node.attributes,); if (attrType === NO_ATTR_TYPE) return; replacements.push(...collectStaticReplacements({ source: node.source, attributes: node.attributes, attrType, code, },),); },
ExportNamedDeclaration(node: ForeignBorrowed<ESTree.ExportNamedDeclaration>,): void { if ((node.source === null) || (node.attributes .length === 0)) return; /** Attribute type on this re-export with source; gates whether to emit a replacement. */ const attrType = extractTypeFromAttributes(node.attributes,); if (attrType === NO_ATTR_TYPE) return; replacements.push(...collectStaticReplacements({ source: node.source, attributes: node.attributes, attrType, code, },),); },
ExportAllDeclaration(node: ForeignBorrowed<ESTree.ExportAllDeclaration>,): void { if (node.attributes .length === 0) return; /** Attribute type on this wildcard re-export; gates whether to emit a replacement. */ const attrType = extractTypeFromAttributes(node.attributes,); if (attrType === NO_ATTR_TYPE) return; replacements.push(...collectStaticReplacements({ source: node.source, attributes: node.attributes, attrType, code, },),); },
ImportExpression(node: ForeignBorrowed<ESTree.ImportExpression>,): void { if (node.options === null) return; /** Attribute type extracted from the dynamic-import options object. */ const attrType = extractTypeFromOptions(node.options,); if (attrType === NO_ATTR_TYPE) return; /** Literal specifier text; computed sources are skipped because their bytes cannot be rewritten safely. */ const sourceValue = getStringLiteralValue(node.source,); if (sourceValue === NON_STRING_NODE) return;
/** Quote character preserved so the rewritten specifier matches the source's quoting style. */ const quote = code[node.source .start]; replacements.push({ start: node.source .start, end: node.source .end, text: `${quote}${sourceValue}?${ATTR_QUERY_KEY}=${attrType}${quote}`, },);
// Remove the options argument: find the first comma between source and options /** Slice between source end and options start, scanned to locate the separating comma. */ const between = code.slice( node.source .end, node.options .start, ); /** Position of the comma within `between`; -1 means no comma was found. */ const relCommaIndex = between.indexOf(',',); /** Absolute offset where the options-argument removal span begins. */ const commaPos = relCommaIndex === (-1) ? node.options .start : node.source .end + relCommaIndex; replacements.push({ start: commaPos, end: node.options .end, text: '', },); }, },);
visitor.visit(result.program,);
if (replacements.length === 0) return NO_TRANSFORM;
// Apply replacements in reverse order to preserve byte offsets replacements.sort(function byStartDesc( a, b, ) { return b.start - a .start; },);
/** Final source after every replacement has been applied in descending start order. */ const transformed = replacements.reduce( function applyReplacement( acc, r, ): string { return acc.slice( 0, r.start, ) + r .text + acc .slice(r.end,); }, code, );
return { code: transformed, };}