Skip to content

Commit 1c28167

Browse files
committed
feat: detect model property
closes #654
1 parent 3739d94 commit 1c28167

File tree

2 files changed

+60
-3
lines changed

2 files changed

+60
-3
lines changed

packages/vue-docgen-api/src/script-handlers/__tests__/propHandler.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,30 @@ describe('propHandler', () => {
377377
expect(documentation.getPropDescriptor).not.toHaveBeenCalledWith('value')
378378
expect(documentation.getPropDescriptor).toHaveBeenCalledWith('v-model')
379379
})
380+
381+
it('should set the v-model instead of value', () => {
382+
const src = `
383+
export default {
384+
model:{
385+
prop: 'value'
386+
},
387+
props: {
388+
/**
389+
* Value of the field
390+
*/
391+
value: {
392+
required: true,
393+
type: undefined
394+
}
395+
}
396+
}
397+
`
398+
tester(src, {
399+
description: 'Value of the field'
400+
})
401+
expect(documentation.getPropDescriptor).not.toHaveBeenCalledWith('value')
402+
expect(documentation.getPropDescriptor).toHaveBeenCalledWith('v-model')
403+
})
380404
})
381405

382406
describe('@values tag parsing', () => {

packages/vue-docgen-api/src/script-handlers/propHandler.ts

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ export default async function propHandler(documentation: Documentation, path: No
2020
return
2121
}
2222

23+
const modelPropertyName = getModelPropName(path)
24+
2325
const propsValuePath = propsPath[0].get('value')
2426

2527
if (bt.isObjectExpression(propsValuePath.node)) {
@@ -38,9 +40,10 @@ export default async function propHandler(documentation: Documentation, path: No
3840
const jsDocTags: BlockTag[] = jsDoc.tags ? jsDoc.tags : []
3941

4042
// if it's the v-model describe it only as such
41-
const propName = jsDocTags.some(t => t.title === 'model')
42-
? 'v-model'
43-
: propNode.key.name || propNode.key.value
43+
const propertyName = propNode.key.name || propNode.key.value
44+
const isPropertyModel =
45+
jsDocTags.some(t => t.title === 'model') || propertyName === modelPropertyName
46+
const propName = isPropertyModel ? 'v-model' : propertyName
4447

4548
const propDescriptor = documentation.getPropDescriptor(propName)
4649

@@ -226,3 +229,33 @@ export function extractValuesFromTags(propDescriptor: PropDescriptor) {
226229
delete propDescriptor.tags['values']
227230
}
228231
}
232+
233+
/**
234+
* extract the property model.prop from the component object
235+
* @param path component NodePath
236+
* @returns name of the model prop, null if none
237+
*/
238+
function getModelPropName(path: NodePath): string | null {
239+
const modelPath = path
240+
.get('properties')
241+
.filter((p: NodePath) => bt.isObjectProperty(p.node) && getMemberFilter('model')(p))
242+
243+
if (!modelPath.length) {
244+
return null
245+
}
246+
247+
const modelPropertyNamePath =
248+
modelPath.length &&
249+
modelPath[0]
250+
.get('value')
251+
.get('properties')
252+
.filter((p: NodePath) => bt.isObjectProperty(p.node) && getMemberFilter('prop')(p))
253+
254+
if (!modelPropertyNamePath.length) {
255+
return null
256+
}
257+
258+
const valuePath = modelPropertyNamePath[0].get('value')
259+
260+
return bt.isStringLiteral(valuePath.node) ? valuePath.node.value : null
261+
}

0 commit comments

Comments
 (0)