refactor: simplify slot extraneous child check
This commit is contained in:
		
							parent
							
								
									6461b3853e
								
							
						
					
					
						commit
						a792d697a3
					
				| @ -66,57 +66,54 @@ export function buildSlots( | |||||||
|   // 2. Iterate through children and check for template slots
 |   // 2. Iterate through children and check for template slots
 | ||||||
|   //    <template v-slot:foo="{ prop }">
 |   //    <template v-slot:foo="{ prop }">
 | ||||||
|   let hasTemplateSlots = false |   let hasTemplateSlots = false | ||||||
|  |   let extraneousChild: ChildNode | undefined = undefined | ||||||
|   const seenSlotNames = new Set<string>() |   const seenSlotNames = new Set<string>() | ||||||
|   const nonSlotChildren: ChildNode[] = [] |  | ||||||
|   for (let i = 0; i < children.length; i++) { |   for (let i = 0; i < children.length; i++) { | ||||||
|     const child = children[i] |     const child = children[i] | ||||||
|  |     let slotDir | ||||||
|     if ( |     if ( | ||||||
|       child.type === NodeTypes.ELEMENT && |       child.type === NodeTypes.ELEMENT && | ||||||
|       child.tagType === ElementTypes.TEMPLATE |       child.tagType === ElementTypes.TEMPLATE && | ||||||
|  |       (slotDir = child.props.find(isVSlot)) | ||||||
|     ) { |     ) { | ||||||
|       const { props, children, loc: nodeLoc } = child |       hasTemplateSlots = true | ||||||
|       const slotDir = props.find(isVSlot) |       const { children, loc: nodeLoc } = child | ||||||
|       if (slotDir) { |       const { arg: slotName, exp: slotProps, loc: dirLoc } = slotDir | ||||||
|         hasTemplateSlots = true |       if (explicitDefaultSlot) { | ||||||
|         const { arg: slotName, exp: slotProps, loc: dirLoc } = slotDir |         // already has on-component default slot - this is incorrect usage.
 | ||||||
|         if (explicitDefaultSlot) { |         context.onError( | ||||||
|           // already has on-component default slot - this is incorrect usage.
 |           createCompilerError(ErrorCodes.X_MIXED_SLOT_USAGE, dirLoc) | ||||||
|           context.onError( |         ) | ||||||
|             createCompilerError(ErrorCodes.X_MIXED_SLOT_USAGE, dirLoc) |         break | ||||||
|           ) |  | ||||||
|           break |  | ||||||
|         } else { |  | ||||||
|           // check duplicate slot names
 |  | ||||||
|           if ( |  | ||||||
|             !slotName || |  | ||||||
|             (slotName.type === NodeTypes.SIMPLE_EXPRESSION && slotName.isStatic) |  | ||||||
|           ) { |  | ||||||
|             const name = slotName ? slotName.content : `default` |  | ||||||
|             if (seenSlotNames.has(name)) { |  | ||||||
|               context.onError( |  | ||||||
|                 createCompilerError(ErrorCodes.X_DUPLICATE_SLOT_NAMES, dirLoc) |  | ||||||
|               ) |  | ||||||
|               continue |  | ||||||
|             } |  | ||||||
|             seenSlotNames.add(name) |  | ||||||
|           } |  | ||||||
|           slots.push( |  | ||||||
|             buildSlot(slotName || `default`, slotProps, children, nodeLoc) |  | ||||||
|           ) |  | ||||||
|         } |  | ||||||
|       } else { |       } else { | ||||||
|         nonSlotChildren.push(child) |         // check duplicate slot names
 | ||||||
|  |         if ( | ||||||
|  |           !slotName || | ||||||
|  |           (slotName.type === NodeTypes.SIMPLE_EXPRESSION && slotName.isStatic) | ||||||
|  |         ) { | ||||||
|  |           const name = slotName ? slotName.content : `default` | ||||||
|  |           if (seenSlotNames.has(name)) { | ||||||
|  |             context.onError( | ||||||
|  |               createCompilerError(ErrorCodes.X_DUPLICATE_SLOT_NAMES, dirLoc) | ||||||
|  |             ) | ||||||
|  |             continue | ||||||
|  |           } | ||||||
|  |           seenSlotNames.add(name) | ||||||
|  |         } | ||||||
|  |         slots.push( | ||||||
|  |           buildSlot(slotName || `default`, slotProps, children, nodeLoc) | ||||||
|  |         ) | ||||||
|       } |       } | ||||||
|     } else { |     } else if (!extraneousChild) { | ||||||
|       nonSlotChildren.push(child) |       extraneousChild = child | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   if (hasTemplateSlots && nonSlotChildren.length) { |   if (hasTemplateSlots && extraneousChild) { | ||||||
|     context.onError( |     context.onError( | ||||||
|       createCompilerError( |       createCompilerError( | ||||||
|         ErrorCodes.X_EXTRANEOUS_NON_SLOT_CHILDREN, |         ErrorCodes.X_EXTRANEOUS_NON_SLOT_CHILDREN, | ||||||
|         nonSlotChildren[0].loc |         extraneousChild.loc | ||||||
|       ) |       ) | ||||||
|     ) |     ) | ||||||
|   } |   } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user