Skip to content

Commit

Permalink
fix: ts linting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Delusoire committed May 7, 2024
1 parent 3238418 commit ee1fcc9
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 27 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
out/
types/
47 changes: 26 additions & 21 deletions module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,15 @@ export interface Metadata {
spotifyVersions?: string;
}

export class Module {
export class AbstractModule {
public awaitedMixins = new Array<Promise<void>>();

constructor(public metadata: Metadata) {}
}

export class Module extends AbstractModule {
public unloadJS: (() => Promise<void>) | null = null;
public unloadCSS: (() => void) | null = null;
public awaitedMixins = new Array<Promise<void>>();
private registerTransform = createRegisterTransform(this);
private dependants = new Set<Module>();
private mixinsEnabled = false;
Expand All @@ -59,22 +64,17 @@ export class Module {

static registry = new Map<string, Module>();

static INTERNAL = new Module(
{
name: "internal",
tags: ["internal"],
preview: "",
version: "dev",
authors: ["internal"],
readme: "",
entries: {},
description: "internal",
dependencies: [],
},
undefined,
undefined,
false,
);
static INTERNAL = new AbstractModule({
name: "internal",
tags: ["internal"],
preview: "",
version: "dev",
authors: ["internal"],
readme: "",
entries: {},
description: "internal",
dependencies: [],
});

static getModules() {
return Array.from(Module.registry.values());
Expand All @@ -100,6 +100,7 @@ export class Module {
public remoteMetadataURL?: string,
private shouldBeEnabled = true,
) {
super(metadata);
const identifier = this.getIdentifier();
if (Module.registry.has(identifier)) {
throw new Error(`A module with the same identifier "${identifier}" is already registered`);
Expand Down Expand Up @@ -230,14 +231,15 @@ export class Module {

await Promise.all(
this.metadata.dependencies.map(dependency => {
const module = Module.registry.get(dependency);
const module = Module.registry.get(dependency)!;
module.dependants.add(this);
return module.enableMixinsRecur();
}),
);

await this.loadMixins();

// @ts-ignore
finishLoading();
this.loading = undefined;
}
Expand All @@ -254,7 +256,7 @@ export class Module {

await Promise.all(
this.metadata.dependencies.map(dependency => {
const module = Module.registry.get(dependency);
const module = Module.registry.get(dependency)!;
return module.enableRecur(send);
}),
);
Expand All @@ -264,6 +266,7 @@ export class Module {
await Promise.all(this.awaitedMixins);
await this.loadJS();

// @ts-ignore
finishLoading();
this.loading = undefined;
}
Expand Down Expand Up @@ -294,6 +297,8 @@ export class Module {
send && ModuleManager.disable(this.getIdentifier());
await this.unloadCSS?.();
await this.unloadJS?.();

// @ts-ignore
finishLoading();
this.loading = undefined;
}
Expand Down Expand Up @@ -343,7 +348,7 @@ export class Module {
async dispose(send = false) {
await this.disable();
for (const dependency of this.metadata.dependencies) {
const module = Module.registry.get(dependency);
const module = Module.registry.get(dependency)!;
module.dependants.delete(this);
}
Module.registry.delete(this.getIdentifier());
Expand Down
1 change: 1 addition & 0 deletions tests/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* along with bespoke/hooks. If not, see <https://www.gnu.org/licenses/>.
*/

// @ts-ignore
const check = ({ props, name }: { props: Array<string>; name: string }) => {
const object = name.split(".").reduce((pobj, k) => pobj[k], globalThis as any);
const nonExistantProps = props.filter(prop => !object[prop]);
Expand Down
1 change: 0 additions & 1 deletion transforms/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,3 @@ export const applyTransforms = (path: string) => {
if (!source) return path;
return source.getObjectURL();
};
globalThis.__applyTransforms = applyTransforms;
3 changes: 3 additions & 0 deletions transforms/styledComponents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
* along with bespoke/hooks. If not, see <https://www.gnu.org/licenses/>.
*/

/*
import { internalRegisterTransform } from "./index.js";
internalRegisterTransform({
transform: emit => str => {
// https://github.com/styled-components/styled-components/blob/22e8b7f233e12500a68be4268b1d79c5d7f2a661/packages/styled-components/src/models/ComponentStyle.ts#L88
Expand Down Expand Up @@ -86,3 +88,4 @@ internalRegisterTransform({
},
glob: /^\/vendor~xpui\.js/,
});
*/
4 changes: 2 additions & 2 deletions transforms/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* along with bespoke/hooks. If not, see <https://www.gnu.org/licenses/>.
*/

import type { Module } from "../module.js";
import type { AbstractModule } from "../module.js";
import { Paths } from "../static.js";
import { fetchText } from "../util.js";

Expand Down Expand Up @@ -58,7 +58,7 @@ export type MixinProps<A> = {
export type RegisterTransformFN = ReturnType<typeof createRegisterTransform>;

export const createRegisterTransform =
(module: Module) =>
(module: AbstractModule) =>
<A = void>({ transform, then = () => {}, glob, noAwait = false }: MixinProps<A>) => {
const p = new Promise<A>(resolve => {
const _sources = Paths.map((path, i) => glob.test(path) && sources[i]).filter(Boolean) as SourceFile[];
Expand Down
7 changes: 4 additions & 3 deletions util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ export const matchLast = (str: string, pattern: RegExp) => {
};

export const visited = new Map();
export const type = (obj, access: string): string => {
export const type = (obj: any, access: string): string => {
if (typeof obj?.then === "function") return "Promise<any>";
if (obj instanceof Map) return "Map<any,any>";
if (obj instanceof Set) return "Set<any>";
if (obj instanceof Uint8Array) return "Uint8Array";
if (obj instanceof HTMLElement) return "HTMLElement";
if (obj instanceof Element) return "Element";

const wrapVisited = obj => {
const wrapVisited = (obj: any) => {
const typeRef = visited.get(obj);
if (typeRef) return typeRef;
visited.set(obj, access);
Expand Down Expand Up @@ -116,7 +116,8 @@ export const type = (obj, access: string): string => {
if (cached) return cached;
if (Array.isArray(obj)) {
const types = obj.map((e, i) => type(e, `${access}[${i}]`));
const uniqueTypes = Object.values(Object.groupBy(types, t => t)).map(v => v[0]);
// @ts-ignore: Property 'groupBy' does not exist on type 'ObjectConstructor'.
const uniqueTypes = Object.values(Object.groupBy(types, t => t)).map(v => v![0]);
return `Array<${uniqueTypes.sort().join("|")}>`;
}

Expand Down

0 comments on commit ee1fcc9

Please sign in to comment.