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