Expose position and/or input of source text? · Issue #10 · tc39/proposal-json-parse-with-source (original) (raw)
Navigation Menu
- Explore
- Pricing
Provide feedback
Saved searches
Use saved searches to filter your results more quickly
Appearance settings
Description
String.prototype.replace
passes position and input arguments to replacer functions and the return value from RegExp.prototype.exec
has "index" and "input" properties; JSON.parse
could behave similarly.
const input = '\n\t"use\u0020strict"'; let spied; const parsed = JSON.parse(input, (key, val, context) => (spied = context, val)); parsed === 'use strict'; // → true spied.source === '"use\u0020strict"'; // → true spied.index === 2; // → true spied.input === input; // → true
As noted by @rbuckton, this could be useful for error reporting.