Skip to content

Commit

Permalink
feat(uniontype): fix formatting and link for union type aliases
Browse files Browse the repository at this point in the history
Co-authored-by: Adam Bradley <adamdbradley@users.noreply.github.com>
  • Loading branch information
imhoffd and adamdbradley committed Dec 15, 2020
1 parent d5bb6f3 commit 1e7818a
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 20 deletions.
11 changes: 6 additions & 5 deletions src/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,12 @@ function typeAliasTable(data: DocsData, t: DocsTypeAlias) {
o.push(``);
}

o.push(
`${t.types.map(ty => {
return formatType(data, ty.text).formatted;
})}`,
);
const type = t.types
.map(ty => formatType(data, ty.text).formatted)
.join(' | ')
.replace(/\<\/code\> \| \<code\>/g, ` | `);

o.push(`${type}`);
o.push(``);

return o.join(`\n`);
Expand Down
16 changes: 13 additions & 3 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,16 +333,26 @@ function getInterfaceProperty(
tags: docs.tags,
docs: docs.docs,
complexTypes: Array.from(referencedTypes),
type: typeToString(typeChecker, type),
type: typeToString(typeChecker, type, properytSignature.type),
};
return p;
}

function typeToString(checker: ts.TypeChecker, type: ts.Type) {
function typeToString(
checker: ts.TypeChecker,
type: ts.Type,
typeNode?: ts.TypeNode,
) {
if (typeNode && ts.isTypeReferenceNode(typeNode)) {
return typeNode.getText();
}

const TYPE_FORMAT_FLAGS =
ts.TypeFormatFlags.NoTruncation |
ts.TypeFormatFlags.NoTypeReduction |
ts.TypeFormatFlags.InElementType;
ts.TypeFormatFlags.WriteArrowStyleSignature |
ts.TypeFormatFlags.WriteTypeArgumentsOfSignature |
ts.TypeFormatFlags.UseSingleQuotesForStringLiteralType;

return checker.typeToString(type, undefined, TYPE_FORMAT_FLAGS);
}
Expand Down
18 changes: 13 additions & 5 deletions src/test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ Add a listener. Callback has <a href="#vibrateoptions">VibrateOptions</a>.

| Param | Type |
| ------------------ | ----------------------------------------------------------- |
| **`eventName`** | <code>"vibrate"</code> |
| **`eventName`** | <code>'vibrate'</code> |
| **`listenerFunc`** | <code><a href="#vibratelistener">VibrateListener</a></code> |

**Since:** 1.0.0
Expand Down Expand Up @@ -161,10 +161,11 @@ Remove all the listeners that are attached to this plugin

#### VibrateListenerEvent

| Prop | Type | Description | Since |
| -------------- | ----------------------------------------------------------------- | ------------------------------ | ----- |
| **`style`** | <code><a href="#hapticsimpactstyle">HapticsImpactStyle</a></code> | The style of vibration. | 1.0.0 |
| **`duration`** | <code>number</code> | The duration of the vibration. | 1.0.0 |
| Prop | Type | Description | Since |
| -------------- | ----------------------------------------------------------------- | ----------------------------------------- | ----- |
| **`style`** | <code><a href="#hapticsimpactstyle">HapticsImpactStyle</a></code> | The style of vibration. | 1.0.0 |
| **`duration`** | <code>number</code> | The duration of the vibration. | 1.0.0 |
| **`repeat`** | <code><a href="#repeatschedule">RepeatSchedule</a></code> | How often this vibrate event is repeated. | 1.0.0 |


### Type Aliases
Expand All @@ -177,6 +178,13 @@ The vibrate listener callback function.
<code>(event: <a href="#vibratelistenerevent">VibrateListenerEvent</a>): void</code>


#### RepeatSchedule

How often a vibration repeats.

<code>'hourly' | 'daily' | 'weekly' | 'monthly'</code>


### Enums


Expand Down
41 changes: 39 additions & 2 deletions src/test/docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
{
"name": "eventName",
"docs": "",
"type": "\"vibrate\""
"type": "'vibrate'"
},
{
"name": "listenerFunc",
Expand Down Expand Up @@ -261,7 +261,7 @@
"complexTypes": [
"HapticsImpactStyle"
],
"type": "HapticsImpactStyle | undefined"
"type": "HapticsImpactStyle"
},
{
"name": "duration",
Expand All @@ -274,6 +274,20 @@
"docs": "The duration of the vibration.",
"complexTypes": [],
"type": "number"
},
{
"name": "repeat",
"tags": [
{
"text": "1.0.0",
"name": "since"
}
],
"docs": "How often this vibrate event is repeated.",
"complexTypes": [
"RepeatSchedule"
],
"type": "RepeatSchedule"
}
]
}
Expand Down Expand Up @@ -371,6 +385,29 @@
]
}
]
},
{
"name": "RepeatSchedule",
"slug": "repeatschedule",
"docs": "How often a vibration repeats.",
"types": [
{
"text": "'hourly'",
"complexTypes": []
},
{
"text": "'daily'",
"complexTypes": []
},
{
"text": "'weekly'",
"complexTypes": []
},
{
"text": "'monthly'",
"complexTypes": []
}
]
}
]
}
12 changes: 12 additions & 0 deletions src/test/fixtures/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,20 @@ export interface VibrateListenerEvent {
* @since 1.0.0
*/
duration: number;

/**
* How often this vibrate event is repeated.
* @since 1.0.0
*/
repeat?: RepeatSchedule;
}

/**
* How often a vibration repeats.
* @since 1.0.0
*/
export type RepeatSchedule = 'hourly' | 'daily' | 'weekly' | 'monthly';

export interface HapticsImpact {
value: number;
}
Expand Down
19 changes: 14 additions & 5 deletions src/test/parse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('parse', () => {
expect(api.slug).toBe(`hapticsplugin`);
expect(api.docs).toContain(`Docs from JSDoc comments!`);
expect(interfaces).toHaveLength(5);
expect(typeAliases).toHaveLength(1);
expect(typeAliases).toHaveLength(2);
expect(enums).toHaveLength(2);

const iNames = interfaces.map(i => i.name);
Expand Down Expand Up @@ -122,12 +122,21 @@ describe('parse', () => {
expect(p0.complexTypes).toHaveLength(1);
expect(p0.complexTypes[0]).toBe(`HapticsImpactStyle`);
expect(p0.type).toBe(`HapticsImpactStyle`);

const i1 = interfaces.find(i => i.name === 'VibrateListenerEvent');
expect(i1.name).toBe('VibrateListenerEvent');
expect(i1.properties).toHaveLength(3);
expect(i1.properties[2].type).toBe('RepeatSchedule');
});

it('type typeAliases', () => {
const t = typeAliases.find(i => i.name === 'VibrateListener');
expect(t.slug).toBe(`vibratelistener`);
expect(t.docs).toBe(`The vibrate listener callback function.`);
expect(t.types).toHaveLength(1);
const t0 = typeAliases.find(i => i.name === 'VibrateListener');
expect(t0.slug).toBe(`vibratelistener`);
expect(t0.docs).toBe(`The vibrate listener callback function.`);
expect(t0.types).toHaveLength(1);

const t1 = typeAliases.find(i => i.name === 'RepeatSchedule');
expect(t1.slug).toBe(`repeatschedule`);
expect(t1.types).toHaveLength(4);
});
});

0 comments on commit 1e7818a

Please sign in to comment.