/**
 * Represents an action that can be performed by the optimizer.
 * All optimizations must be done as an action. This provides
 * a central point for creating logs and reports of the work
 * that was done. It can also be used to implement backtracking.
 */ import { SourcePosition } from "@opticss/element-analysis" ; import { Keys } from "@opticss/util" ; import * as postcss from "postcss" ; import { Optimizations } from "../optimizations" ; export abstract class Action { optimization : string ; abstract perform ( ) ; abstract logString ( ) ; abstract readonly sourcePosition : SourcePosition | undefined | null ; constructor ( optimization ) { this . optimization = optimization ; } positionString ( sourcePosition = this . sourcePosition , includeFilename = true ) { let posStr = "" ; if ( sourcePosition && sourcePosition . line > 0 ) { if ( includeFilename && sourcePosition . filename ) { posStr += sourcePosition . filename + ":" ; } posStr += sourcePosition . line ; if ( sourcePosition . column ) { posStr += ":" + sourcePosition . column ; } } return posStr ; } annotateLogMessage ( message , sourcePosition ? , indent = 0 ) { if ( sourcePosition === undefined ) { sourcePosition = this . sourcePosition ; } let annotated = "" ; if ( sourcePosition ) { annotated += this . positionString ( sourcePosition ) + " " ; } let indentation = "" ; for ( let i = 0 ; i < indent ; i ++ ) { indentation += "  " ; } annotated += `[${ this . optimization }] ${ indentation }${ message }` ; return annotated ; } nodeSourcePosition ( node ) { if ( node . source && node . source . start ) { return { filename : node . source . input . file , line : node . source . start . line , column : node . source . start . column , } ; } else { return undefined ; } } } export abstract class MultiAction extends Action { logString ( ) { return this . logStrings ( ) . join ( "\n" ) ; } abstract logStrings ( ) ; } export function isMultiAction ( action ) { return action instanceof MultiAction ; } export function stripNL ( str ) { return str . replace ( /[\r\n\s]+/gm , " " ) ; }