Skip to content

Commit e5f87ce

Browse files
committed
Remove more any, as cast.
- Removes more any types from dom/event/iso.ts. - As casts where necessary - Makes check helper function for integrityCheck - Adds return types to functions
1 parent b6a3d48 commit e5f87ce

File tree

6 files changed

+94
-110
lines changed

6 files changed

+94
-110
lines changed

ts/miso.spec.ts

Lines changed: 22 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,8 +1656,7 @@ describe('js tests', () => {
16561656
};
16571657
var result = hydrate(false, body, vtree);
16581658
expect(result).toEqual(true);
1659-
var check = integrityCheck(true, vtree);
1660-
expect(check).toBe(true);
1659+
expect(integrityCheck(vtree)).toBe(true);
16611660
});
16621661

16631662
test('Should fail integrity check on bad tag', () => {
@@ -1678,11 +1677,9 @@ describe('js tests', () => {
16781677
};
16791678
var result = hydrate(false, body, vtree);
16801679
expect(result).toEqual(true);
1681-
var check = integrityCheck(true, vtree);
1682-
expect(check).toBe(true);
1680+
expect(integrityCheck(vtree)).toBe(true);
16831681
vtree.tag = 'lol';
1684-
check = integrityCheck(true, vtree);
1685-
expect(check).toBe(false);
1682+
expect(integrityCheck(vtree)).toBe(false);
16861683
});
16871684

16881685
test('Should fail integrity check on bad tag in hydrate w/ logging enabled', () => {
@@ -1723,11 +1720,9 @@ describe('js tests', () => {
17231720
};
17241721
var result = hydrate(false, body, vtree);
17251722
expect(result).toEqual(true);
1726-
var check = integrityCheck(true, vtree);
1727-
expect(check).toBe(true);
1723+
expect(integrityCheck(vtree)).toBe(true);
17281724
vtree.children[0].text = 'oops';
1729-
check = integrityCheck(true, vtree);
1730-
expect(check).toBe(false);
1725+
expect(integrityCheck(vtree)).toBe(false);
17311726
});
17321727

17331728
test('Should fail integrity check on differing child lengths', () => {
@@ -1748,11 +1743,9 @@ describe('js tests', () => {
17481743
};
17491744
var result = hydrate(false, body, vtree);
17501745
expect(result).toEqual(true);
1751-
var check = integrityCheck(true, vtree);
1752-
expect(check).toBe(true);
1746+
expect(integrityCheck(vtree)).toBe(true);
17531747
vtree.children = [];
1754-
check = integrityCheck(true, vtree);
1755-
expect(check).toBe(false);
1748+
expect(integrityCheck(vtree)).toBe(false);
17561749
});
17571750

17581751
test('Should fail integrity check on differing styles', () => {
@@ -1774,11 +1767,9 @@ describe('js tests', () => {
17741767
};
17751768
var result = hydrate(false, body, vtree);
17761769
expect(result).toEqual(true);
1777-
var check = integrityCheck(true, vtree);
1778-
expect(check).toBe(true);
1770+
expect(integrityCheck(vtree)).toBe(true);
17791771
vtree.css['background-color'] = 'green';
1780-
check = integrityCheck(true, vtree);
1781-
expect(check).toBe(false);
1772+
expect(integrityCheck(vtree)).toBe(false);
17821773
});
17831774

17841775
test('Should fail integrity check on differing styles, for color', () => {
@@ -1801,11 +1792,9 @@ describe('js tests', () => {
18011792
};
18021793
var result = hydrate(false, body, vtree);
18031794
expect(result).toEqual(true);
1804-
var check = integrityCheck(true, vtree);
1805-
expect(check).toBe(true);
1795+
expect(integrityCheck(vtree)).toBe(true);
18061796
vtree.css['color'] = '#dddddd';
1807-
check = integrityCheck(true, vtree);
1808-
expect(check).toBe(false);
1797+
expect(integrityCheck(vtree)).toBe(false);
18091798
});
18101799

18111800
test('Should fail integrity check on differing props', () => {
@@ -1828,11 +1817,9 @@ describe('js tests', () => {
18281817
};
18291818
var result = hydrate(false, body, vtree);
18301819
expect(result).toEqual(true);
1831-
var check = integrityCheck(true, vtree);
1832-
expect(check).toBe(true);
1820+
expect(integrityCheck(vtree)).toBe(true);
18331821
vtree.props['class'] = 'something-else';
1834-
check = integrityCheck(true, vtree);
1835-
expect(check).toBe(false);
1822+
expect(integrityCheck(vtree)).toBe(false);
18361823
});
18371824

18381825
test('Should fail integrity check on differing height / width', () => {
@@ -1857,12 +1844,10 @@ describe('js tests', () => {
18571844
};
18581845
var result = hydrate(false, body, vtree);
18591846
expect(result).toEqual(true);
1860-
var check = integrityCheck(true, vtree);
1861-
expect(check).toBe(true);
1847+
expect(integrityCheck(vtree)).toBe(true);
18621848
vtree.props['height'] = '200';
18631849
vtree.props['width'] = '200';
1864-
check = integrityCheck(true, vtree);
1865-
expect(check).toBe(false);
1850+
expect(integrityCheck(vtree)).toBe(false);
18661851
});
18671852

18681853
test('Should fail integrity check on random property (title)', () => {
@@ -1884,11 +1869,9 @@ describe('js tests', () => {
18841869
};
18851870
var result = hydrate(false, body, vtree);
18861871
expect(result).toEqual(true);
1887-
var check = integrityCheck(true, vtree);
1888-
expect(check).toBe(true);
1872+
expect(integrityCheck(vtree)).toBe(true);
18891873
vtree.props['title'] = 'woz';
1890-
check = integrityCheck(true, vtree);
1891-
expect(check).toBe(false);
1874+
expect(integrityCheck(vtree)).toBe(false);
18921875
});
18931876

18941877
test('Should fail integrity check on href', () => {
@@ -1911,11 +1894,9 @@ describe('js tests', () => {
19111894
};
19121895
var result = hydrate(false, body, vtree);
19131896
expect(result).toEqual(true);
1914-
var check = integrityCheck(true, vtree);
1915-
expect(check).toBe(true);
1897+
expect(integrityCheck(vtree)).toBe(true);
19161898
vtree.props['href'] = 'notgoogle.com';
1917-
check = integrityCheck(true, vtree);
1918-
expect(check).toBe(false);
1899+
expect(integrityCheck(vtree)).toBe(false);
19191900
});
19201901

19211902
test('Should fail integrity check on vtext domRef', () => {
@@ -1938,11 +1919,9 @@ describe('js tests', () => {
19381919
};
19391920
var result = hydrate(false, body, vtree);
19401921
expect(result).toEqual(true);
1941-
var check = integrityCheck(true, vtree);
1942-
expect(check).toBe(true);
1922+
expect(integrityCheck(vtree)).toBe(true);
19431923
vtree.children[0].domRef = document.createElement('div');
1944-
check = integrityCheck(true, vtree);
1945-
expect(check).toBe(false);
1924+
expect(integrityCheck(vtree)).toBe(false);
19461925
});
19471926

19481927
test('Should fail integrity check on unknown property test', () => {
@@ -1963,8 +1942,7 @@ describe('js tests', () => {
19631942
};
19641943
var result = hydrate(false, body, vtree);
19651944
expect(result).toEqual(true);
1966-
var check = integrityCheck(true, vtree);
1967-
expect(check).toBe(false);
1945+
expect(integrityCheck(vtree)).toBe(false);
19681946
});
19691947

19701948
test('Should set body[data-component-id] via setBodyComponent()', () => {

ts/miso/dom.ts

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { VTree } from './types';
22

33
/* virtual-dom diffing algorithm, applies patches as detected */
4-
export function diff(currentObj: VTree, newObj: VTree, parent: Element) {
4+
export function diff(currentObj: VTree, newObj: VTree, parent: Element) : void {
55
if (!currentObj && !newObj) return;
66
else if (!currentObj && newObj) create(newObj, parent);
77
else if (!newObj) destroy(currentObj, parent);
@@ -14,7 +14,7 @@ export function diff(currentObj: VTree, newObj: VTree, parent: Element) {
1414
}
1515
}
1616
// replace everything function
17-
function replace (c: VTree, n: VTree, parent: Element) {
17+
function replace (c: VTree, n: VTree, parent: Element) : void {
1818
// step1 : prepare to delete, unmount things
1919
callBeforeDestroyedRecursive(c);
2020
// ^ this will unmount sub components before we replace the child
@@ -33,7 +33,7 @@ function replace (c: VTree, n: VTree, parent: Element) {
3333
};
3434

3535
// destroy vtext, vnode, vcomp
36-
function destroy (obj: VTree, parent: Element) {
36+
function destroy (obj: VTree, parent: Element) : void {
3737
// step 1: invoke destroy pre-hooks on vnode and vcomp
3838
callBeforeDestroyedRecursive(obj);
3939
// step 2: destroy
@@ -42,7 +42,7 @@ function destroy (obj: VTree, parent: Element) {
4242
callDestroyedRecursive(obj);
4343
};
4444

45-
function diffNodes (c: VTree, n: VTree, parent: Element) {
45+
function diffNodes (c: VTree, n: VTree, parent: Element) : void {
4646
// bail out on easy vtext case
4747
if (c['type'] === 'vtext') {
4848
if (c['text'] !== n['text']) c['domRef'].textContent = n['text'];
@@ -64,36 +64,36 @@ function diffNodes (c: VTree, n: VTree, parent: Element) {
6464
}
6565
};
6666
// ** recursive calls to hooks
67-
function callDestroyedRecursive (obj: VTree) {
67+
function callDestroyedRecursive (obj: VTree) : void {
6868
callDestroyed(obj);
6969
for (var i in obj['children']) {
7070
callDestroyedRecursive(obj['children'][i]);
7171
}
7272
};
7373

74-
function callDestroyed (obj: VTree) {
74+
function callDestroyed (obj: VTree) : void {
7575
if (obj['onDestroyed']) obj['onDestroyed']();
7676
};
7777

78-
function callBeforeDestroyed (obj: any) {
78+
function callBeforeDestroyed (obj: VTree) : void {
7979
if (obj['onBeforeDestroyed']) obj['onBeforeDestroyed']();
8080
if (obj['type'] === 'vcomp') obj['unmount'](obj['domRef']);
8181
};
8282

83-
function callBeforeDestroyedRecursive (obj: VTree) {
83+
function callBeforeDestroyedRecursive (obj: VTree) : void {
8484
callBeforeDestroyed(obj);
8585
for (var i in obj['children']) {
8686
callBeforeDestroyedRecursive(obj['children'][i]);
8787
}
8888
};
8989

9090
// ** </> recursive calls to hooks
91-
export function callCreated(obj: VTree) {
91+
export function callCreated(obj: VTree) : void {
9292
if (obj['onCreated']) obj['onCreated']();
9393
if (obj['type'] === 'vcomp') mountComponent(obj);
9494
}
9595

96-
export function populate(c: VTree, n: VTree) {
96+
export function populate(c: VTree, n: VTree) : void {
9797
if (!c) {
9898
c = {
9999
props: null,
@@ -125,7 +125,7 @@ function diffProps
125125
, nProps: Map<string,string>
126126
, node: Element
127127
, isSvg: boolean
128-
) {
128+
) : void {
129129
var newProp;
130130
/* Is current prop in new prop list? */
131131
for (var c in cProps) {
@@ -173,7 +173,11 @@ function diffProps
173173
}
174174
};
175175

176-
function diffCss (cCss: Map<string,string>, nCss: Map<string,string>, node: HTMLElement) {
176+
function diffCss
177+
( cCss: Map<string,string>
178+
, nCss: Map<string,string>
179+
, node: HTMLElement
180+
) : void {
177181
var result;
178182
/* is current attribute in new attribute list? */
179183
for (var c in cCss) {
@@ -192,7 +196,7 @@ function diffCss (cCss: Map<string,string>, nCss: Map<string,string>, node: HTML
192196
}
193197
};
194198

195-
function hasKeys (ns: Array<VTree>, cs: Array<VTree>) {
199+
function hasKeys (ns: Array<VTree>, cs: Array<VTree>) : boolean {
196200
return (
197201
ns.length > 0 &&
198202
cs.length > 0 &&
@@ -201,7 +205,7 @@ function hasKeys (ns: Array<VTree>, cs: Array<VTree>) {
201205
);
202206
};
203207

204-
function diffChildren (cs: Array<VTree>, ns: Array<VTree>, parent: HTMLElement) {
208+
function diffChildren (cs: Array<VTree>, ns: Array<VTree>, parent: HTMLElement) : void {
205209
var longest: number = ns.length > cs.length ? ns.length : cs.length;
206210
if (hasKeys(ns, cs)) {
207211
syncChildren(cs, ns, parent);
@@ -212,7 +216,7 @@ function diffChildren (cs: Array<VTree>, ns: Array<VTree>, parent: HTMLElement)
212216
}
213217
};
214218

215-
function populateDomRef (obj: VTree) {
219+
function populateDomRef (obj: VTree) : void {
216220
if (obj['ns'] === 'svg') {
217221
obj['domRef'] = document.createElementNS(
218222
'http://www.w3.org/2000/svg',
@@ -228,15 +232,15 @@ function populateDomRef (obj: VTree) {
228232
}
229233
};
230234
// dmj: refactor this, the callback function feels meh
231-
function createElement (obj: VTree, cb: ((Element) => void)) {
235+
function createElement (obj: VTree, cb: ((Element) => void)) : void {
232236
populateDomRef(obj);
233237
cb(obj['domRef']);
234238
populate(null, obj);
235239
callCreated(obj);
236240
};
237241
// mounts vcomp by calling into Haskell side.
238242
// unmount is handled with pre-destroy recursive hooks
239-
function mountComponent (obj: VTree) {
243+
function mountComponent (obj: VTree) : void {
240244
var componentId = obj['data-component-id'],
241245
nodeList = document.querySelectorAll(
242246
"[data-component-id='" + componentId + "']",
@@ -262,7 +266,7 @@ function mountComponent (obj: VTree) {
262266
});
263267
};
264268
// creates nodes on virtual and dom (vtext, vcomp, vnode)
265-
function create (obj: VTree, parent: Element) {
269+
function create (obj: VTree, parent: Element) : void {
266270
if (obj.type === 'vtext') {
267271
obj['domRef'] = document.createTextNode(obj['text']);
268272
parent.appendChild(obj['domRef']);
@@ -273,7 +277,7 @@ function create (obj: VTree, parent: Element) {
273277
}
274278
};
275279
/* Child reconciliation algorithm, inspired by kivi and Bobril */
276-
function syncChildren (os: Array<VTree>, ns: Array<VTree>, parent: Element) {
280+
function syncChildren (os: Array<VTree>, ns: Array<VTree>, parent: Element) : void {
277281
var oldFirstIndex: number = 0,
278282
newFirstIndex: number = 0,
279283
oldLastIndex: number = os.length - 1,
@@ -284,7 +288,7 @@ function syncChildren (os: Array<VTree>, ns: Array<VTree>, parent: Element) {
284288
oLast: VTree,
285289
oFirst: VTree,
286290
found: boolean,
287-
node: any;
291+
node: VTree;
288292
for (;;) {
289293
/* check base case, first > last for both new and old
290294
[ ] -- old children empty (fully-swapped)
@@ -333,7 +337,7 @@ function syncChildren (os: Array<VTree>, ns: Array<VTree>, parent: Element) {
333337
-> [ c b a ] <- new children
334338
*/
335339
else if (oFirst['key'] === nLast['key'] && nFirst['key'] === oLast['key']) {
336-
swapDOMRefs(node, oLast['domRef'], oFirst['domRef'], parent);
340+
swapDOMRefs(oLast['domRef'], oFirst['domRef'], parent);
337341
swap<VTree>(os, oldFirstIndex, oldLastIndex);
338342
diff(os[oldFirstIndex++], ns[newFirstIndex++], parent);
339343
diff(os[oldLastIndex--], ns[newLastIndex--], parent);
@@ -430,13 +434,13 @@ function syncChildren (os: Array<VTree>, ns: Array<VTree>, parent: Element) {
430434
}
431435
};
432436

433-
function swapDOMRefs (tmp: ChildNode, a: Element, b: Element, p: Element) {
434-
tmp = a.nextSibling;
437+
function swapDOMRefs (a: Element, b: Element, p: Element) : void {
438+
const tmp = a.nextSibling;
435439
p.insertBefore(a, b);
436440
p.insertBefore(b, tmp);
437441
};
438442

439-
function swap<T> (os: Array<T>, l: number, r: number) {
443+
function swap<T> (os: Array<T>, l: number, r: number) : void {
440444
var k = os[l];
441445
os[l] = os[r];
442446
os[r] = k;

0 commit comments

Comments
 (0)