Я пытаюсь превратить PropTypes
объекты в документацию, обернув PropTypes
прокси. До сих пор мне удалось с помощью следующего кода:
import _PropTypes from "prop-types";
class CallAtom {
constructor(prop, args) {
this.name = prop;
this.args = args;
}
toString() {
if (this.args == null) {
return "" + this.name;
}
return "" + this.name + "(" + this.args.join(", ") + ")";
}
}
function createCallChain(chain = [], prop, args) {
return chain.concat([new CallAtom(prop, args)]);
}
// set up a PropTypes proxy
function newProx(obj, callChain = []) {
const handler = {
get: (obj, prop) => {
if (prop === "callChain") {
return callChain;
const ret = obj[prop];
return newProx(ret, createCallChain(callChain, prop));
},
apply: (obj, thisArg, args) => {
const lastProp = callChain.pop() || { name: "<root>" };
return newProx(
obj(...args),
createCallChain(callChain, lastProp.name, args)
);
}
};
return new Proxy(obj, handler);
}
PropTypes = newProx(_PropTypes);
Затем я могу использовать прокси-версию PropTypes
и получить обратно цепочку вызовов.
tt = PropTypes.arrayOf({a: PropTypes.oneOf(["a", "b", "c"])}).isRequired
tt.callChain
работаеткак и ожидалось. Однако, когда я пытаюсь превратить его в объект JSON с
JSON.stringify(tt, function(k,v) {return (v||{}).callChain || v}))
, я получаю сообщение об ошибке TypeError: target argument of Proxy must be an object, got undefined
.
Есть ли способ заставить прокси-объект работать с заменителем JSONфункция