more anys

This commit is contained in:
Olivier Gagnon 2022-07-19 22:54:38 -04:00
parent ce2ebf576e
commit f220965a73
2 changed files with 22 additions and 15 deletions

@ -2,38 +2,45 @@ import { Interpreter } from "../ThirdParty/JSInterpreter";
const defaultInterpreter = new Interpreter("", () => undefined);
interface PseudoObject {
properties: Record<string, unknown>;
class: string;
}
const isPseudoObject = (v: unknown): v is PseudoObject =>
!!v &&
typeof v === "object" &&
v.hasOwnProperty("properties") &&
v.hasOwnProperty("getter") &&
v.hasOwnProperty("setter") &&
v.hasOwnProperty("proto");
// the acorn interpreter has a bug where it doesn't convert arrays correctly.
// so we have to more or less copy it here.
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export function toNative(pseudoObj: any): any {
export function toNative(pseudoObj: unknown): unknown {
if (pseudoObj == null) return null;
if (
!pseudoObj.hasOwnProperty("properties") ||
!pseudoObj.hasOwnProperty("getter") ||
!pseudoObj.hasOwnProperty("setter") ||
!pseudoObj.hasOwnProperty("proto")
) {
if (!isPseudoObject(pseudoObj)) {
return pseudoObj; // it wasn't a pseudo object anyway.
}
let nativeObj: any;
if (pseudoObj.hasOwnProperty("class") && pseudoObj.class === "Array") {
nativeObj = [];
const arr: unknown[] = [];
const length = defaultInterpreter.getProperty(pseudoObj, "length");
if (typeof length === "number") {
for (let i = 0; i < length; i++) {
if (defaultInterpreter.hasProperty(pseudoObj, i)) {
nativeObj[i] = toNative(defaultInterpreter.getProperty(pseudoObj, i));
arr[i] = toNative(defaultInterpreter.getProperty(pseudoObj, i));
}
}
}
return arr;
} else {
// Object.
nativeObj = {};
const obj: Record<string, unknown> = {};
for (const key of Object.keys(pseudoObj.properties)) {
const val = pseudoObj.properties[key];
nativeObj[key] = toNative(val);
obj[key] = toNative(val);
}
return obj;
}
}
return nativeObj;
}

@ -60,7 +60,7 @@ export function Generic_toJSON(ctorName: string, obj: Record<string, any>, keys?
keys = Object.keys(obj); // Only "own" properties are included
}
const data: any = {};
const data: Record<string, unknown> = {};
for (let index = 0; index < keys.length; ++index) {
const key = keys[index];
data[key] = obj[key];