@@ -35,6 +35,21 @@ function getSlotsName(node) {
3535 return null
3636}
3737
38+ /**
39+ * @param {VElement } node
40+ * @return {VAttribute | VDirective | undefined }
41+ */
42+ function getSlotNameNode ( node ) {
43+ return node . startTag . attributes . find (
44+ ( node ) =>
45+ ( ! node . directive && node . key . name === 'name' ) ||
46+ ( node . directive &&
47+ node . key . name . name === 'bind' &&
48+ node . key . argument ?. type === 'VIdentifier' &&
49+ node . key . argument ?. name === 'name' )
50+ )
51+ }
52+
3853module . exports = {
3954 meta : {
4055 type : 'problem' ,
@@ -68,6 +83,19 @@ module.exports = {
6883 }
6984 const slotsDefined = new Set ( )
7085
86+ /**
87+ * @param {VElement } node
88+ * @param {string | undefined } slotName
89+ */
90+ function reportMissingSlot ( node , slotName ) {
91+ if ( ! slotsDefined . has ( slotName ) ) {
92+ context . report ( {
93+ node,
94+ messageId : 'requireExplicitSlots'
95+ } )
96+ }
97+ }
98+
7199 return utils . compositingVisitors (
72100 utils . defineScriptSetupVisitor ( context , {
73101 onDefineSlotsEnter ( node ) {
@@ -137,20 +165,27 @@ module.exports = {
137165 }
138166 } ) ,
139167 utils . defineTemplateBodyVisitor ( context , {
168+ /** @param {VElement } node */
140169 "VElement[name='slot']" ( node ) {
141- let slotName = 'default'
142-
143- const slotNameAttr = utils . getAttribute ( node , 'name' )
170+ const nameNode = getSlotNameNode ( node )
144171
145- if ( slotNameAttr ?. value ) {
146- slotName = slotNameAttr . value . value
172+ // if no slot name is declared, default to 'default'
173+ if ( ! nameNode ) {
174+ reportMissingSlot ( node , 'default' )
175+ return
147176 }
148177
149- if ( ! slotsDefined . has ( slotName ) ) {
150- context . report ( {
151- node,
152- messageId : 'requireExplicitSlots'
153- } )
178+ if ( nameNode . directive ) {
179+ const expression = nameNode . value ?. expression
180+ // ignore attribute binding except string literal
181+ if ( ! expression || ! utils . isStringLiteral ( expression ) ) {
182+ return
183+ }
184+
185+ const name = utils . getStringLiteralValue ( expression ) || undefined
186+ reportMissingSlot ( node , name )
187+ } else {
188+ reportMissingSlot ( node , nameNode . value ?. value )
154189 }
155190 }
156191 } )
0 commit comments