Skip to content

Commit 106750e

Browse files
committed
finish rewriting js to ts
1 parent 0468ccd commit 106750e

File tree

5 files changed

+118
-224
lines changed

5 files changed

+118
-224
lines changed

src/backend/astParser.js

Lines changed: 0 additions & 65 deletions
This file was deleted.
Lines changed: 113 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,113 @@
1-
/* eslint-disable linebreak-style */
2-
/* eslint-disable no-shadow */
3-
/* eslint-disable max-len */
4-
/* eslint-disable no-console */
5-
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
6-
/* eslint-disable @typescript-eslint/no-var-requires */
7-
/* eslint-disable linebreak-style */
8-
/* eslint-disable no-inner-declarations, no-loop-func */
9-
// eslint-disable-next-line import/newline-after-import
10-
const acorn = require('acorn');
11-
const jsx = require('acorn-jsx');
12-
// import { acorn } from 'acorn'; // javascript parser
13-
// import { jsx } from 'acorn-jsx';
14-
15-
const JSXParser = acorn.Parser.extend(jsx());
16-
17-
// Returns a throttled version of an input function
18-
// The returned throttled function only executes at most once every t milliseconds
19-
export const throttle = (f, t) => {
20-
let isOnCooldown = false;
21-
let isCallQueued = false;
22-
const throttledFunc = () => {
23-
if (isOnCooldown && isCallQueued) return;
24-
if (isOnCooldown) {
25-
isCallQueued = true;
26-
return;
27-
}
28-
f();
29-
isOnCooldown = true;
30-
isCallQueued = false;
31-
32-
const runAfterTimeout = () => {
33-
if (isCallQueued) {
34-
isCallQueued = false;
35-
isOnCooldown = true; // not needed I think
36-
f();
37-
setTimeout(runAfterTimeout, t);
38-
return;
39-
}
40-
isOnCooldown = false;
41-
};
42-
setTimeout(runAfterTimeout, t);
43-
};
44-
return throttledFunc;
45-
};
46-
47-
// Helper function to grab the getters/setters from `elementType`
48-
export const getHooksNames = (elementType) => {
49-
// Initialize empty object to store the setters and getter
50-
let ast;
51-
try {
52-
ast = JSXParser.parse(elementType);
53-
} catch (e) {
54-
return ['unknown'];
55-
}
56-
const hookState = {};
57-
const hooksNames = {};
58-
59-
while (Object.hasOwnProperty.call(ast, 'body')) {
60-
let tsCount = 0; // Counter for the number of TypeScript hooks seen (to distinguish in masterState)
61-
ast = ast.body;
62-
const statements = [];
63-
64-
/** All module exports always start off as a single 'FunctionDeclaration' type
65-
* Other types: "BlockStatement" / "ExpressionStatement" / "ReturnStatement"
66-
* Iterate through AST of every function declaration
67-
* Check within each function declaration if there are hook declarations */
68-
ast.forEach((functionDec) => {
69-
let body;
70-
if (functionDec.expression && functionDec.expression.body)
71-
body = functionDec.expression.body.body;
72-
else body = functionDec.body ? functionDec.body.body : [];
73-
// Traverse through the function's funcDecs and Expression Statements
74-
body.forEach((elem) => {
75-
if (elem.type === 'VariableDeclaration') {
76-
elem.declarations.forEach((hook) => {
77-
// * TypeScript hooks appear to have no "VariableDeclarator"
78-
// * with id.name of _useState, _useState2, etc...
79-
// * hook.id.type relevant for TypeScript applications
80-
// *
81-
// * Works for useState hooks
82-
if (hook.id.type === 'ArrayPattern') {
83-
hook.id.elements.forEach((hook) => {
84-
statements.push(hook.name);
85-
// * Unshift a wildcard name to achieve similar functionality as before
86-
statements.unshift(`_useWildcard${tsCount}`);
87-
tsCount += 1;
88-
});
89-
} else {
90-
if (hook.init.object && hook.init.object.name) {
91-
const varName = hook.init.object.name;
92-
if (!hooksNames[varName] && varName.match(/_use/)) {
93-
hooksNames[varName] = hook.id.name;
94-
}
95-
}
96-
if (hook.id.name !== undefined) {
97-
statements.push(hook.id.name);
98-
}
99-
}
100-
});
101-
}
102-
});
103-
104-
statements.forEach((el, i) => {
105-
if (el.match(/_use/)) hookState[el] = statements[i + 2];
106-
});
107-
});
108-
}
109-
return Object.values(hooksNames);
110-
};
1+
/* eslint-disable linebreak-style */
2+
/* eslint-disable no-shadow */
3+
/* eslint-disable max-len */
4+
/* eslint-disable no-console */
5+
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
6+
/* eslint-disable @typescript-eslint/no-var-requires */
7+
/* eslint-disable linebreak-style */
8+
/* eslint-disable no-inner-declarations, no-loop-func */
9+
// eslint-disable-next-line import/newline-after-import
10+
const acorn = require('acorn');
11+
const jsx = require('acorn-jsx');
12+
// import { acorn } from 'acorn'; // javascript parser
13+
// import { jsx } from 'acorn-jsx';
14+
15+
const JSXParser = acorn.Parser.extend(jsx());
16+
17+
// Returns a throttled version of an input function
18+
// The returned throttled function only executes at most once every t milliseconds
19+
export const throttle = (f : Function, t: number) : Function => {
20+
let isOnCooldown : boolean = false;
21+
let isCallQueued : boolean = false;
22+
const throttledFunc = () : any => {
23+
if (isOnCooldown && isCallQueued) return;
24+
if (isOnCooldown) {
25+
isCallQueued = true;
26+
return;
27+
}
28+
f();
29+
isOnCooldown = true;
30+
isCallQueued = false;
31+
console.log(5);
32+
const runAfterTimeout = () : any => {
33+
if (isCallQueued) {
34+
isCallQueued = false;
35+
isOnCooldown = true; // not needed I think
36+
f();
37+
setTimeout(runAfterTimeout, t);
38+
return;
39+
}
40+
isOnCooldown = false;
41+
};
42+
setTimeout(runAfterTimeout, t);
43+
};
44+
return throttledFunc;
45+
};
46+
47+
// Helper function to grab the getters/setters from `elementType`
48+
export const getHooksNames = (elementType : string) : Array<string> => {
49+
// Initialize empty object to store the setters and getter
50+
let ast : any;
51+
try {
52+
ast = JSXParser.parse(elementType);
53+
} catch (e) {
54+
return ['unknown'];
55+
}
56+
57+
// const hookState = {};
58+
// const hooksNames = {};
59+
const hookState: any = {};
60+
const hooksNames: any = {};
61+
62+
while (Object.hasOwnProperty.call(ast, 'body')) {
63+
let tsCount : number = 0; // Counter for the number of TypeScript hooks seen (to distinguish in masterState)
64+
ast = ast.body;
65+
const statements : Array<any> = [];
66+
67+
/** All module exports always start off as a single 'FunctionDeclaration' type
68+
* Other types: "BlockStatement" / "ExpressionStatement" / "ReturnStatement"
69+
* Iterate through AST of every function declaration
70+
* Check within each function declaration if there are hook declarations */
71+
ast.forEach((functionDec) => {
72+
let body : any;
73+
if (functionDec.expression && functionDec.expression.body)
74+
body = functionDec.expression.body.body;
75+
else body = functionDec.body ? functionDec.body.body : [];
76+
// Traverse through the function's funcDecs and Expression Statements
77+
body.forEach((elem : any) => {
78+
if (elem.type === 'VariableDeclaration') {
79+
elem.declarations.forEach((hook : any) => {
80+
// * TypeScript hooks appear to have no "VariableDeclarator"
81+
// * with id.name of _useState, _useState2, etc...
82+
// * hook.id.type relevant for TypeScript applications
83+
// *
84+
// * Works for useState hooks
85+
if (hook.id.type === 'ArrayPattern') {
86+
hook.id.elements.forEach((hook) => {
87+
statements.push(hook.name);
88+
// * Unshift a wildcard name to achieve similar functionality as before
89+
statements.unshift(`_useWildcard${tsCount}`);
90+
tsCount += 1;
91+
});
92+
} else {
93+
if (hook.init.object && hook.init.object.name) {
94+
const varName : any = hook.init.object.name;
95+
if (!hooksNames[varName] && varName.match(/_use/)) {
96+
hooksNames[varName] = hook.id.name;
97+
}
98+
}
99+
if (hook.id.name !== undefined) {
100+
statements.push(hook.id.name);
101+
}
102+
}
103+
});
104+
}
105+
});
106+
107+
statements.forEach((el, i) => {
108+
if (el.match(/_use/)) hookState[el] = statements[i + 2];
109+
});
110+
});
111+
}
112+
return Object.values(hooksNames);
113+
};

