(text, options = {})
| 32 | } |
| 33 | |
| 34 | function parseJson(text, options = {}) { |
| 35 | const { allowComments = true, allowEmpty = false } = options; |
| 36 | |
| 37 | let ast; |
| 38 | let comments; |
| 39 | try { |
| 40 | ast = parseExpression(text, babelParseOptions); |
| 41 | comments = ast.comments; |
| 42 | } catch (/** @type {any} */ error) { |
| 43 | if ( |
| 44 | allowEmpty && |
| 45 | error.code === "BABEL_PARSER_SYNTAX_ERROR" && |
| 46 | error.reasonCode === "ParseExpressionEmptyInput" |
| 47 | ) { |
| 48 | try { |
| 49 | ({ comments } = parseEmptyJson(text)); |
| 50 | } catch { |
| 51 | // No op |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | if (!comments) { |
| 56 | throw createBabelParseError(error); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | if (!allowComments && isNonEmptyArray(comments)) { |
| 61 | throw createJsonError(comments[0], "Comment"); |
| 62 | } |
| 63 | |
| 64 | if (!allowEmpty || ast) { |
| 65 | assertJsonNode(ast); |
| 66 | } |
| 67 | |
| 68 | ast = wrapExpression({ type: "JsonRoot", expression: ast, comments, text }); |
| 69 | |
| 70 | return ast; |
| 71 | } |
| 72 | |
| 73 | function createJsonError(node, description) { |
| 74 | const [start, end] = [node.loc.start, node.loc.end].map( |
no test coverage detected