Skip to content
This repository was archived by the owner on Mar 8, 2019. It is now read-only.

Commit f1be657

Browse files
authored
fix: add scoped flag to slot descriptor (#118)
closes #31
1 parent c0cb2db commit f1be657

File tree

6 files changed

+73
-1
lines changed

6 files changed

+73
-1
lines changed

src/Documentation.ts

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export interface MethodDescriptor {
6767
export interface SlotDescriptor {
6868
description?: string
6969
bindings?: Record<string, any>
70+
scoped?: boolean
7071
}
7172

7273
export interface ComponentDoc {

src/template-handlers/__tests__/slotHandler.ts

+21
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,25 @@ describe('slotHandler', () => {
6868
fail()
6969
}
7070
})
71+
72+
it('should detect scoped slots', () => {
73+
const ast = compile(
74+
[
75+
'<div title="a list of item with a scope" >',
76+
' <!-- @slot a slot named oeuf -->',
77+
' <slot name="oeuf" v-for="item in items" :item="item"/>',
78+
'</div>',
79+
].join('\n'),
80+
{ comments: true },
81+
).ast
82+
if (ast) {
83+
traverse(ast, doc, [slotHandler], { functional: false, rootLeadingComment: '' })
84+
expect(doc.toObject().slots.oeuf).toMatchObject({
85+
scoped: true,
86+
description: 'a slot named oeuf',
87+
})
88+
} else {
89+
fail()
90+
}
91+
})
7192
})

src/template-handlers/slotHandler.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ export default function slotHandler(
1717

1818
const slotDescriptor = documentation.getSlotDescriptor(name)
1919

20+
if (bindings && Object.keys(bindings).length) {
21+
slotDescriptor.scoped = true
22+
}
23+
2024
slotDescriptor.bindings = bindings
2125
let comment = ''
2226
if (templateAst.parent) {
@@ -34,7 +38,7 @@ export default function slotHandler(
3438
const slotSiblingsBeforeSlot = slotSiblings.slice(0, currentSlotIndex).reverse()
3539

3640
for (const potentialComment of slotSiblingsBeforeSlot) {
37-
// if there is text between the slot and the comment ignore
41+
// if there is text between the slot and the comment, ignore
3842
if (
3943
potentialComment.type !== 3 ||
4044
(!potentialComment.isComment && potentialComment.text.trim())
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<template>
2+
<div class="modal-footer">
3+
<!-- @slot Modal footer here -->
4+
<slot name="footer" v-bind:item="item"/>
5+
</div>
6+
</template>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`tests wrapper with root slot should match the reference for the footer slot 1`] = `
4+
Object {
5+
"bindings": Object {
6+
":item": "item",
7+
},
8+
"description": "Modal footer here",
9+
"scoped": true,
10+
}
11+
`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import * as path from 'path'
2+
import { ComponentDoc } from '../../../src/Documentation'
3+
import { parse } from '../../../src/main'
4+
5+
const button = path.join(__dirname, './Wrapper.vue')
6+
let docButton: ComponentDoc
7+
8+
describe('tests wrapper with root slot', () => {
9+
beforeEach(done => {
10+
docButton = parse(button)
11+
done()
12+
})
13+
14+
it('should have a slot.', () => {
15+
expect(Object.keys(docButton.slots).length).toEqual(1)
16+
})
17+
18+
it('should have a wrapper slot.', () => {
19+
expect(docButton.slots.footer.description).toBe('Modal footer here')
20+
})
21+
22+
it('should show the slot as scoped', () => {
23+
expect(docButton.slots.footer.scoped).toBeTruthy()
24+
})
25+
26+
it('should match the reference for the footer slot', () => {
27+
expect(docButton.slots.footer).toMatchSnapshot()
28+
})
29+
})

0 commit comments

Comments
 (0)