@@ -56,6 +56,46 @@ export interface YAMLParsed extends Parsed<YAMLAST.YAMLNode> {
5656 ast : YAMLAST . YAMLProgram
5757}
5858
59+ function hasEndTag (
60+ element : VAST . VElement
61+ ) : element is VAST . VElement & { endTag : VAST . VEndTag } {
62+ return ! ! element . endTag
63+ }
64+
65+ function getSourceCodeString (
66+ context : RuleContext ,
67+ i18nBlock : VAST . VElement & { endTag : VAST . VEndTag }
68+ ) : string {
69+ const tokenStore = context . parserServices . getTemplateBodyTokenStore ( )
70+ const tokens = tokenStore . getTokensBetween (
71+ i18nBlock . startTag ,
72+ i18nBlock . endTag
73+ )
74+ if (
75+ tokens . length ||
76+ i18nBlock . startTag . range [ 1 ] === i18nBlock . endTag . range [ 0 ]
77+ ) {
78+ return tokens . map ( t => t . value ) . join ( '' )
79+ }
80+ // without <template></template>
81+ const df = context . parserServices . getDocumentFragment ?.( )
82+ if ( ! df ) {
83+ return ''
84+ }
85+ const start = i18nBlock . startTag . range [ 1 ]
86+ const end = i18nBlock . endTag . range [ 0 ]
87+ let sourceCode = ''
88+ for ( const token of df . tokens ) {
89+ if ( start <= token . range [ 0 ] && token . range [ 1 ] <= end ) {
90+ sourceCode += token . value
91+ }
92+ if ( end <= token . range [ 0 ] ) {
93+ break
94+ }
95+ }
96+ return sourceCode
97+ }
98+
5999/**
60100 * @param {RuleContext } context
61101 * @param {VElement } i18nBlock
@@ -68,19 +108,14 @@ function parseInI18nBlock<P extends JSONAST.JSONProgram | YAMLAST.YAMLProgram>(
68108 option : unknown
69109 ) => { ast : P ; visitorKeys : VisitorKeys }
70110) {
71- if ( ! i18nBlock . endTag ) {
111+ if ( ! hasEndTag ( i18nBlock ) ) {
72112 return null
73113 }
74- const offsetIndex = i18nBlock . startTag . range [ 1 ]
75- const tokenStore = context . parserServices . getTemplateBodyTokenStore ( )
76- const tokens = tokenStore . getTokensBetween (
77- i18nBlock . startTag ,
78- i18nBlock . endTag
79- )
80- const sourceString = tokens . map ( t => t . value ) . join ( '' )
114+ const sourceString = getSourceCodeString ( context , i18nBlock )
81115 if ( ! sourceString . trim ( ) ) {
82116 return null
83117 }
118+ const offsetIndex = i18nBlock . startTag . range [ 1 ]
84119 const sourceCode = context . getSourceCode ( )
85120 const locationFixer = new LocationFixer (
86121 sourceCode ,
0 commit comments