import { SourceLocation, SourcePosition } from "@opticss/element-analysis"; import { SerializedTemplateInfo, TemplateAnalysis as OptimizationTemplateAnalysis, TemplateTypes } from "@opticss/template-api"; import { ObjectDictionary } from "@opticss/util"; import { IdentGenerator } from "opticss"; import { BlockFactory } from "../BlockParser"; import { Block, Style } from "../BlockTree"; import { ResolvedConfiguration } from "../configuration"; import { Analyzer } from "./Analyzer"; import { ElementAnalysis, SerializedElementAnalysis, SerializedElementSourceAnalysis } from "./ElementAnalysis"; import { TemplateValidatorOptions } from "./validations"; /** * This interface defines a JSON friendly serialization * of an {Analysis}. */ export interface SerializedAnalysis { template: SerializedTemplateInfo; blocks: ObjectDictionary; stylesFound: string[]; elements: ObjectDictionary; } /** * This interface defines a JSON friendly serialization * of an {Analysis}. */ export interface SerializedSourceAnalysis { template: SerializedTemplateInfo; blocks: ObjectDictionary; stylesFound: string[]; elements: ObjectDictionary; } declare type ElementAnalyzedCallback = (element: ElementAnalysis) => void; /** * An Analysis performs book keeping and ensures internal consistency of the block objects referenced * within a single template. It is designed to be used as part of an AST walk over a template. * * 1. Call [[startElement startElement()]] at the beginning of an new html element. * 2. Call [[addStyle addStyle(style, isDynamic)]] for all the styles used on the current html element. * 2. Call [[addExclusiveStyle addExclusiveStyle(alwaysPresent, ...style)]] for all the styles used that are mutually exclusive on the current html element. * 3. Call [[endElement endElement()]] when done adding styles for the current element. */ export declare class Analysis { idGenerator: IdentGenerator; template: TemplateTypes[K]; /** * A per-element correlation of styles used. The current correlation is added * to this list when [[endElement]] is called. */ elements: Map>; /** * A map from a local name for the block to the [[Block]]. * The local name must be a legal CSS ident/class name but this is not validated here. * See [[CLASS_NAME_IDENT]] for help validating a legal class name. */ private blocks; /** * The current element, created when calling [[startElement]]. * The current element is unset after calling [[endElement]]. */ private currentElement; /** * Template validator instance to verify blocks applied to an element. */ private validator; /** * Callback when an element is done being analyzed. * The element analysis will be sealed. */ onElementAnalyzed?: ElementAnalyzedCallback; /** * @param template The template being analyzed. */ constructor(template: TemplateTypes[K], options?: TemplateValidatorOptions, onElementAnalyzed?: ElementAnalyzedCallback); /** * Return the number of blocks discovered in this Template. */ blockCount(): number; /** * Convenience setter for adding a block to the template scope. */ addBlock(name: string, block: Block): Block; /** * Convenience getter for fetching a block from the template scope. */ getBlock(name: string): Block | undefined; /** * Return the number of elements discovered in this Analysis. */ elementCount(): number; /** * Get the nth element discovered in this Analysis. */ getElement(idx: number): ElementAnalysis; /** * Return the number of styles discovered in this Analysis' Template. * TODO: This is slow. We can pre-compute this as elements are added. */ styleCount(): number; /** * Get an Element by ID. */ getElementById(id: string): ElementAnalysis | undefined; /** * Returns the local name of the provided in this Analysis' template. * @param block The block for which the local name should be returned. * @return The local name of the given block. */ getBlockName(block: Block): string | null; _searchForBlock(blockToFind: Block, block: Block, parentPath: string): string | null; /** * Get a new {ElementAnalysis} object to track an individual element's {Style} * consumption in this {Analysis}' template. Every {ElementAnalysis} returned from * `Analysis.startElement` must be passed to `Analysis.endElement` before startElement * is called again. * @param location The {SourceLocation} of this element in the template. * @param tagName Optional. The tag name of the element being represented by this {ElementAnalysis}. * @returns A new {ElementAnalysis}. */ startElement(location: SourceLocation | SourcePosition, tagName?: string): ElementAnalysis; /** * Finish an {ElementAnalysis} object returned from `Analysis.startElement` to * the end location in source and save {Style} usage on the parent {Analysis}. * @param element The {ElementAnalysis} we are finishing. * @param endPosition Optional. The location in code where this element tag is closed.. */ endElement(element: ElementAnalysis, endPosition?: SourcePosition): void; /** * Given a {SourcePosition}, ensure that the file name is present. If not, * add the template identifier. * @param post The {SourcePosition} we are validating. */ private ensureFilename; /** * @return The local name for the block object using the local prefix for the block. */ serializedName(o: Style): string; /** * All the blocks referenced by this analysis. */ referencedBlocks(): Block[]; /** * For now, returns all aliases referenced by this block and all the blocks they * reference recursively. We can add more to this set in future */ reservedClassNames(): Set; /** * All the blocks referenced by this block and all the blocks they reference recursively. */ transitiveBlockDependencies(): Set; /** * All bhe blocks this block depends on. Same as referenced blocks except for the return type. */ blockDependencies(): Set; stylesFound(dynamic?: boolean): IterableIterator