Skip to content

Commit 3c0febc

Browse files
committed
feat: add helpers package
1 parent 702536a commit 3c0febc

File tree

5 files changed

+722
-0
lines changed

5 files changed

+722
-0
lines changed

packages/helpers/helpers.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
const has = arr => Array.isArray(arr) && arr?.length > 0;
2+
3+
/** Package */
4+
export function hasModules(_package) {
5+
return has(_package?.modules);
6+
}
7+
8+
/** JavaScriptModule */
9+
export function hasExports(_module) {
10+
return has(_module?.exports);
11+
}
12+
13+
export function hasDeclarations(_module) {
14+
return has(_module?.declarations);
15+
}
16+
17+
export function isJavaScriptModule(_module) {
18+
return _module.kind === 'javascript-module';
19+
}
20+
21+
/** Exports */
22+
export function isCustomElementExport(_export) {
23+
return _export.kind === 'custom-element-definition';
24+
}
25+
26+
export function isJavaScriptExport(_export) {
27+
return _export.kind === 'js';
28+
}
29+
30+
/** Declarations */
31+
export function isClass(item) {
32+
return item.kind === 'class';
33+
}
34+
35+
export function isMixin(item) {
36+
return item.kind === 'mixin';
37+
}
38+
39+
export function isFunction(item) {
40+
return item.kind === 'function';
41+
}
42+
43+
export function isVariable(item) {
44+
return item.kind === 'variable';
45+
}
46+
47+
/** CustomElement */
48+
export function hasAttributes(customElement) {
49+
return has(customElement?.attributes)
50+
}
51+
52+
export function hasEvents(customElement) {
53+
return has(customElement?.events)
54+
}
55+
56+
export function hasSlots(customElement) {
57+
return has(customElement?.slots)
58+
}
59+
60+
export function hasMethods(customElement) {
61+
return (
62+
has(customElement?.members) &&
63+
customElement?.members?.some(member => member.kind === 'method')
64+
);
65+
}
66+
67+
export function hasFields(customElement) {
68+
return (
69+
has(customElement?.members) &&
70+
customElement?.members?.some(member => member.kind === 'field')
71+
);
72+
}
73+
74+
export function hasMixins(customElement) {
75+
return has(customElement?.mixins);
76+
}
77+
78+
/** ClassMember */
79+
export function isField(member) {
80+
return member.kind === 'field';
81+
}
82+
83+
export function isMethod(member) {
84+
return member.kind === 'method';
85+
}

packages/helpers/index.js

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
import * as h from './helpers.js';
2+
3+
export class CustomElementsJson {
4+
schemaVersion;
5+
readme;
6+
modules;
7+
8+
_classes = new Map();
9+
_tagNames = new Map();
10+
_definitions = new Map();
11+
_mixins = new Map();
12+
13+
constructor(
14+
{ schemaVersion, readme, modules } = {
15+
schemaVersion: '0.1.0',
16+
readme: '',
17+
modules: [],
18+
},
19+
) {
20+
this.schemaVersion = schemaVersion;
21+
this.readme = readme;
22+
this.modules = modules;
23+
this.init();
24+
}
25+
26+
init() {
27+
this.loopAll((item) => {
28+
if (h.isClass(item)) {
29+
this._classes.set(item.name, item);
30+
}
31+
32+
if (h.isMixin(item)) {
33+
this._mixins.set(item.name, item);
34+
}
35+
36+
if (h.isCustomElementExport(item)) {
37+
this._tagNames.set(
38+
item.name,
39+
this._classes.get(item.declaration.name),
40+
);
41+
42+
this._definitions.set(item.name, item);
43+
}
44+
});
45+
}
46+
47+
loopAll(cb) {
48+
this.modules.forEach((mod) => {
49+
mod?.exports?.forEach((ex) => {
50+
cb(ex);
51+
});
52+
53+
mod?.declarations?.forEach((declaration) => {
54+
cb(declaration);
55+
});
56+
});
57+
}
58+
59+
getByTagName(tagName) {
60+
return this._tagNames.get(tagName);
61+
}
62+
63+
getByClassName(className) {
64+
return this._classes.get(className);
65+
}
66+
67+
getByMixinName(mixinName) {
68+
return this._mixins.get(mixinName);
69+
}
70+
71+
/** Gets all classes from declarations */
72+
getClasses() {
73+
return [...this._classes.values()];
74+
}
75+
76+
/** Gets registered custom elements, so elements that have customElements.define called, returns class including tagName */
77+
getTagNames() {
78+
return [...this._tagNames.values()];
79+
}
80+
81+
/** Gets all CustomElementDefinitions */
82+
getDefinitions() {
83+
return [...this._definitions.values()];
84+
}
85+
86+
87+
getMixins() {
88+
return [...this._mixins.values()];
89+
}
90+
91+
// @TODO
92+
getInheritanceTree(className) {
93+
const tree = [];
94+
95+
let klass = this._classes.get(className);
96+
97+
if (klass) {
98+
tree.push(klass);
99+
100+
klass?.mixins?.forEach((mixin) => {
101+
tree.push(this._mixins.get(mixin.name));
102+
});
103+
104+
while (this._classes.has(klass.superclass.name)) {
105+
const newKlass = this._classes.get(klass.superclass.name);
106+
107+
newKlass?.mixins?.forEach((mixin) => {
108+
tree.push(this._mixins.get(mixin.name));
109+
});
110+
111+
tree.push(newKlass);
112+
klass = newKlass;
113+
}
114+
115+
return tree;
116+
}
117+
return [];
118+
}
119+
120+
getModuleForClass(className) {
121+
let result = undefined;
122+
123+
this.modules.forEach((mod) => {
124+
mod?.declarations?.forEach((declaration) => {
125+
if (h.isClass(declaration) && declaration.name === className) {
126+
result = mod.path;
127+
}
128+
});
129+
});
130+
131+
return result;
132+
}
133+
134+
getModuleForMixin(className) {
135+
let result = undefined;
136+
137+
this.modules.forEach((mod) => {
138+
mod?.declarations?.forEach((declaration) => {
139+
if (h.isMixin(declaration) && declaration.name === className) {
140+
result = mod.path;
141+
}
142+
});
143+
});
144+
145+
return result;
146+
}
147+
}

0 commit comments

Comments
 (0)