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

Function parseVue

src/language-html/parse/parse.js:45–109  ·  view source on GitHub ↗

@param {string} input @param {ParseOptions} parseOptions

(input, parseOptions)

Source from the content-addressed store, hash-verified

43@param {ParseOptions} parseOptions
44*/
45function parseVue(input, parseOptions) {
46 const angularHtmlParserParseOptions =
47 toAngularHtmlParserParseOptions(parseOptions);
48 let { rootNodes, errors } = angularHtmlParserParse(
49 input,
50 angularHtmlParserParseOptions,
51 );
52
53 const isHtml = rootNodes.some(
54 (node) =>
55 (node.kind === "docType" && node.value === "html") ||
56 (node.kind === "element" && node.name.toLowerCase() === "html"),
57 );
58
59 // If not Vue.js SFC, treat as HTML
60 if (isHtml) {
61 return parseHtml(input, HTML_PARSE_OPTIONS);
62 }
63
64 /** @type {ParseTreeResult | undefined} */
65 let secondParseResult;
66 const getHtmlParseResult = () =>
67 (secondParseResult ??= angularHtmlParserParse(input, {
68 ...angularHtmlParserParseOptions,
69 getTagContentType: undefined,
70 }));
71
72 const getElementWithSameLocation = (node) => {
73 const { offset } = node.startSourceSpan.start;
74 return (
75 getHtmlParseResult().rootNodes.find(
76 (searching) =>
77 searching.kind === "element" &&
78 searching.startSourceSpan.start.offset === offset,
79 ) ?? node
80 );
81 };
82 for (const [index, node] of rootNodes.entries()) {
83 if (node.kind !== "element") {
84 continue;
85 }
86
87 if (node.isVoid) {
88 errors = getHtmlParseResult().errors;
89 rootNodes[index] = getElementWithSameLocation(node);
90 } else if (shouldParseVueRootNodeAsHtml(node)) {
91 const { endSourceSpan, startSourceSpan } = node;
92 const error = getHtmlParseResult().errors.find(
93 (error) =>
94 error.span.start.offset > startSourceSpan.start.offset &&
95 error.span.start.offset < endSourceSpan.end.offset,
96 );
97 if (error) {
98 throwParseError(error);
99 }
100 rootNodes[index] = getElementWithSameLocation(node);
101 }
102 }

Callers

nothing calls this directly

Calls 6

parseHtmlFunction · 0.85
getHtmlParseResultFunction · 0.85
throwParseErrorFunction · 0.85

Tested by

no test coverage detected