diff --git a/runtime/src/debug.js b/runtime/src/debug.js index 83cc5bc2..a057d56f 100644 --- a/runtime/src/debug.js +++ b/runtime/src/debug.js @@ -1,24 +1,22 @@ import indentString from "indent-string"; -import { isVnode } from './equality'; -import { Variant } from './variant'; -import { Name } from './symbols'; +import { isVnode } from "./equality"; +import { Variant } from "./variant"; +import { Name } from "./symbols"; const render = (items, prefix, suffix, fn) => { - items = - items.map(fn); + items = items.map(fn); const newLines = items.size > 3 || items.filter((item) => item.indexOf("\n") > 0).length; - const joined = - items.join(newLines ? ",\n" : ", "); + const joined = items.join(newLines ? ",\n" : ", "); if (newLines) { return `${prefix.trim()}\n${indentString(joined, 2)}\n${suffix.trim()}`; } else { return `${prefix}${joined}${suffix}`; } -} +}; const toString = (object) => { if (object.type === "null") { @@ -32,7 +30,7 @@ const toString = (object) => { } else if (object.type === "boolean") { return `${object.value}`; } else if (object.type === "element") { - return `<${object.value.toLowerCase()}>` + return `<${object.value.toLowerCase()}>`; } else if (object.type === "variant") { if (object.items) { return render(object.items, `${object.value}(`, `)`, toString); @@ -54,7 +52,7 @@ const toString = (object) => { } else if (object.value) { return toString(object.value); } -} +}; const objectify = (value) => { if (value === null) { @@ -76,19 +74,19 @@ const objectify = (value) => { for (const key in value) { if (key === "length" || key === "record" || key.startsWith("_")) { continue; - }; + } items.push({ value: objectify(value[key]), - key: key + key: key, }); } } else { for (let i = 0; i < value.length; i++) { items.push({ - value: objectify(value[`_${i}`]) + value: objectify(value[`_${i}`]), }); - }; + } } if (items.length) { @@ -99,30 +97,30 @@ const objectify = (value) => { } else if (Array.isArray(value)) { return { items: value.map((item) => ({ value: objectify(item) })), - type: "array" + type: "array", }; } else if (isVnode(value)) { - return { type: "vnode" } + return { type: "vnode" }; } else if (typeof value == "object") { const items = []; for (const key in value) { items.push({ value: objectify(value[key]), - key: key + key: key, }); - }; + } if (Name in value) { - return { type: "record", value: value[Name], items: items }; + return { type: "record", value: value[Name], items: items }; } else { return { type: "object", items: items }; } } else { return { type: "unknown", value: value.toString() }; } -} +}; export const inspect = (value) => { - return toString(objectify(value)) -} + return toString(objectify(value)); +}; diff --git a/runtime/src/decoders.js b/runtime/src/decoders.js index 7a371749..962a5005 100644 --- a/runtime/src/decoders.js +++ b/runtime/src/decoders.js @@ -319,7 +319,7 @@ export const decodeMap = (decoder, ok, err) => (input) => { // Decodes a record, using the mappings. export const decoder = (name, mappings, ok, err) => (input) => { - const object = {[Name]: name}; + const object = { [Name]: name }; for (let key in mappings) { let decoder = mappings[key]; diff --git a/runtime/src/equality.js b/runtime/src/equality.js index efa5d24b..7b388609 100644 --- a/runtime/src/equality.js +++ b/runtime/src/equality.js @@ -1,11 +1,11 @@ // This file contains code to have value equality instead of reference equality. // We use a `Symbol` to have a custom equality functions and then use these // functions when comparing two values. -import { Equals } from './symbols'; +import { Equals } from "./symbols"; /* v8 ignore next 3 */ if (typeof Node === "undefined") { - self.Node = class {} + self.Node = class {}; } // We use regular functions instead of arrow functions because they have @@ -132,7 +132,7 @@ export const isVnode = (object) => "type" in object && "ref" in object && "key" in object && - "__" in object + "__" in object; // This is the custom comparison function. export const compare = (a, b) => { @@ -143,7 +143,7 @@ export const compare = (a, b) => { } else if (b != null && b != undefined && b[Equals]) { return b[Equals](a); } else if (isVnode(a) || isVnode(b)) { - return a === b + return a === b; } else { return compareObjects(a, b); } diff --git a/runtime/src/pattern_matching.js b/runtime/src/pattern_matching.js index d81da2cc..ef204df0 100644 --- a/runtime/src/pattern_matching.js +++ b/runtime/src/pattern_matching.js @@ -87,9 +87,7 @@ export const destructure = (value, pattern, values = []) => { } else if (pattern instanceof Pattern) { if (value instanceof pattern.variant) { for (let index in pattern.pattern) { - if ( - !destructure(value[`_${index}`], pattern.pattern[index], values) - ) { + if (!destructure(value[`_${index}`], pattern.pattern[index], values)) { return false; } } diff --git a/runtime/src/program.js b/runtime/src/program.js index 5773b98c..3b762f06 100644 --- a/runtime/src/program.js +++ b/runtime/src/program.js @@ -149,7 +149,9 @@ class Program { !equals(routeInfo.vars, this.routeInfo.vars) ) { const handler = this.runRouteHandler(routeInfo); - if (routeInfo.route.await) { await handler } + if (routeInfo.route.await) { + await handler; + } } this.resolvePagePosition(!!event?.triggerJump); @@ -198,7 +200,7 @@ class Program { let mainNode; if (typeof main !== "undefined") { - mainNode = h(main, { key: "MINT_MAIN" }) + mainNode = h(main, { key: "MINT_MAIN" }); } render([...components, mainNode], this.root); diff --git a/runtime/src/symbols.js b/runtime/src/symbols.js index fd8cb2cf..76531b8e 100644 --- a/runtime/src/symbols.js +++ b/runtime/src/symbols.js @@ -1,2 +1,2 @@ export const Equals = Symbol("Equals"); -export const Name = Symbol('Name'); +export const Name = Symbol("Name"); diff --git a/runtime/src/utilities.js b/runtime/src/utilities.js index aa28f391..2ff0e404 100644 --- a/runtime/src/utilities.js +++ b/runtime/src/utilities.js @@ -1,7 +1,12 @@ -import { createRef as createRefOriginal, Component, createElement } from "preact"; import { useEffect, useRef, useMemo } from "preact/hooks"; import { signal } from "@preact/signals"; +import { + createRef as createRefOriginal, + createElement, + Component, +} from "preact"; + import { compare } from "./equality"; import { Name } from "./symbols"; @@ -9,12 +14,12 @@ import { Name } from "./symbols"; export const mapAccess = (map, key, just, nothing) => { for (const item of map) { if (compare(item[0], key)) { - return new just(item[1]) + return new just(item[1]); } } return new nothing(); -} +}; // We need to have a different function for accessing array items because there // is no concept of `null` in Mint so we return `Just(a)` or `Nothing`. @@ -89,29 +94,29 @@ export const access = (field) => (value) => value[field]; export const identity = (a) => a; // Creates an instrumented object so we know which record it belongs to. -export const record = (name) => (value) => ({ [Name]: name, ...value}) +export const record = (name) => (value) => ({ [Name]: name, ...value }); // A component to lazy load another component. export class lazyComponent extends Component { async componentDidMount() { let x = await this.props.x(); - this.setState({ x: x }) + this.setState({ x: x }); } render() { if (this.state.x) { - return createElement(this.state.x, this.props.p, this.props.c) + return createElement(this.state.x, this.props.p, this.props.c); } else { - return null + return null; } } } // A higher order function to lazy load a module. -export const lazy = (path) => async () => load(path) +export const lazy = (path) => async () => load(path); // Loads load a module. export const load = async (path) => { - const x = await import(path) - return x.default -} + const x = await import(path); + return x.default; +}; diff --git a/runtime/src/variant.js b/runtime/src/variant.js index 1a22d756..35c4635c 100644 --- a/runtime/src/variant.js +++ b/runtime/src/variant.js @@ -32,7 +32,7 @@ export const variant = (input, name) => { return class extends Variant { constructor(...args) { super(); - this[Name] = name + this[Name] = name; if (Array.isArray(input)) { this.length = input.length; this.record = true; diff --git a/runtime/tests/debug.test.js b/runtime/tests/debug.test.js index 8b28f67b..2d8970b8 100644 --- a/runtime/tests/debug.test.js +++ b/runtime/tests/debug.test.js @@ -22,7 +22,9 @@ test("inspecting boolean", () => { }); test("inspecting boolean", () => { - expect(inspect({props: {}, type: {}, ref: {}, key: {},"__": {}})).toBe(`VNode`); + expect(inspect({ props: {}, type: {}, ref: {}, key: {}, __: {} })).toBe( + `VNode`, + ); }); test("inspecting object", () => { @@ -38,23 +40,27 @@ test("inspecting element", () => { }); test("inspecting variant", () => { - const Test = variant(0, `Test`) + const Test = variant(0, `Test`); expect(inspect(newVariant(Test)())).toBe(`Test`); }); test("inspecting variant (with parameters)", () => { - const Test = variant(1, `Test`) + const Test = variant(1, `Test`); expect(inspect(newVariant(Test)("Hello"))).toBe(`Test("Hello")`); }); test("inspecting variant (with named parameters)", () => { - const Test = variant(["a", "b"], `Test`) - expect(inspect(newVariant(Test)("Hello", "World!"))).toBe(`Test(a: "Hello", b: "World!")`); + const Test = variant(["a", "b"], `Test`); + expect(inspect(newVariant(Test)("Hello", "World!"))).toBe( + `Test(a: "Hello", b: "World!")`, + ); }); test("inspecting record", () => { - const Test = record(`Test`) - expect(inspect(Test({ a: "Hello", b: "World!"}))).toBe(`Test { a: "Hello", b: "World!" }`); + const Test = record(`Test`); + expect(inspect(Test({ a: "Hello", b: "World!" }))).toBe( + `Test { a: "Hello", b: "World!" }`, + ); }); test("inspecting array", () => { @@ -66,7 +72,9 @@ test("inspecting unkown", () => { }); test("inspecting nested", () => { - expect(inspect({ a: "Hello", b: "World!", nested: { x: "With new line!\nYes!"}})).toBe(`{ + expect( + inspect({ a: "Hello", b: "World!", nested: { x: "With new line!\nYes!" } }), + ).toBe(`{ a: "Hello", b: "World!", nested: { diff --git a/runtime/tests/equality.test.js b/runtime/tests/equality.test.js index 91de76a2..3a5e2f53 100644 --- a/runtime/tests/equality.test.js +++ b/runtime/tests/equality.test.js @@ -17,11 +17,13 @@ test("comparing same symbols", () => { }); test("comparing vnodes", () => { - expect(compare( - {props: {}, type: {}, ref: {}, key: {},"__": {}}, - {props: {}, type: {}, ref: {}, key: {},"__": {}} - )).toBe(false); -}) + expect( + compare( + { props: {}, type: {}, ref: {}, key: {}, __: {} }, + { props: {}, type: {}, ref: {}, key: {}, __: {} }, + ), + ).toBe(false); +}); test("comparing same arrays", () => { expect(["A"] == ["A"]).toBe(false); diff --git a/runtime/tests/utilities.test.js b/runtime/tests/utilities.test.js index bb3652c4..a311c1d7 100644 --- a/runtime/tests/utilities.test.js +++ b/runtime/tests/utilities.test.js @@ -45,7 +45,7 @@ describe("or", () => { }); test("it returns the given item", () => { - expect(or(Nothing, Err, new Nothing, "b")).toEqual("b"); + expect(or(Nothing, Err, new Nothing(), "b")).toEqual("b"); }); });