Skip to content

Commit f3fdce9

Browse files
committed
Fix typescript
1 parent 145ad49 commit f3fdce9

File tree

4 files changed

+60
-25
lines changed

4 files changed

+60
-25
lines changed

website/src/repl/FormatterOutput.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { toHTML } from 'jsonml-html';
22
import { useEffect, useRef, type JSX } from 'react';
3+
import { Element, JsonMLElementList } from '../worker/jsonml-types';
34

45
/**
56
* immutable-devtools is a console custom formatter.
@@ -8,7 +9,7 @@ import { useEffect, useRef, type JSX } from 'react';
89
* The `jsonml-html` package can convert jsonml to HTML.
910
*/
1011
type Props = {
11-
output: Array<unknown>;
12+
output: JsonMLElementList | Element;
1213
};
1314

1415
export default function FormatterOutput({ output }: Props): JSX.Element {

website/src/repl/Repl.tsx

+10-6
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@ import React, { useEffect, useRef, useState, type JSX } from 'react';
44
import { Editor } from './Editor';
55
import FormatterOutput from './FormatterOutput';
66
import './repl.css';
7+
import { Element, JsonMLElementList } from '../worker/jsonml-types';
78

89
type Props = { defaultValue: string; onRun?: (code: string) => void };
910

1011
function Repl({ defaultValue, onRun }: Props): JSX.Element {
1112
const [code, setCode] = useState<string>(defaultValue);
12-
const [output, setOutput] = useState<{
13-
header: Array<unknown>;
14-
body?: Array<unknown>;
15-
}>({ header: [] });
13+
const [output, setOutput] = useState<JsonMLElementList | Element>([]);
1614
const workerRef = useRef<Worker | null>(null);
1715

1816
useEffect(() => {
@@ -41,9 +39,15 @@ function Repl({ defaultValue, onRun }: Props): JSX.Element {
4139
workerRef.current.postMessage(code);
4240
workerRef.current.onmessage = (event) => {
4341
if (event.data.error) {
44-
setOutput({ header: ['div', 'Error: ' + event.data.error] });
42+
setOutput(['div', 'Error: ' + event.data.error]);
4543
} else {
46-
setOutput(event.data.output);
44+
const { output } = event.data;
45+
46+
if (typeof output === 'object' && !Array.isArray(output)) {
47+
setOutput(['div', { object: output }]);
48+
} else {
49+
setOutput(output);
50+
}
4751
}
4852
};
4953
}

website/src/worker/normalizeResult.test.ts

+42-15
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import normalizeResult from './normalizeResult';
66
// eslint-disable-next-line @typescript-eslint/no-require-imports -- import does not work
77
const installDevTools = require('@jdeniau/immutable-devtools');
88

9-
console.log(installDevTools);
10-
119
installDevTools(Immutable);
1210

1311
// hack to get the formatters from immutable-devtools as they are not exported, but they modify the "global" variable
@@ -17,17 +15,15 @@ describe('normalizeResult', () => {
1715
it('should return the correct object', () => {
1816
const result = normalizeResult(immutableFormaters, { a: 1, b: 2 });
1917

20-
expect(result).toEqual({
21-
header: ['span', JSON.stringify({ a: 1, b: 2 })],
22-
body: null,
23-
});
18+
expect(result).toEqual(JSON.stringify({ a: 1, b: 2 }));
2419
});
2520

2621
it('should return the correct object for a list', () => {
2722
const result = normalizeResult(immutableFormaters, Immutable.List(['a']));
2823

29-
expect(result).toEqual({
30-
header: [
24+
expect(result).toEqual([
25+
'span',
26+
[
3127
'span',
3228
[
3329
'span',
@@ -39,7 +35,7 @@ describe('normalizeResult', () => {
3935
],
4036
['span', '[1]'],
4137
],
42-
body: [
38+
[
4339
'ol',
4440
{
4541
style:
@@ -51,7 +47,7 @@ describe('normalizeResult', () => {
5147
['object', { object: 'a', config: undefined }],
5248
],
5349
],
54-
});
50+
]);
5551
});
5652

5753
it('should return the correct object for a deep list', () => {
@@ -60,8 +56,9 @@ describe('normalizeResult', () => {
6056
Immutable.List([Immutable.List(['a'])])
6157
);
6258

63-
expect(result).toEqual({
64-
header: [
59+
expect(result).toEqual([
60+
'span',
61+
[
6562
'span',
6663
[
6764
'span',
@@ -73,7 +70,7 @@ describe('normalizeResult', () => {
7370
],
7471
['span', '[1]'],
7572
],
76-
body: [
73+
[
7774
'ol',
7875
{
7976
style:
@@ -82,9 +79,39 @@ describe('normalizeResult', () => {
8279
[
8380
'li',
8481
['span', { style: 'color: light-dark( #881391, #D48CE6)' }, '0: '],
85-
['object', { object: 'a', config: undefined }],
82+
[
83+
'span',
84+
[
85+
'span',
86+
[
87+
'span',
88+
{
89+
style:
90+
'color: light-dark(rgb(232,98,0), rgb(255, 150, 50)); position: relative',
91+
},
92+
'List',
93+
],
94+
['span', '[1]'],
95+
],
96+
[
97+
'ol',
98+
{
99+
style:
100+
'list-style-type: none; padding: 0; margin: 0 0 0 12px; font-style: normal; position: relative',
101+
},
102+
[
103+
'li',
104+
[
105+
'span',
106+
{ style: 'color: light-dark( #881391, #D48CE6)' },
107+
'0: ',
108+
],
109+
['object', { object: 'a', config: undefined }],
110+
],
111+
],
112+
],
86113
],
87114
],
88-
});
115+
]);
89116
});
90117
});

website/src/worker/normalizeResult.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ function getFormatter(
1818
return immutableFormaters.find((formatter) => formatter.header(result));
1919
}
2020

21-
// console.log(immutableFormaters)
2221
export default function normalizeResult(
2322
immutableFormaters: Array<DevToolsFormatter>,
2423
result: unknown
@@ -42,7 +41,11 @@ export default function normalizeResult(
4241
return normalizeElement(immutableFormaters, result);
4342
}
4443

45-
return result;
44+
if (typeof result === 'string') {
45+
return result;
46+
}
47+
48+
return JSON.stringify(result);
4649
}
4750

4851
const header = formatter.header(result) ?? [];
@@ -73,13 +76,13 @@ function normalizeElement(
7376
const explodedItem = explodeElement(item);
7477

7578
const { tagName, attributes, children } = explodedItem;
76-
// console.log(explodedItem);
7779

7880
const normalizedChildren = children.map((child) =>
7981
normalizeResult(immutableFormaters, child)
8082
);
8183

8284
if (attributes) {
85+
// @ts-expect-error type is not perfect here because of self-reference
8386
return [tagName, attributes, ...normalizedChildren];
8487
}
8588

0 commit comments

Comments
 (0)