Expose position and/or input of source text? · Issue #10 · tc39/proposal-json-parse-with-source (original) (raw)

Skip to content

Provide feedback

Saved searches

Use saved searches to filter your results more quickly

Sign up

Appearance settings

@gibson042

Description

@gibson042

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.