/
generate-component-types.ts
70 lines (63 loc) · 2.38 KB
/
generate-component-types.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import * as d from '../../declarations';
import { dashToPascalCase, sortBy } from '@utils';
import { generateEventTypes } from './generate-event-types';
import { generateMethodTypes } from './generate-method-types';
import { generatePropTypes } from './generate-prop-types';
/**
* Generate a string based on the types that are defined within a component.
*
* @param cmp the metadata for the component that a type definition string is generated for
* @param importPath the path of the component file
*/
export function generateComponentTypes(cmp: d.ComponentCompilerMeta): d.TypesModule {
const tagName = cmp.tagName.toLowerCase();
const tagNameAsPascal = dashToPascalCase(tagName);
const htmlElementName = `HTML${tagNameAsPascal}Element`;
const propAttributes = generatePropTypes(cmp);
const methodAttributes = generateMethodTypes(cmp.methods);
const eventAttributes = generateEventTypes(cmp.events);
const stencilComponentAttributes = attributesToMultiLineString([
...propAttributes,
...methodAttributes
], false);
const isDep = cmp.isCollectionDependency;
const stencilComponentJSXAttributes = attributesToMultiLineString([
...propAttributes,
...eventAttributes
], true);
return {
isDep,
tagName,
tagNameAsPascal,
htmlElementName,
component: `interface ${tagNameAsPascal} {${stencilComponentAttributes}}`,
jsx: `interface ${tagNameAsPascal} {${stencilComponentJSXAttributes}}`,
element: `
interface ${htmlElementName} extends Components.${tagNameAsPascal}, HTMLStencilElement {}
var ${htmlElementName}: {
prototype: ${htmlElementName};
new (): ${htmlElementName};
};`,
};
}
function attributesToMultiLineString(attributes: d.TypeInfo, jsxAttributes: boolean, paddingString = '') {
const attributesStr = sortBy(attributes, a => a.name)
.filter(type => type.public || !jsxAttributes)
.reduce((fullList, type) => {
if (type.jsdoc) {
fullList.push(`/**`);
fullList.push(
...type.jsdoc.split('\n').map(line => ' * ' + line)
);
fullList.push(` */`);
}
const optional = (jsxAttributes)
? !type.required
: type.optional;
fullList.push(`'${type.name}'${ optional ? '?' : '' }: ${type.type};`);
return fullList;
}, [] as string[])
.map(item => `${paddingString}${item}`)
.join(`\n`);
return attributesStr !== '' ? `\n${attributesStr}\n` : '';
}