@@ -56,6 +56,46 @@ export interface YAMLParsed extends Parsed<YAMLAST.YAMLNode> {
56
56
ast : YAMLAST . YAMLProgram
57
57
}
58
58
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
+
59
99
/**
60
100
* @param {RuleContext } context
61
101
* @param {VElement } i18nBlock
@@ -68,19 +108,14 @@ function parseInI18nBlock<P extends JSONAST.JSONProgram | YAMLAST.YAMLProgram>(
68
108
option : unknown
69
109
) => { ast : P ; visitorKeys : VisitorKeys }
70
110
) {
71
- if ( ! i18nBlock . endTag ) {
111
+ if ( ! hasEndTag ( i18nBlock ) ) {
72
112
return null
73
113
}
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 )
81
115
if ( ! sourceString . trim ( ) ) {
82
116
return null
83
117
}
118
+ const offsetIndex = i18nBlock . startTag . range [ 1 ]
84
119
const sourceCode = context . getSourceCode ( )
85
120
const locationFixer = new LocationFixer (
86
121
sourceCode ,
0 commit comments