Skip to content

Commit

Permalink
feat(docs): expose type as array of values (#1913)
Browse files Browse the repository at this point in the history
  • Loading branch information
manucorporat committed Oct 3, 2019
1 parent f1c5fd5 commit 59b9a83
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 8 deletions.
47 changes: 47 additions & 0 deletions src/compiler/docs/generate-doc-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as d from '../../declarations';
import { AUTO_GENERATE_COMMENT } from './constants';
import { flatOne, isDocsPublic, normalizePath, sortBy } from '@utils';
import { getBuildTimestamp } from '../build/build-ctx';
import { JsonDocsValue } from '../../declarations';


export async function generateDocData(config: d.Config, compilerCtx: d.CompilerCtx, buildCtx: d.BuildCtx): Promise<d.JsonDocs> {
Expand Down Expand Up @@ -108,6 +109,7 @@ function getRealProperties(properties: d.ComponentCompilerProperty[]): d.JsonDoc
docsTags: member.docs.tags,
default: member.defaultValue,
deprecation: getDeprecation(member.docs.tags),
values: parseTypeIntoValues(member.complexType.resolved),

optional: member.optional,
required: member.required,
Expand All @@ -125,12 +127,57 @@ function getVirtualProperties(virtualProps: d.ComponentCompilerVirtualProperty[]
docsTags: [],
default: undefined,
deprecation: undefined,
values: parseTypeIntoValues(member.type),

optional: true,
required: false,
}));
}

function parseTypeIntoValues(type: string) {
if (typeof type === 'string') {
const unions = type.split('|').map(u => u.trim());
const parsedUnions: JsonDocsValue[] = [];
unions.forEach(u => {
if (u === 'true') {
parsedUnions.push({
value: 'true',
type: 'boolean'
});
return;
}
if (u === 'false') {
parsedUnions.push({
value: 'false',
type: 'boolean'
});
return;
}
if (!Number.isNaN(parseFloat(u))) {
// union is a number
parsedUnions.push({
value: u,
type: 'number'
});
return;
}
if (/^("|').+("|')$/gm.test(u)) {
// ionic is a string
parsedUnions.push({
value: u.slice(1, -1),
type: 'string'
});
return;
}
parsedUnions.push({
type: u
});
});
return parsedUnions;
}
return [];
}

function getMethods(methods: d.ComponentCompilerMethod[]): d.JsonDocsMethod[] {
return sortBy(methods, member => member.name)
.filter(member => isDocsPublic(member.docs))
Expand Down
14 changes: 6 additions & 8 deletions src/compiler/docs/vscode/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,12 @@ function serializeAttribute(prop: d.JsonDocsProp) {
'name': prop.attr,
'description': prop.docs,
};
if (typeof prop.type === 'string') {
const unions = prop.type.split('|').map(u => u.trim()).filter(u => u !== 'undefined' && u !== 'null');
const includeValues = unions.every(u => /^("|').+("|')$/gm.test(u));
if (includeValues) {
attribute.values = unions.map(u => ({
name: u.slice(1, -1)
}));
}
const values = prop.values
.filter(({ type, value }) => type === 'string' && value !== undefined)
.map(({ value }) => ({ name: value }));

if (values.length > 0) {
attribute.values = values;
}
return attribute;
}
6 changes: 6 additions & 0 deletions src/declarations/docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ export interface JsonDocsTag {
text?: string;
}

export interface JsonDocsValue {
value?: string;
type: string;
}

export interface JsonDocsUsage {
[key: string]: string;
}
Expand All @@ -59,6 +64,7 @@ export interface JsonDocsProp {
docsTags: JsonDocsTag[];
default: string;
deprecation: string | undefined;
values: JsonDocsValue[];

optional: boolean;
required: boolean;
Expand Down

0 comments on commit 59b9a83

Please sign in to comment.