Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addon-docs/Vue: Fix string docgen #14200

Merged
merged 2 commits into from
Mar 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions addons/docs/src/lib/docgen/createPropDef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@ function createDefaultValue(
type: DocgenType
): PropDefaultValue {
if (defaultValue != null) {
const { value, computed } = defaultValue;
const { value, computed, func } = defaultValue;


if (!isDefaultValueBlacklisted(value)) {
// Work around a bug in `react-docgen-typescript-loader`, which returns 'string' for a string
// default, instead of "'string'" -- which is incorrect (PR to RDT to follow)
if (typeof computed === 'undefined' && type.name === 'string') {
if (
typeof computed === 'undefined' &&
typeof func === 'undefined' &&
type.name === 'string'
) {
return createSummaryValue(JSON.stringify(value));
}

Expand Down
19 changes: 19 additions & 0 deletions addons/docs/src/lib/docgen/extractDocgenProps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,25 @@ TypeSystems.forEach((x) => {
expect(propDef.required).toBe(false);
expect(propDef.defaultValue.summary).toBe('"Default"');
});

it('should map defaults docgen info properly, vue', () => {
const component = createComponent({
...createStringType(x),
description: 'Hey! Hey!',
defaultValue: {
value: "'Default'",
func: false,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is func always false in these cases?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose func is probably true when you are using one of them vue default value "generators". I haven't explored the full space of everything vue can do though :/

},
});

const { propDef } = extractComponentProps(component, DOCGEN_SECTION)[0];

expect(propDef.name).toBe(PROP_NAME);
expect(propDef.type.summary).toBe('string');
expect(propDef.description).toBe('Hey! Hey!');
expect(propDef.required).toBe(false);
expect(propDef.defaultValue.summary).toBe("'Default'");
});
}

it('should remove JSDoc tags from the description', () => {
Expand Down
1 change: 1 addition & 0 deletions addons/docs/src/lib/docgen/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export interface DocgenTypeScriptType extends DocgenType {}
export interface DocgenPropDefaultValue {
value: string;
computed?: boolean;
func?: boolean;
}

export interface DocgenInfo {
Expand Down
8 changes: 7 additions & 1 deletion examples/vue-3-cli/src/stories/Button.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<template>
<button type="button" :class="classes" @click="onClick" :style="style">{{ label }}</button>
<button type="button" :class="classes" @click="onClick" :style="style">
{{ label }} - {{ sublabel }}
</button>
</template>

<script lang="typescript">
Expand All @@ -14,6 +16,10 @@ export default {
type: String,
required: true,
},
sublabel: {
type: String,
default: 'sublabel',
},
primary: {
type: Boolean,
default: false,
Expand Down