src/backend/linkFiber.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ const circularComponentTable = new Set();
5959

6060
export default (snap: Snapshot, mode: Mode): (() => void) => {
6161
let fiberRoot = null;
62-
6362
function sendSnapshot(): void {
6463
// Don't send messages while jumping or while paused
6564
if (mode.jumping || mode.paused) return;

src/backend/masterState.js

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/backend/masterState.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,17 @@ import 'core-js';
33
/* eslint-disable no-plusplus */
44
/* eslint-disable guard-for-in */
55
/* eslint-disable no-restricted-syntax */
6-
const componentActionsRecord = {};
7-
let index = 0;
6+
const componentActionsRecord : Array<any> = [];
7+
let index : number = 0;
88

99
export default {
10-
saveNew: (state, component) => {
10+
saveNew: (state, component) : number => {
1111
componentActionsRecord[index] = { state, component };
1212
index++;
1313
return index - 1;
1414
},
15-
getRecordByIndex: inputIndex => componentActionsRecord[inputIndex],
16-
getComponentByIndex: inputIndex => (componentActionsRecord[inputIndex]
15+
getRecordByIndex: (inputIndex : number) : any => componentActionsRecord[inputIndex],
16+
getComponentByIndex: (inputIndex: number) : any => (componentActionsRecord[inputIndex]
1717
? componentActionsRecord[inputIndex].component
1818
: undefined),
1919
};
20-
21-
class twoHaha {
22-
private componenActionsRecord : Array<any>;
23-
private index = 0;
24-
}

0 commit comments

Comments
 (0)