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

Commit 9b918bf

Browse files
authored
fix: props definition priority is script then template (#93)
1 parent 7801fdf commit 9b918bf

File tree

6 files changed

+48
-13
lines changed

6 files changed

+48
-13
lines changed

src/parse.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ export function parseSource(documentation: Documentation, source: string, opt: P
4848
parts = cacher(() => parseComponent(source), source)
4949
}
5050

51+
// get slots and props from template
52+
if (parts && parts.template) {
53+
parseTemplate(parts.template, documentation, templateHandlers, opt.filePath)
54+
}
55+
5156
const scriptSource = parts ? (parts.script ? parts.script.content : undefined) : source
5257
if (scriptSource) {
5358
opt.lang =
@@ -59,11 +64,6 @@ export function parseSource(documentation: Documentation, source: string, opt: P
5964
parseScript(scriptSource, documentation, handlers, opt)
6065
}
6166

62-
// get slots from template
63-
if (parts && parts.template) {
64-
parseTemplate(parts.template, documentation, templateHandlers, opt.filePath)
65-
}
66-
6767
if (!documentation.get('displayName')) {
6868
// a component should always have a display name
6969
documentation.set('displayName', path.basename(opt.filePath).replace(/\.\w+$/, ''))

src/template-handlers/__tests__/propHandler.ts

+21-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('slotHandler', () => {
2121
).ast
2222
if (ast) {
2323
traverse(ast, doc, [propHandler], { functional: true })
24-
expect(doc.toObject().props).toMatchObject({ size: { type: { name: 'string' } } })
24+
expect(doc.toObject().props).toMatchObject({ size: { type: { name: 'undefined' } } })
2525
} else {
2626
fail()
2727
}
@@ -41,13 +41,13 @@ describe('slotHandler', () => {
4141
).ast
4242
if (ast) {
4343
traverse(ast, doc, [propHandler], { functional: true })
44-
expect(doc.toObject().props).toMatchObject({ name: { type: { name: 'string' } } })
44+
expect(doc.toObject().props).toMatchObject({ name: { type: { name: 'undefined' } } })
4545
} else {
4646
fail()
4747
}
4848
})
4949

50-
it('should not match props if in a litteral', () => {
50+
it('should not match props if in a string litteral', () => {
5151
const ast = compile(
5252
[
5353
'<div>',
@@ -64,4 +64,22 @@ describe('slotHandler', () => {
6464
fail()
6565
}
6666
})
67+
68+
it('should not match props if in a non evaluated attribute', () => {
69+
const ast = compile(
70+
[
71+
'<div>',
72+
' <h1>titleof the template</h1>',
73+
' <button style="width:props.size"></slot>',
74+
'</div>',
75+
].join('\n'),
76+
{ comments: true },
77+
).ast
78+
if (ast) {
79+
traverse(ast, doc, [propHandler], { functional: true })
80+
expect(doc.toObject().props).toBeUndefined()
81+
} else {
82+
fail()
83+
}
84+
})
6785
})

src/template-handlers/propHandler.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function getPropsFromExpression(expression: string, documentation: Documentation
5757
) {
5858
const pName = propName.name
5959
const p = documentation.getPropDescriptor(pName)
60-
p.type = { name: 'string' }
60+
p.type = { name: 'undefined' }
6161
}
6262
return false
6363
},

tests/components/button-functinoal/MyButton.vue

+9
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,12 @@
77
{{props.text}}
88
</div>
99
</template>
10+
11+
<script>
12+
export default {
13+
props: {
14+
size: Number,
15+
default: 200,
16+
},
17+
}
18+
</script>

tests/components/button-functinoal/__snapshots__/button-functional.test.ts.snap

+10-2
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,26 @@ Object {
77
"events": Object {},
88
"methods": Array [],
99
"props": Object {
10+
"default": Object {
11+
"description": "",
12+
"tags": Object {},
13+
"type": Object {
14+
"func": true,
15+
"name": "200",
16+
},
17+
},
1018
"size": Object {
1119
"description": "",
1220
"tags": Object {},
1321
"type": Object {
14-
"name": "string",
22+
"name": "number",
1523
},
1624
},
1725
"text": Object {
1826
"description": "",
1927
"tags": Object {},
2028
"type": Object {
21-
"name": "string",
29+
"name": "undefined",
2230
},
2331
},
2432
},

tests/components/button-functinoal/button-functional.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ describe('tests button functional', () => {
2121

2222
it('should extract props from template if functional', () => {
2323
expect(docButton.props).toMatchObject({
24-
size: { type: { name: 'string' } },
25-
text: { type: { name: 'string' } },
24+
size: { type: { name: 'number' } },
25+
text: { type: { name: 'undefined' } },
2626
})
2727
})
2828

0 commit comments

Comments
 (0)