-
-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve debugging of Mint values. (#704)
- Loading branch information
1 parent
64e1590
commit 942c4b6
Showing
70 changed files
with
706 additions
and
315 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
crystal 1.14.0 | ||
mint 0.19.0 | ||
mint 0.20.0 | ||
nodejs 20.10.0 | ||
yarn 1.22.19 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import indentString from "indent-string"; | ||
import { isVnode } from './equality'; | ||
import { Variant } from './variant'; | ||
import { Name } from './symbols'; | ||
|
||
const render = (items, prefix, suffix, fn) => { | ||
items = | ||
items.map(fn); | ||
|
||
const newLines = | ||
items.size > 3 || items.filter((item) => item.indexOf("\n") > 0).length; | ||
|
||
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") { | ||
return "null"; | ||
} else if (object.type === "undefined") { | ||
return "undefined"; | ||
} else if (object.type === "string") { | ||
return `"${object.value}"`; | ||
} else if (object.type === "number") { | ||
return `${object.value}`; | ||
} else if (object.type === "boolean") { | ||
return `${object.value}`; | ||
} else if (object.type === "element") { | ||
return `<${object.value.toLowerCase()}>` | ||
} else if (object.type === "variant") { | ||
if (object.items) { | ||
return render(object.items, `${object.value}(`, `)`, toString); | ||
} else { | ||
return object.value; | ||
} | ||
} else if (object.type === "array") { | ||
return render(object.items, `[`, `]`, toString); | ||
} else if (object.type === "object") { | ||
return render(object.items, `{ `, ` }`, toString); | ||
} else if (object.type === "record") { | ||
return render(object.items, `${object.value} { `, ` }`, toString); | ||
} else if (object.type === "unknown") { | ||
return `{ ${object.value} }`; | ||
} else if (object.type === "vnode") { | ||
return `VNode`; | ||
} else if (object.key) { | ||
return `${object.key}: ${toString(object.value)}`; | ||
} else if (object.value) { | ||
return toString(object.value); | ||
} | ||
} | ||
|
||
const objectify = (value) => { | ||
if (value === null) { | ||
return { type: "null" }; | ||
} else if (value === undefined) { | ||
return { type: "undefined" }; | ||
} else if (typeof value === "string") { | ||
return { type: "string", value: value }; | ||
} else if (typeof value === "number") { | ||
return { type: "number", value: value.toString() }; | ||
} else if (typeof value === "boolean") { | ||
return { type: "boolean", value: value.toString() }; | ||
} else if (value instanceof HTMLElement) { | ||
return { type: "element", value: value.tagName }; | ||
} else if (value instanceof Variant) { | ||
const items = []; | ||
|
||
if (value.record) { | ||
for (const key in value) { | ||
if (key === "length" || key === "record" || key.startsWith("_")) { | ||
continue; | ||
}; | ||
|
||
items.push({ | ||
value: objectify(value[key]), | ||
key: key | ||
}); | ||
} | ||
} else { | ||
for (let i = 0; i < value.length; i++) { | ||
items.push({ | ||
value: objectify(value[`_${i}`]) | ||
}); | ||
}; | ||
} | ||
|
||
if (items.length) { | ||
return { type: "variant", value: value[Name], items: items }; | ||
} else { | ||
return { type: "variant", value: value[Name] }; | ||
} | ||
} else if (Array.isArray(value)) { | ||
return { | ||
items: value.map((item) => ({ value: objectify(item) })), | ||
type: "array" | ||
}; | ||
} else if (isVnode(value)) { | ||
return { type: "vnode" } | ||
} else if (typeof value == "object") { | ||
const items = []; | ||
|
||
for (const key in value) { | ||
items.push({ | ||
value: objectify(value[key]), | ||
key: key | ||
}); | ||
}; | ||
|
||
if (Name in value) { | ||
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const Equals = Symbol("Equals"); | ||
export const Name = Symbol('Name'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { variant, newVariant, inspect, record } from "../index_testing"; | ||
import { expect, test, describe } from "vitest"; | ||
|
||
test("inspecting null", () => { | ||
expect(inspect(null)).toBe("null"); | ||
}); | ||
|
||
test("inspecting undefined", () => { | ||
expect(inspect(undefined)).toBe("undefined"); | ||
}); | ||
|
||
test("inspecting string", () => { | ||
expect(inspect("Hello")).toBe(`"Hello"`); | ||
}); | ||
|
||
test("inspecting number", () => { | ||
expect(inspect(0)).toBe(`0`); | ||
}); | ||
|
||
test("inspecting boolean", () => { | ||
expect(inspect(true)).toBe(`true`); | ||
}); | ||
|
||
test("inspecting boolean", () => { | ||
expect(inspect({props: {}, type: {}, ref: {}, key: {},"__": {}})).toBe(`VNode`); | ||
}); | ||
|
||
test("inspecting object", () => { | ||
expect(inspect({ name: "Joe" })).toBe(`{ name: "Joe" }`); | ||
}); | ||
|
||
test("inspecting element", () => { | ||
expect(inspect(document.createElement("div"))).toBe(`<div>`); | ||
}); | ||
|
||
test("inspecting element", () => { | ||
expect(inspect(document.createElement("div"))).toBe(`<div>`); | ||
}); | ||
|
||
test("inspecting variant", () => { | ||
const Test = variant(0, `Test`) | ||
expect(inspect(newVariant(Test)())).toBe(`Test`); | ||
}); | ||
|
||
test("inspecting variant (with parameters)", () => { | ||
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!")`); | ||
}); | ||
|
||
test("inspecting record", () => { | ||
const Test = record(`Test`) | ||
expect(inspect(Test({ a: "Hello", b: "World!"}))).toBe(`Test { a: "Hello", b: "World!" }`); | ||
}); | ||
|
||
test("inspecting array", () => { | ||
expect(inspect(["Hello", "World!"])).toBe(`["Hello", "World!"]`); | ||
}); | ||
|
||
test("inspecting unkown", () => { | ||
expect(inspect(Symbol("WTF"))).toBe(`{ Symbol(WTF) }`); | ||
}); | ||
|
||
test("inspecting nested", () => { | ||
expect(inspect({ a: "Hello", b: "World!", nested: { x: "With new line!\nYes!"}})).toBe(`{ | ||
a: "Hello", | ||
b: "World!", | ||
nested: { | ||
x: "With new line! | ||
Yes!" | ||
} | ||
}`); | ||
}); |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.