import { TextDocument } from 'vscode' ; import { Parser } from 'htmlparser2' ; import { Framework , ScopeRange } from './base' ; import { LanguageId } from '~/utils' ; export default class TranslocoFramework extends Framework { id = 'transloco' ; display = 'Angular Transloco' ; detection = { packageJSON : [ '@ngneat/transloco' , ] , } ; languageIds : LanguageId [ ] = [ 'javascript' , 'typescript' , 'html' , ] ; usageMatchRegex = [ '[`\'"]({key})[`\'"][\\s\\n]*\\|[\\s\\n]*transloco' , '[^\\w\\d](?:t|translate|selectTranslate|getTranslateObject|selectTranslateObject|getTranslation|setTranslationKey)\\([\\s\\n]*[\'"`]({key})[\'"`]' , '[^*\\w\\d]transloco=[\'"`]({key})[\'"`]' , ] ; refactorTemplates ( keypath ) { return [ `{{ '${ keypath }' | transloco }}` , `t('${ keypath }')` , keypath , ] ; } getScopeRange ( document ) { if ( document . languageId !== 'html' ) return ; const ranges = [ ] ; const regex = /^.*read:\s*['"](.+?)['"].*$/ ; const tagStack = [ ] ; let stackDepth = - 1 ; let namespace = '' ; let start = 0 ; const parser = new Parser ( { onopentag ( name , attribs ) { tagStack . push ( name ) ; const attr = attribs [ '*transloco' ] ; if ( attr && parser . endIndex != null ) { if ( ! regex . test ( attr ) ) return ; namespace = attr . replace ( regex , '$1' ) ; start = parser . startIndex ; stackDepth = tagStack . length ; } } , onclosetag ( ) { if ( tagStack . length === stackDepth ) { if ( namespace ) { ranges . push ( { namespace , start , end : parser . endIndex ?? parser . startIndex , } ) ; } stackDepth = - 1 ; namespace = '' ; start = 0 ; } tagStack . pop ( ) ; } , } , { decodeEntities : true } ) ; parser . write ( document . getText ( ) ) ; parser . end ( ) ; return ranges ; } }