From 1c9377ec696c1f8269bf30a7af1e4a9566d55625 Mon Sep 17 00:00:00 2001 From: Aylur Date: Sat, 20 Jul 2024 14:05:47 +0200 Subject: update eslint, ts-for-gir --- gjs/src/application.ts | 11 ++++--- gjs/src/astalify.ts | 76 ++++++++++++++++++++++------------------------ gjs/src/file.ts | 6 ++-- gjs/src/jsx/jsx-runtime.ts | 61 +++++++++++++++++++------------------ gjs/src/process.ts | 6 ++-- gjs/src/variable.ts | 18 ++++++----- gjs/src/widgets.ts | 60 ++++++++++++++++++------------------ 7 files changed, 122 insertions(+), 116 deletions(-) (limited to 'gjs/src') diff --git a/gjs/src/application.ts b/gjs/src/application.ts index 606bf30..d1bc13f 100644 --- a/gjs/src/application.ts +++ b/gjs/src/application.ts @@ -33,7 +33,8 @@ class AstalJS extends Astal.Application { fn()() .then(res) .catch(rej) - } catch (error) { + } + catch (error) { rej(error) } }) @@ -43,12 +44,13 @@ class AstalJS extends Astal.Application { vfunc_request(msg: string, conn: Gio.SocketConnection): void { if (typeof this.requestHandler === "function") { - this.requestHandler(msg, response => { + this.requestHandler(msg, (response) => { Astal.write_sock(conn, String(response), (_, res) => Astal.write_sock_finish(res), ) }) - } else { + } + else { super.vfunc_request(msg, conn) } } @@ -68,7 +70,6 @@ class AstalJS extends Astal.Application { this.requestHandler = requestHandler this.connect("activate", () => { - // @ts-expect-error missing url type const path: string[] = import.meta.url.split("/").slice(3) const file = path.at(-1)!.replace(".js", ".css") const css = `/${path.slice(0, -1).join("/")}/${file}` @@ -92,4 +93,4 @@ class AstalJS extends Astal.Application { } } -export default new AstalJS +export default new AstalJS() diff --git a/gjs/src/astalify.ts b/gjs/src/astalify.ts index ad39da0..df9845f 100644 --- a/gjs/src/astalify.ts +++ b/gjs/src/astalify.ts @@ -71,14 +71,15 @@ function setProp(obj: any, prop: string, value: any) { if (Object.hasOwn(obj, prop)) return (obj[prop] = value) - } catch (error) { + } + catch (error) { console.error(`could not set property "${prop}" on ${obj}:`, error) } console.error(`could not set property "${prop}" on ${obj}`) } -export type Widget = InstanceType & { +export type Widget> = C & { className: string css: string cursor: Cursor @@ -170,7 +171,7 @@ function ctor(self: any, config: any = {}, children: any = []) { children = mergeBindings(children.flat(Infinity)) if (children instanceof Binding) { setChildren(self, children.get()) - self.connect("destroy", children.subscribe(v => { + self.connect("destroy", children.subscribe((v) => { setChildren(self, v) })) } @@ -184,60 +185,58 @@ function ctor(self: any, config: any = {}, children: any = []) { } function proxify< - C extends { new(...args: any[]): any }, + C extends typeof Gtk.Widget, >(klass: C) { - klass.prototype.hook = function (obj: any, sig: any, callback: any) { - return hook(this, obj, sig, callback) - } - - klass.prototype.toggleClassName = function (name: string, on = true) { - Astal.widget_toggle_class_name(this, name, on) - } - Object.defineProperty(klass.prototype, "className", { get() { return Astal.widget_get_class_names(this).join(" ") }, set(v) { Astal.widget_set_class_names(this, v.split(/\s+/)) }, }) - klass.prototype.set_class_name = function (name: string) { - this.className = name - } - Object.defineProperty(klass.prototype, "css", { get() { return Astal.widget_get_css(this) }, set(v) { Astal.widget_set_css(this, v) }, }) - klass.prototype.set_css = function (css: string) { - this.css = css - } - Object.defineProperty(klass.prototype, "cursor", { get() { return Astal.widget_get_cursor(this) }, set(v) { Astal.widget_set_cursor(this, v) }, }) - klass.prototype.set_cursor = function (cursor: string) { - this.cursor = cursor - } - Object.defineProperty(klass.prototype, "clickThrough", { get() { return Astal.widget_get_click_through(this) }, set(v) { Astal.widget_set_click_through(this, v) }, }) - klass.prototype.set_click_through = function (clickThrough: boolean) { - this.clickThrough = clickThrough - } + Object.assign(klass.prototype, { + hook: function (obj: any, sig: any, callback: any) { + return hook(this as InstanceType, obj, sig, callback) + }, + set_class_name: function (name: string) { + // @ts-expect-error unknown key + this.className = name + }, + set_css: function (css: string) { + // @ts-expect-error unknown key + this.css = css + }, + set_cursor: function (cursor: string) { + // @ts-expect-error unknown key + this.cursor = cursor + }, + set_click_through: function (clickThrough: boolean) { + // @ts-expect-error unknown key + this.clickThrough = clickThrough + }, + }) const proxy = new Proxy(klass, { construct(_, [conf, ...children]) { - const self = new klass - return ctor(self, conf, children) + // @ts-expect-error abstract class + return ctor(new klass(), conf, children) }, apply(_t, _a, [conf, ...children]) { - const self = new klass - return ctor(self, conf, children) + // @ts-expect-error abstract class + return ctor(new klass(), conf, children) }, }) @@ -251,33 +250,32 @@ export default function astalify< >(klass: C) { // eslint-disable-next-line @typescript-eslint/no-unused-vars type Astal = Omit & { - new(props?: P, ...children: Gtk.Widget[]): Widget - (props?: P, ...children: Gtk.Widget[]): Widget + new(props?: P, ...children: Gtk.Widget[]): Widget> + (props?: P, ...children: Gtk.Widget[]): Widget> } return proxify(klass) as unknown as Astal } - type BindableProps = { [K in keyof T]: Binding | T[K]; } type SigHandler< - W extends { new(...args: any): Gtk.Widget }, + W extends InstanceType, Args extends Array, > = ((self: Widget, ...args: Args) => unknown) | string | string[] export type ConstructProps< - Self extends { new(...args: any[]): any }, - Props = unknown, - Signals extends Record<`on${string}`, Array> = Record<`on${string}`, any[]> + Self extends InstanceType, + Props extends Gtk.Widget.ConstructorProps, + Signals extends Record<`on${string}`, Array> = Record<`on${string}`, any[]>, > = Partial<{ // @ts-expect-error can't assign to unknown, but it works as expected though [S in keyof Signals]: SigHandler }> & Partial<{ [Key in `on${string}`]: SigHandler -}> & BindableProps & BindableProps & { className?: string css?: string cursor?: string diff --git a/gjs/src/file.ts b/gjs/src/file.ts index d15474b..8875833 100644 --- a/gjs/src/file.ts +++ b/gjs/src/file.ts @@ -9,7 +9,8 @@ export function readFileAsync(path: string): Promise { Astal.read_file_async(path, (_, res) => { try { resolve(Astal.read_file_finish(res) || "") - } catch (error) { + } + catch (error) { reject(error) } }) @@ -25,7 +26,8 @@ export function writeFileAsync(path: string, content: string): Promise { Astal.write_file_async(path, content, (_, res) => { try { resolve(Astal.write_file_finish(res)) - } catch (error) { + } + catch (error) { reject(error) } }) diff --git a/gjs/src/jsx/jsx-runtime.ts b/gjs/src/jsx/jsx-runtime.ts index baa0444..70f098f 100644 --- a/gjs/src/jsx/jsx-runtime.ts +++ b/gjs/src/jsx/jsx-runtime.ts @@ -27,30 +27,31 @@ export function jsx( if (isArrowFunction(ctor)) return ctor(props) + // @ts-expect-error can be class or function return new ctor(props) } const ctors = { - "box": Widget.Box, - "button": Widget.Button, - "centerbox": Widget.CenterBox, + box: Widget.Box, + button: Widget.Button, + centerbox: Widget.CenterBox, // TODO: circularprogress - "drawingarea": Widget.DrawingArea, - "entry": Widget.Entry, - "eventbox": Widget.EventBox, + drawingarea: Widget.DrawingArea, + entry: Widget.Entry, + eventbox: Widget.EventBox, // TODO: fixed // TODO: flowbox - "icon": Widget.Icon, - "label": Widget.Label, - "levelbar": Widget.LevelBar, + icon: Widget.Icon, + label: Widget.Label, + levelbar: Widget.LevelBar, // TODO: listbox - "overlay": Widget.Overlay, - "revealer": Widget.Revealer, - "scrollable": Widget.Scrollable, - "slider": Widget.Slider, + overlay: Widget.Overlay, + revealer: Widget.Revealer, + scrollable: Widget.Scrollable, + slider: Widget.Slider, // TODO: stack - "switch": Widget.Switch, - "window": Widget.Window, + switch: Widget.Switch, + window: Widget.Window, } declare global { @@ -59,26 +60,26 @@ declare global { type Element = Gtk.Widget type ElementClass = Gtk.Widget interface IntrinsicElements { - "box": Widget.BoxProps, - "button": Widget.ButtonProps, - "centerbox": Widget.CenterBoxProps, + box: Widget.BoxProps + button: Widget.ButtonProps + centerbox: Widget.CenterBoxProps // TODO: circularprogress - "drawingarea": Widget.DrawingAreaProps, - "entry": Widget.EntryProps, - "eventbox": Widget.EventBoxProps, + drawingarea: Widget.DrawingAreaProps + entry: Widget.EntryProps + eventbox: Widget.EventBoxProps // TODO: fixed // TODO: flowbox - "icon": Widget.IconProps, - "label": Widget.LabelProps, - "levelbar": Widget.LevelBarProps, + icon: Widget.IconProps + label: Widget.LabelProps + levelbar: Widget.LevelBarProps // TODO: listbox - "overlay": Widget.OverlayProps, - "revealer": Widget.RevealerProps, - "scrollable": Widget.ScrollableProps, - "slider": Widget.SliderProps, + overlay: Widget.OverlayProps + revealer: Widget.RevealerProps + scrollable: Widget.ScrollableProps + slider: Widget.SliderProps // TODO: stack - "switch": Widget.SwitchProps, - "window": Widget.WindowProps, + switch: Widget.SwitchProps + window: Widget.WindowProps } } } diff --git a/gjs/src/process.ts b/gjs/src/process.ts index 59ab328..f7ff7b0 100644 --- a/gjs/src/process.ts +++ b/gjs/src/process.ts @@ -1,9 +1,9 @@ import { Astal } from "./imports.js" type Args = { - cmd: string | string[], - out?: (stdout: string) => Out, - err?: (stderr: string) => Err, + cmd: string | string[] + out?: (stdout: string) => Out + err?: (stderr: string) => Err } function args(argsOrCmd: Args | string | string[], onOut: O, onErr: E) { diff --git a/gjs/src/variable.ts b/gjs/src/variable.ts index b5f186b..05e142f 100644 --- a/gjs/src/variable.ts +++ b/gjs/src/variable.ts @@ -22,7 +22,7 @@ class VariableWrapper extends Function { constructor(init: T) { super() this._value = init - this.variable = new Astal.VariableBase + this.variable = new Astal.VariableBase() this.variable.connect("dropped", () => { this.stopWatch() this.stopPoll() @@ -60,11 +60,13 @@ class VariableWrapper extends Function { if (v instanceof Promise) { v.then(v => this.set(v)) .catch(err => this.variable.emit("error", err)) - } else { + } + else { this.set(v) } }) - } else if (this.pollExec) { + } + else if (this.pollExec) { this._poll = interval(this.pollInterval, () => { execAsync(this.pollExec!) .then(v => this.set(this.pollTransform!(v, this.get()))) @@ -142,7 +144,8 @@ class VariableWrapper extends Function { if (typeof exec === "function") { this.pollFn = exec delete this.pollExec - } else { + } + else { this.pollExec = exec delete this.pollFn } @@ -183,7 +186,8 @@ class VariableWrapper extends Function { const [o, s] = obj o.connect(s, set) } - } else { + } + else { if (typeof sigOrFn === "string") objs.connect(sigOrFn, set) } @@ -195,7 +199,7 @@ class VariableWrapper extends Function { const Deps extends Array | Binding>, Args extends { [K in keyof Deps]: Deps[K] extends Variable - ? T : Deps[K] extends Binding ? T : never + ? T : Deps[K] extends Binding ? T : never }, >(deps: Deps, fn: (...args: Args) => V) { const update = () => fn(...deps.map(d => d.get()) as Args) @@ -216,7 +220,7 @@ export const Variable = new Proxy(VariableWrapper as any, { }) as { derive: typeof VariableWrapper["derive"] (init: T): Variable - new (init: T): Variable + new(init: T): Variable } export default Variable diff --git a/gjs/src/widgets.ts b/gjs/src/widgets.ts index bf5ac50..a0f333b 100644 --- a/gjs/src/widgets.ts +++ b/gjs/src/widgets.ts @@ -5,14 +5,14 @@ import astalify, { type ConstructProps, type Widget } from "./astalify.js" export { astalify, ConstructProps } // Box -export type Box = Widget +export type Box = Widget export const Box = astalify(Astal.Box) -export type BoxProps = ConstructProps +export type BoxProps = ConstructProps // Button -export type Button = Widget +export type Button = Widget export const Button = astalify(Astal.Button) -export type ButtonProps = ConstructProps // CenterBox -export type CenterBox = Widget +export type CenterBox = Widget export const CenterBox = astalify(Astal.CenterBox) -export type CenterBoxProps = ConstructProps +export type CenterBoxProps = ConstructProps // TODO: CircularProgress // DrawingArea -export type DrawingArea = Widget +export type DrawingArea = Widget export const DrawingArea = astalify(Gtk.DrawingArea) -export type DrawingAreaProps = ConstructProps // Entry -export type Entry = Widget +export type Entry = Widget export const Entry = astalify(Gtk.Entry) -export type EntryProps = ConstructProps // EventBox -export type EventBox = Widget +export type EventBox = Widget export const EventBox = astalify(Astal.EventBox) -export type EventBoxProps = ConstructProps +export type Icon = Widget export const Icon = astalify(Astal.Icon) -export type IconProps = ConstructProps +export type IconProps = ConstructProps // Label -export type Label = Widget +export type Label = Widget export const Label = astalify(Gtk.Label) -export type LabelProps = ConstructProps +export type LabelProps = ConstructProps // LevelBar -export type LevelBar = Widget +export type LevelBar = Widget export const LevelBar = astalify(Astal.LevelBar) -export type LevelBarProps = ConstructProps +export type LevelBarProps = ConstructProps // TODO: ListBox // Overlay -export type Overlay = Widget +export type Overlay = Widget export const Overlay = astalify(Astal.Overlay) -export type OverlayProps = ConstructProps +export type OverlayProps = ConstructProps // Revealer -export type Revealer = Widget +export type Revealer = Widget export const Revealer = astalify(Gtk.Revealer) -export type RevealerProps = ConstructProps +export type RevealerProps = ConstructProps // Scrollable -export type Scrollable = Widget +export type Scrollable = Widget export const Scrollable = astalify(Astal.Scrollable) -export type ScrollableProps = ConstructProps +export type ScrollableProps = ConstructProps // Slider -export type Slider = Widget +export type Slider = Widget export const Slider = astalify(Astal.Slider) -export type SliderProps = ConstructProps // TODO: Stack // Switch -export type Switch = Widget +export type Switch = Widget export const Switch = astalify(Gtk.Switch) -export type SwitchProps = ConstructProps +export type SwitchProps = ConstructProps // Window -export type Window = Widget +export type Window = Widget export const Window = astalify(Astal.Window) -export type WindowProps = ConstructProps +export type WindowProps = ConstructProps -- cgit v1.2.3