license import { ParseSourceSpan } from "./parse_util" ; export class Message { sources : MessageSpan [ ] ; param source param placeholders param placeholderToMessage param meaning param description param id constructor ( public nodes , public placeholders , public placeholderToMessage , public meaning , public description , public id ) { if ( nodes . length ) { this . sources = [ { filePath : nodes [ 0 ] . sourceSpan . start . file . url , startLine : nodes [ 0 ] . sourceSpan . start . line + 1 , startCol : nodes [ 0 ] . sourceSpan . start . col + 1 , endLine : nodes [ nodes . length - 1 ] . sourceSpan . end . line + 1 , endCol : nodes [ 0 ] . sourceSpan . start . col + 1 } ] ; } else { this . sources = [ ] ; } } } export interface MessageSpan { filePath : string ; startLine : number ; startCol : number ; endLine : number ; endCol : number ; } export interface Node { sourceSpan : ParseSourceSpan ; visit ( visitor , context ? ) : any ; } export class Text implements Node { constructor ( public value , public sourceSpan ) { } visit ( visitor , context ? ) { return visitor . visitText ( this , context ) ; } } export class Container implements Node { constructor ( public children , public sourceSpan ) { } visit ( visitor , context ? ) { return visitor . visitContainer ( this , context ) ; } } export class Icu implements Node { public expressionPlaceholder : string ; constructor ( public expression , public type , public cases , public sourceSpan ) { } visit ( visitor , context ? ) { return visitor . visitIcu ( this , context ) ; } } export class TagPlaceholder implements Node { constructor ( public tag , public attrs , public startName , public closeName , public children , public isVoid , public sourceSpan ) { } visit ( visitor , context ? ) { return visitor . visitTagPlaceholder ( this , context ) ; } } export class Placeholder implements Node { constructor ( public value , public name , public sourceSpan ) { } visit ( visitor , context ? ) { return visitor . visitPlaceholder ( this , context ) ; } } export class IcuPlaceholder implements Node { constructor ( public value , public name , public sourceSpan ) { } visit ( visitor , context ? ) { return visitor . visitIcuPlaceholder ( this , context ) ; } } export interface Visitor { visitText ( text , context ? ) : any ; visitContainer ( container , context ? ) : any ; visitIcu ( icu , context ? ) : any ; visitTagPlaceholder ( ph , context ? ) : any ; visitPlaceholder ( ph , context ? ) : any ; visitIcuPlaceholder ( ph , context ? ) : any ; } export class CloneVisitor implements Visitor { visitText ( text , context ? ) { return new Text ( text . value , text . sourceSpan ) ; } visitContainer ( container , context ? ) { const children = container . children . map ( n => n . visit ( this , context ) ) ; return new Container ( children , container . sourceSpan ) ; } visitIcu ( icu , context ? ) { const cases = { } ; Object . keys ( icu . cases ) . forEach ( key => ( cases [ key ] = icu . cases [ key ] . visit ( this , context ) ) ) ; const msg = new Icu ( icu . expression , icu . type , cases , icu . sourceSpan ) ; msg . expressionPlaceholder = icu . expressionPlaceholder ; return msg ; } visitTagPlaceholder ( ph , context ? ) { const children = ph . children . map ( n => n . visit ( this , context ) ) ; return new TagPlaceholder ( ph . tag , ph . attrs , ph . startName , ph . closeName , children , ph . isVoid , ph . sourceSpan ) ; } visitPlaceholder ( ph , context ? ) { return new Placeholder ( ph . value , ph . name , ph . sourceSpan ) ; } visitIcuPlaceholder ( ph , context ? ) { return new IcuPlaceholder ( ph . value , ph . name , ph . sourceSpan ) ; } } export class RecurseVisitor implements Visitor { visitText ( text , context ? ) { } visitContainer ( container , context ? ) { container . children . forEach ( child => child . visit ( this ) ) ; } visitIcu ( icu , context ? ) { Object . keys ( icu . cases ) . forEach ( k => { icu . cases [ k ] . visit ( this ) ; } ) ; } visitTagPlaceholder ( ph , context ? ) { ph . children . forEach ( child => child . visit ( this ) ) ; } visitPlaceholder ( ph , context ? ) { } visitIcuPlaceholder ( ph , context ? ) { } }