import { BaseVisitorContext , VisitorContext } from "../visitor-context" ; import { TS } from "../../type/type" ; import { AfterVisitorOptions } from "./after-visitor-options" ; import { visitNode } from "./visitor/visit-node" ; import { getDefineArrayLiteralExpression , getRootBlock , } from "../../util/ts-util" ; import { ensureNodeFactory } from "compatfactory" ; type SourceFileWithEmitNodes = TS . SourceFile & { emitNode ? : { helpers ? : TS . EmitHelper [ ] ; } ; } ; export function afterTransformer ( context ) { return ( transformationContext ) => { const factory = ensureNodeFactory ( transformationContext . factory ?? context . typescript ) ; return ( sourceFile ) => transformSourceFile ( sourceFile , { ... context , transformationContext , factory , } ) ; } ; } function transformSourceFile ( sourceFile , context ) { if ( sourceFile . emitNode != null && sourceFile . emitNode . helpers != null ) { const seenNames = new Set ( ) ; const filtered = sourceFile . emitNode . helpers . filter ( ( helper ) => { if ( seenNames . has ( helper . name ) ) return false ; seenNames . add ( helper . name ) ; return true ; } ) ; if ( filtered . length !== sourceFile . emitNode . helpers . length ) { sourceFile . emitNode . helpers = filtered ; } } const visitorOptions = { context , defineArrayLiteralExpression : getDefineArrayLiteralExpression ( sourceFile , context ) , rootBlock : getRootBlock ( sourceFile , context ) , continuation : ( node ) => visitNode ( { ... visitorOptions , sourceFile , node , } ) , childContinuation : ( node ) => context . typescript . visitEachChild ( node , ( cbNode ) => visitNode ( { ... visitorOptions , sourceFile , node : cbNode , } ) , context . transformationContext ) , } ; return visitorOptions . continuation ( sourceFile ) as TS . SourceFile ; }