JSON superset
Closed this issue · 3 comments
I'm not sure this is a bug, but this behavior is not an ideal one, I think.
This plugin generates a source code that old JavaScript engine cannot parse when object includes U+2028 or U+2029 in a string. cf. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON#JavaScript_and_JSON_differences
before
const obj = {
foo: "\u2028\u2029"
};
console.log(obj.foo);
after
const obj = JSON.parse('{"foo":"
"}');
console.log(obj.foo);
When the code after conversion is run in node.js v8:
(function (exports, require, module, __filename, __dirname) { const obj = JSON.parse('{"foo":"
^^^^^^^^^
SyntaxError: Invalid or unexpected token
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:617:28)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Function.Module.runMain (module.js:694:10)
at startup (bootstrap_node.js:204:16)
at bootstrap_node.js:625:3
According to MDN, the latest Edge seems to fail too.
Relevant write-up: https://v8.dev/features/subsume-json
If you want optimal backwards compatibility, you could indeed escape those two characters. Alternatively you could document that this plugin produces output that only works in modern environments (e.g. Node.js v10+).
@nd-02110114 @mathiasbynens is this plugin production ready?
@mathiasbynens
Thank you for your advice 👍
Alternatively you could document that this plugin produces output that only works in modern environments (e.g. Node.js v10+).
I decided not to care about this backwards compatibility... (This is because Node.js v8 LTS is until 2019/12/31). I just documented this issue in README.