-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathmanifest.ts
140 lines (124 loc) · 3.53 KB
/
manifest.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import type {
Attribute,
ClassLike,
ClassMember,
ClassMethod,
CssCustomProperty,
CssPart,
CustomElement,
CustomElementDeclaration,
CustomElementExport,
CustomElementField,
Event,
Export,
Package,
Slot
} from 'custom-elements-manifest/schema';
// FIXME: remove once new custom-elements-manifest version is released
// https://github.com/webcomponents/custom-elements-manifest/pull/118
type ClassField = CustomElementField & {
/**
* Whether the property is read-only.
*/
readonly?: boolean;
};
export type {
Attribute,
ClassField,
ClassMember,
ClassMethod,
CssCustomProperty,
CssPart,
CustomElement,
Event,
Package,
Slot
};
export function hasCustomElements(
manifest?: Package | null
): manifest is Package {
return (
!!manifest &&
Array.isArray(manifest.modules) &&
manifest.modules.some(
(x) =>
x.exports?.some((y) => y.kind === 'custom-element-definition') ||
x.declarations?.some((z) => (z as CustomElement).customElement)
)
);
}
const isCustomElementExport = (y: Export): y is CustomElementExport =>
y.kind === 'custom-element-definition';
const isCustomElementDeclaration = (y: ClassLike): y is CustomElement =>
(y as CustomElement).customElement;
const isPublic = (x: ClassMember): boolean =>
!(x.privacy === 'private' || x.privacy === 'protected');
export async function fetchManifest(src: string): Promise<Package | null> {
try {
const file = await fetch(src);
const manifest: Package = await file.json();
if (hasCustomElements(manifest)) {
return manifest;
}
throw new Error(`No element definitions found at ${src}`);
} catch (e) {
console.error(e);
return null;
}
}
export function getCustomElements(
manifest: Package,
only?: string[]
): CustomElementExport[] {
const exports = (manifest.modules ?? []).flatMap(
(x) => x.exports?.filter(isCustomElementExport) ?? []
);
return only ? exports.filter((e) => only.includes(e.name)) : exports;
}
export const getElementData = (
manifest: Package,
elements: CustomElementExport[],
selected?: string
): CustomElement | null => {
const index = selected
? elements.findIndex((el) => el?.name === selected)
: 0;
const element = elements[index];
if (!element) {
return null;
}
const { name, module } = element.declaration;
const decl = !module
? manifest.modules
.flatMap((x) => x.declarations)
.find((y): y is CustomElementDeclaration => y?.name === name)
: manifest.modules
.find((m) => m.path === module.replace(/^\//, ''))
?.declarations?.find((d) => d.name === name);
if (!decl || !isCustomElementDeclaration(decl)) {
throw new Error(`Could not find declaration for ${selected}`);
}
return {
customElement: true,
name: element.name,
description: decl?.description,
slots: decl.slots ?? [],
attributes: decl.attributes ?? [],
members: decl.members ?? [],
events: decl.events ?? [],
cssParts: decl.cssParts ?? [],
// TODO: analyzer should sort CSS custom properties
cssProperties: [...(decl.cssProperties ?? [])].sort((a, b) =>
a.name > b.name ? 1 : -1
),
deprecated: decl.deprecated
};
};
export const getPublicFields = (members: ClassMember[] = []): ClassField[] =>
members.filter(
(x: ClassMember): x is ClassField => x.kind === 'field' && isPublic(x)
);
export const getPublicMethods = (members: ClassMember[] = []): ClassMethod[] =>
members.filter(
(x: ClassMember): x is ClassMethod => x.kind === 'method' && isPublic(x)
);