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

Commit a4e5bcb

Browse files
authored
fix: pickup root comments in template (#107)
* fix: pickup root comments in template * fix: test propHandler
1 parent f0a7f76 commit a4e5bcb

File tree

6 files changed

+82
-12
lines changed

6 files changed

+82
-12
lines changed

src/parse-template.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import cacher from './utils/cacher'
55

66
export interface TemplateParserOptions {
77
functional: boolean
8+
rootLeadingComment: string
89
}
910

1011
export type Handler = (
@@ -25,8 +26,16 @@ export default function parseTemplate(
2526
? pug.render(tpl.content, { filename: filePath })
2627
: tpl.content
2728
const ast = cacher(() => compile(template, { comments: true }).ast, template)
29+
const rootLeadingCommentArray = /^<!--(.+)-->/.exec(template.trim())
30+
const rootLeadingComment =
31+
rootLeadingCommentArray && rootLeadingCommentArray.length > 1
32+
? rootLeadingCommentArray[1].trim()
33+
: ''
2834
if (ast) {
29-
traverse(ast, documentation, handlers, { functional: !!tpl.attrs.functional })
35+
traverse(ast, documentation, handlers, {
36+
functional: !!tpl.attrs.functional,
37+
rootLeadingComment,
38+
})
3039
}
3140
}
3241
}

src/template-handlers/__tests__/propHandler.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('slotHandler', () => {
2020
{ comments: true },
2121
).ast
2222
if (ast) {
23-
traverse(ast, doc, [propHandler], { functional: true })
23+
traverse(ast, doc, [propHandler], { functional: true, rootLeadingComment: '' })
2424
expect(doc.toObject().props).toMatchObject({ size: { type: { name: 'undefined' } } })
2525
} else {
2626
fail()
@@ -40,7 +40,7 @@ describe('slotHandler', () => {
4040
{ comments: true },
4141
).ast
4242
if (ast) {
43-
traverse(ast, doc, [propHandler], { functional: true })
43+
traverse(ast, doc, [propHandler], { functional: true, rootLeadingComment: '' })
4444
expect(doc.toObject().props).toMatchObject({ name: { type: { name: 'undefined' } } })
4545
} else {
4646
fail()
@@ -58,7 +58,7 @@ describe('slotHandler', () => {
5858
{ comments: true },
5959
).ast
6060
if (ast) {
61-
traverse(ast, doc, [propHandler], { functional: true })
61+
traverse(ast, doc, [propHandler], { functional: true, rootLeadingComment: '' })
6262
expect(doc.toObject().props).toBeUndefined()
6363
} else {
6464
fail()
@@ -76,7 +76,7 @@ describe('slotHandler', () => {
7676
{ comments: true },
7777
).ast
7878
if (ast) {
79-
traverse(ast, doc, [propHandler], { functional: true })
79+
traverse(ast, doc, [propHandler], { functional: true, rootLeadingComment: '' })
8080
expect(doc.toObject().props).toBeUndefined()
8181
} else {
8282
fail()

src/template-handlers/__tests__/slotHandler.ts

+24-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,28 @@ describe('slotHandler', () => {
99
doc = new Documentation()
1010
})
1111

12+
it('should pick comments at the beginning of templates', () => {
13+
const ast = compile(
14+
[
15+
'<slot name="first">',
16+
' <div>',
17+
' <h1>titleof the template</h1>',
18+
' </div>',
19+
'</slot>',
20+
].join('\n'),
21+
{ comments: true },
22+
).ast
23+
if (ast) {
24+
traverse(ast, doc, [slotHandler], {
25+
functional: false,
26+
rootLeadingComment: '@slot first slot found',
27+
})
28+
expect(doc.toObject().slots.first).toMatchObject({ description: 'first slot found' })
29+
} else {
30+
fail()
31+
}
32+
})
33+
1234
it('should pick comments before slots', () => {
1335
const ast = compile(
1436
[
@@ -21,7 +43,7 @@ describe('slotHandler', () => {
2143
{ comments: true },
2244
).ast
2345
if (ast) {
24-
traverse(ast, doc, [slotHandler], { functional: false })
46+
traverse(ast, doc, [slotHandler], { functional: false, rootLeadingComment: '' })
2547
expect(doc.toObject().slots.default).toMatchObject({ description: 'a default slot' })
2648
} else {
2749
fail()
@@ -40,7 +62,7 @@ describe('slotHandler', () => {
4062
{ comments: true },
4163
).ast
4264
if (ast) {
43-
traverse(ast, doc, [slotHandler], { functional: false })
65+
traverse(ast, doc, [slotHandler], { functional: false, rootLeadingComment: '' })
4466
expect(doc.toObject().slots.oeuf).toMatchObject({ description: 'a slot named oeuf' })
4567
} else {
4668
fail()

src/template-handlers/slotHandler.ts

+15-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import { ASTElement, ASTNode } from 'vue-template-compiler'
22
import { Documentation } from '../Documentation'
3+
import { TemplateParserOptions } from '../parse-template'
34

4-
export default function slotHandler(documentation: Documentation, templateAst: ASTElement) {
5+
export default function slotHandler(
6+
documentation: Documentation,
7+
templateAst: ASTElement,
8+
options: TemplateParserOptions,
9+
) {
510
if (templateAst.tag === 'slot') {
611
const bindings = extractAndFilterAttr(templateAst.attrsMap)
712
let name = 'default'
@@ -13,6 +18,7 @@ export default function slotHandler(documentation: Documentation, templateAst: A
1318
const slotDescriptor = documentation.getSlotDescriptor(name)
1419

1520
slotDescriptor.bindings = bindings
21+
let comment = ''
1622
if (templateAst.parent) {
1723
const slotSiblings: ASTNode[] = templateAst.parent.children
1824
// First find the position of the slot in the list
@@ -42,13 +48,17 @@ export default function slotHandler(documentation: Documentation, templateAst: A
4248
templateAst.parent.tag === 'slot' && templateAst.parent.children[0] === potentialComment
4349
)
4450
) {
45-
const comment = potentialComment.text.trim()
46-
if (comment.search(/\@slot/) !== -1) {
47-
slotDescriptor.description = comment.replace('@slot', '').trim()
48-
}
51+
comment = potentialComment.text.trim()
52+
4953
break
5054
}
5155
}
56+
} else if (options.rootLeadingComment.length) {
57+
comment = options.rootLeadingComment
58+
}
59+
60+
if (comment.length && comment.search(/\@slot/) !== -1) {
61+
slotDescriptor.description = comment.replace('@slot', '').trim()
5262
}
5363
}
5464
}
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<template>
2+
<!-- @slot Use this slot default -->
3+
<slot name="wrapper">
4+
<button>
5+
Click Me
6+
</button>
7+
</slot>
8+
<template>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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.wrapper.description).toBe('Use this slot default')
20+
})
21+
})

0 commit comments

Comments
 (0)