MCPcopy Create free account
hub / github.com/prettier/prettier / parseJson

Function parseJson

src/language-json/parse/json.js:34–71  ·  view source on GitHub ↗
(text, options = {})

Source from the content-addressed store, hash-verified

32}
33
34function 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
73function createJsonError(node, description) {
74 const [start, end] = [node.loc.start, node.loc.end].map(

Callers 2

parsers.jsFile · 0.90
readJsonFunction · 0.85

Calls 6

parseEmptyJsonFunction · 0.85
createBabelParseErrorFunction · 0.85
isNonEmptyArrayFunction · 0.85
createJsonErrorFunction · 0.85
assertJsonNodeFunction · 0.85
wrapExpressionFunction · 0.85

Tested by

no test coverage detected