UNPKG

@custom-elements-manifest/helpers

Version:
254 lines 9.73 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __exportStar = (this && this.__exportStar) || function(m, exports) { for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); }; var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, privateMap, value) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to set private field on non-instance"); } privateMap.set(receiver, value); return value; }; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, privateMap) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to get private field on non-instance"); } return privateMap.get(receiver); }; var _classes, _tagNames, _definitions, _mixins, _initialized; Object.defineProperty(exports, "__esModule", { value: true }); exports.CustomElementsJson = void 0; const h = __importStar(require("./helpers")); class CustomElementsJson { constructor({ schemaVersion, readme, modules } = { schemaVersion: '0.1.0', readme: '', modules: [], }) { _classes.set(this, new Map()); _tagNames.set(this, new Map()); _definitions.set(this, new Map()); _mixins.set(this, new Map()); _initialized.set(this, false); this.schemaVersion = schemaVersion; this.readme = readme; this.modules = modules; } loopAll(cb) { this.modules.forEach((_module) => { if (h.hasExports(_module)) { _module.exports.forEach((_export) => { cb(_export); }); } if (h.hasDeclarations(_module)) { _module.declarations.forEach((declaration) => { cb(declaration); }); } }); } reset() { this.modules = []; __classPrivateFieldSet(this, _classes, new Map()); __classPrivateFieldSet(this, _tagNames, new Map()); __classPrivateFieldSet(this, _definitions, new Map()); __classPrivateFieldSet(this, _mixins, new Map()); __classPrivateFieldSet(this, _initialized, false); } init() { __classPrivateFieldSet(this, _initialized, true); this.loopAll((item) => { if (h.isClass(item) && item.superclass) { __classPrivateFieldGet(this, _classes).set(item.name, item); } }); this.loopAll((item) => { if (h.isMixin(item)) { __classPrivateFieldGet(this, _mixins).set(item.name, item); } }); this.loopAll((item) => { if (h.isCustomElementExport(item)) { __classPrivateFieldGet(this, _tagNames).set(item.name, __classPrivateFieldGet(this, _classes).get(item.declaration.name)); // get all CustomElementExports __classPrivateFieldGet(this, _definitions).set(item.name, item); } }); } getByTagName(tagName) { return __classPrivateFieldGet(this, _tagNames).get(tagName); } getByClassName(className) { return __classPrivateFieldGet(this, _classes).get(className); } getByMixinName(className) { return __classPrivateFieldGet(this, _mixins).get(className); } /** Gets all classes from declarations */ getClasses() { if (__classPrivateFieldGet(this, _initialized) === false) { this.init(); __classPrivateFieldSet(this, _initialized, true); } const classes = []; for (let [_, val] of __classPrivateFieldGet(this, _classes)) { classes.push(val); } return classes; } /** Gets registered custom elements, so elements that have customElements.define called, returns class including tagName */ getTagNames() { if (!__classPrivateFieldGet(this, _initialized)) { this.init(); __classPrivateFieldSet(this, _initialized, true); } const definitions = []; for (let [_, val] of __classPrivateFieldGet(this, _tagNames)) { definitions.push(val); } return definitions; } /** Gets all CustomElementDefinitions */ getDefinitions() { if (!__classPrivateFieldGet(this, _initialized)) { this.init(); __classPrivateFieldSet(this, _initialized, true); } const definitions = []; for (let [_, val] of __classPrivateFieldGet(this, _definitions)) { definitions.push(val); } return definitions; } // private initMixins() { // let foundMixins = new Map(); // let _mixins: Reference[] = []; // this.loopAllExports((_export) => { // if (h.hasMixins(_export as CustomElement)) { // const { mixins } = _export as CustomElement; // [...<Reference[]>mixins].forEach((mixin) => { // foundMixins.set(mixin.name, mixin); // }); // } // }); // this.loopAllExports((_export) => { // if (h.isVariable(_export as Variable)) { // const mergedMixin = { // ...foundMixins.get(_export.name), // ..._export // }; // _mixins.push(mergedMixin); // } // }); // [...new Set([..._mixins])].forEach((mixin) => { // this.mixins.set(mixin.name, mixin); // }); // } getMixins() { if (!__classPrivateFieldGet(this, _initialized)) { this.init(); __classPrivateFieldSet(this, _initialized, true); } const mixins = []; for (const [, val] of __classPrivateFieldGet(this, _mixins)) { mixins.push(val); } return mixins; } getInheritanceTree(className) { if (!__classPrivateFieldGet(this, _initialized)) { this.init(); __classPrivateFieldSet(this, _initialized, true); } const tree = []; let klass = __classPrivateFieldGet(this, _classes).get(className); if (klass) { tree.push(klass); if (h.hasMixins(klass)) { klass.mixins.forEach((mixin) => { tree.push(__classPrivateFieldGet(this, _mixins).get(mixin.name)); }); } while (__classPrivateFieldGet(this, _classes).has(klass.superclass.name)) { const newKlass = __classPrivateFieldGet(this, _classes).get(klass.superclass.name); if (h.hasMixins(newKlass)) { newKlass.mixins.forEach((mixin) => { tree.push(__classPrivateFieldGet(this, _mixins).get(mixin.name)); }); } tree.push(newKlass); klass = newKlass; } return tree; } else { return []; } } getModuleForClass(className) { if (!__classPrivateFieldGet(this, _initialized)) { this.init(); __classPrivateFieldSet(this, _initialized, true); } let result = undefined; this.modules.forEach((_module) => { if (h.hasDeclarations(_module)) { _module.declarations.forEach((declaration) => { if (h.isClass(declaration)) { if (declaration.name === className) { result = _module.path; } } }); } }); return result; } getModuleForMixin(className) { if (!__classPrivateFieldGet(this, _initialized)) { this.init(); __classPrivateFieldSet(this, _initialized, true); } let result = undefined; this.modules.forEach((_module) => { if (h.hasDeclarations(_module)) { _module.declarations.forEach((declaration) => { if (h.isMixin(declaration)) { if (declaration.name === className) { result = _module.path; } } }); } }); return result; } } exports.CustomElementsJson = CustomElementsJson; _classes = new WeakMap(), _tagNames = new WeakMap(), _definitions = new WeakMap(), _mixins = new WeakMap(), _initialized = new WeakMap(); // const default_fixture = require('../../custom-elements-json-analyzer/fixtures/mixins/fixture/custom-elements.json'); // const customElementsJson = new CustomElementsJson(default_fixture); // console.log(customElementsJson.getMixins()); // console.log(customElementsJson.getInheritanceTree('MyElement')); __exportStar(require("./helpers"), exports); //# sourceMappingURL=index.js.map