serialize-javascript icon indicating copy to clipboard operation
serialize-javascript copied to clipboard

feat: option to inject code verbatim

Open AndersDJohnson opened this issue 6 years ago • 0 comments

It would be handy for a use case I have for this package to support a feature that could inject code verbatim if it appears in a JavaScript object.

To implement, we could use (A) a special object constructor, (B) a special property on a string, or (C) a tagged template literal returning some special value, which would allow us to detect such cases in the serialization code.

A:

const object = {
  customCode: serialize.verbatim('window.navigator.userAgent')
};

B:

const customCode = 'window.navigator.userAgent';
customCode.verbatim = true;
const object = {
  customCode
};

C:

const customCode = serialize.verbatim`window.navigator.userAgent`;
const object = {
  customCode
};

I tried a hack similar to https://github.com/yahoo/serialize-javascript/issues/32#issuecomment-350184098 using a function as a value and overriding its toString method, but it didn't work since I can't include ( in the code without it being rewritten as part of the function serialization.

AndersDJohnson avatar Jan 01 '19 20:01 AndersDJohnson