"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isUnformattable = exports.isRoot = exports.isMultiBlock = exports.isBlock = exports.isInline = exports.parseGoTemplate = void 0; const create_id_generator_1 = require("./create-id-generator"); const parseGoTemplate = (text, options) => { var _a, _b, _c, _d, _e, _f, _g, _h, _j; const regex = /{{(?-|<|%|\/\*)?\s*(?(?if|range|block|with|define|end|else|prettier-ignore-start|prettier-ignore-end)?[\s\S]*?)\s*(?-|>|%|\*\/)?}}|(?<(script)((?!<)[\s\S])*>((?!<\/script)[\s\S])*?{{[\s\S]*?<\/(script)>)|(?<(style)((?!<)[\s\S])*>((?!<\/style)[\s\S])*?{{[\s\S]*?<\/(style)>)/g; const root = { type: "root", content: text, aliasedContent: "", children: {}, index: 0, contentStart: 0, length: text.length, }; const nodeStack = [root]; const getId = (0, create_id_generator_1.createIdGenerator)(); for (let match of text.matchAll(regex)) { const current = last(nodeStack); const keyword = (_a = match.groups) === null || _a === void 0 ? void 0 : _a.keyword; const statement = (_b = match.groups) === null || _b === void 0 ? void 0 : _b.statement; const unformattable = (_d = (_c = match.groups) === null || _c === void 0 ? void 0 : _c.unformattableScript) !== null && _d !== void 0 ? _d : (_e = match.groups) === null || _e === void 0 ? void 0 : _e.unformattableStyle; const startDelimiter = ((_g = (_f = match.groups) === null || _f === void 0 ? void 0 : _f.startdelimiter) !== null && _g !== void 0 ? _g : ""); const endDelimiter = ((_j = (_h = match.groups) === null || _h === void 0 ? void 0 : _h.endDelimiter) !== null && _j !== void 0 ? _j : ""); if (current === undefined) { throw Error("Node stack empty."); } if (match.index === undefined) { throw Error("Regex match index undefined."); } const id = getId(); if (unformattable) { current.children[id] = { id, type: "unformattable", index: match.index, length: match[0].length, content: unformattable, parent: current, }; continue; } if (statement === undefined) { throw Error("Formattable match without statement."); } const inline = { index: match.index, length: match[0].length, startDelimiter, endDelimiter, parent: current, type: "inline", statement, id, }; if (keyword === "end" || keyword === "prettier-ignore-end") { if (current.type !== "block") { throw Error("Encountered unexpected end keyword."); } current.length = match[0].length + match.index - current.index; current.content = text.substring(current.contentStart, match.index); current.aliasedContent = aliasNodeContent(current); current.end = inline; if (current.parent.type === "double-block") { const firstChild = current.parent.blocks[0]; const lastChild = current.parent.blocks[current.parent.blocks.length - 1]; current.parent.length = lastChild.index + lastChild.length - firstChild.index; } nodeStack.pop(); } else if (isBlock(current) && keyword === "else") { const nextChild = { type: "block", start: inline, end: null, children: {}, keyword: keyword, index: match.index, parent: current.parent, contentStart: match.index + match[0].length, content: "", aliasedContent: "", length: -1, id: getId(), startDelimiter, endDelimiter, }; if (isMultiBlock(current.parent)) { current.parent.blocks.push(nextChild); } else { const multiBlock = { type: "double-block", parent: current.parent, index: current.index, length: -1, keyword, id: current.id, blocks: [current, nextChild], }; nextChild.parent = multiBlock; current.parent = multiBlock; if ("children" in multiBlock.parent) { multiBlock.parent.children[multiBlock.id] = multiBlock; } else { throw Error("Could not find child in parent."); } } current.id = getId(); current.length = match[0].length + match.index - current.index; current.content = text.substring(current.contentStart, match.index); current.aliasedContent = aliasNodeContent(current); nodeStack.pop(); nodeStack.push(nextChild); } else if (keyword) { const block = { type: "block", start: inline, end: null, children: {}, keyword: keyword, index: match.index, parent: current, contentStart: match.index + match[0].length, content: "", aliasedContent: "", length: -1, id: getId(), startDelimiter, endDelimiter, }; current.children[block.id] = block; nodeStack.push(block); } else { current.children[inline.id] = inline; } } if (!isRoot(nodeStack.pop())) { throw Error("Missing end block."); } root.aliasedContent = aliasNodeContent(root); return root; }; exports.parseGoTemplate = parseGoTemplate; function aliasNodeContent(current) { let result = current.content; Object.entries(current.children) .sort(([_, node1], [__, node2]) => node2.index - node1.index) .forEach(([id, node]) => (result = result.substring(0, node.index - current.contentStart) + id + result.substring(node.index + node.length - current.contentStart))); return result; } function last(array) { return array[array.length - 1]; } function isInline(node) { return node.type === "inline"; } exports.isInline = isInline; function isBlock(node) { return node.type === "block"; } exports.isBlock = isBlock; function isMultiBlock(node) { return node.type === "double-block"; } exports.isMultiBlock = isMultiBlock; function isRoot(node) { return node.type === "root"; } exports.isRoot = isRoot; function isUnformattable(node) { return node.type === "unformattable"; } exports.isUnformattable = isUnformattable; //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicGFyc2UuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvcGFyc2UudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7O0FBQ0EsK0RBQTBEO0FBRW5ELE1BQU0sZUFBZSxHQUE0QixDQUFDLElBQUksRUFBRSxPQUFPLEVBQUUsRUFBRTs7SUFDeEUsTUFBTSxLQUFLLEdBQ1QsK1dBQStXLENBQUM7SUFDbFgsTUFBTSxJQUFJLEdBQVc7UUFDbkIsSUFBSSxFQUFFLE1BQU07UUFDWixPQUFPLEVBQUUsSUFBSTtRQUNiLGNBQWMsRUFBRSxFQUFFO1FBQ2xCLFFBQVEsRUFBRSxFQUFFO1FBQ1osS0FBSyxFQUFFLENBQUM7UUFDUixZQUFZLEVBQUUsQ0FBQztRQUNmLE1BQU0sRUFBRSxJQUFJLENBQUMsTUFBTTtLQUNwQixDQUFDO0lBQ0YsTUFBTSxTQUFTLEdBQXlCLENBQUMsSUFBSSxDQUFDLENBQUM7SUFDL0MsTUFBTSxLQUFLLEdBQUcsSUFBQSx1Q0FBaUIsR0FBRSxDQUFDO0lBRWxDLEtBQUssSUFBSSxLQUFLLElBQUksSUFBSSxDQUFDLFFBQVEsQ0FBQyxLQUFLLENBQUMsRUFBRTtRQUN0QyxNQUFNLE9BQU8sR0FBRyxJQUFJLENBQUMsU0FBUyxDQUFDLENBQUM7UUFDaEMsTUFBTSxPQUFPLEdBQUcsTUFBQSxLQUFLLENBQUMsTUFBTSwwQ0FBRSxPQUFxQyxDQUFDO1FBQ3BFLE1BQU0sU0FBUyxHQUFHLE1BQUEsS0FBSyxDQUFDLE1BQU0sMENBQUUsU0FBUyxDQUFDO1FBQzFDLE1BQU0sYUFBYS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ICAgICB9XG5cbiAgICAgIG5vZGVTdGFjay5wb3AoKTtcbiAgICB9IGVsc2UgaWYgKGlzQmxvY2soY3VycmVudCkgJiYga2V5d29yZCA9PT0gXCJlbHNlXCIpIHtcbiAgICAgIGNvbnN0IG5leHRDaGlsZDogR29CbG9jayA9IHtcbiAgICAgICAgdHlwZTogXCJibG9ja1wiLFxuICAgICAgICBzdGFydDogaW5saW5lLFxuICAgICAgICBlbmQ6IG51bGwsXG4gICAgICAgIGNoaWxkcmVuOiB7fSxcbiAgICAgICAga2V5d29yZDoga2V5d29yZCxcbiAgICAgICAgaW5kZXg6IG1hdGNoLmluZGV4LFxuICAgICAgICBwYXJlbnQ6IGN1cnJlbnQucGFyZW50LFxuICAgICAgICBjb250ZW50U3RhcnQ6IG1hdGNoLmluZGV4ICsgbWF0Y2hbMF0ubGVuZ3RoLFxuICAgICAgICBjb250ZW50OiBcIlwiLFxuICAgICAgICBhbGlhc2VkQ29udGVudDogXCJcIixcbiAgICAgICAgbGVuZ3RoOiAtMSxcbiAgICAgICAgaWQ6IGdldElkKCksXG4gICAgICAgIHN0YXJ0RGVsaW1pdGVyLFxuICAgICAgICBlbmREZWxpbWl0ZXIsXG4gICAgICB9O1xuXG4gICAgICBpZiAoaXNNdWx0aUJsb2NrKGN1cnJlbnQucGFyZW50KSkge1xuICAgICAgICBjdXJyZW50LnBhcmVudC5ibG9ja3MucHVzaChuZXh0Q2hpbGQpO1xuICAgICAgfSBlbHNlIHtcbiAgICAgICAgY29uc3QgbXVsdGlCbG9jazogR29NdWx0aUJsb2NrID0ge1xuICAgICAgICAgIHR5cGU6IFwiZG91YmxlLWJsb2NrXCIsXG4gICAgICAgICAgcGFyZW50OiBjdXJyZW50LnBhcmVudCxcbiAgICAgICAgICBpbmRleDogY3VycmVudC5pbmRleCxcbiAgICAgICAgICBsZW5ndGg6IC0xLFxuICAgICAgICAgIGtleXdvcmQsXG4gICAgICAgICAgaWQ6IGN1cnJlbnQuaWQsXG4gICAgICAgICAgYmxvY2tzOiBbY3VycmVudCwgbmV4dENoaWxkXSxcbiAgICAgICAgfTtcbiAgICAgICAgbmV4dENoaWxkLnBhcmVudCA9IG11bHRpQmxvY2s7XG4gICAgICAgIGN1cnJlbnQucGFyZW50ID0gbXVsdGlCbG9jaztcblxuICAgICAgICBpZiAoXCJjaGlsZHJlblwiIGluIG11bHRpQmxvY2sucGFyZW50KSB7XG4gICAgICAgICAgbXVsdGlCbG9jay5wYXJlbnQuY2hpbGRyZW5bbXVsdGlCbG9jay5pZF0gPSBtdWx0aUJsb2NrO1xuICAgICAgICB9IGVsc2Uge1xuICAgICAgICAgIHRocm93IEVycm9yKFwiQ291bGQgbm90IGZpbmQgY2hpbGQgaW4gcGFyZW50LlwiKTtcbiAgICAgICAgfVxuICAgICAgfVxuXG4gICAgICBjdXJyZW50LmlkID0gZ2V0SWQoKTtcbiAgICAgIGN1cnJlbnQubGVuZ3RoID0gbWF0Y2hbMF0ubGVuZ3RoICsgbWF0Y2guaW5kZXggLSBjdXJyZW50LmluZGV4O1xuICAgICAgY3VycmVudC5jb250ZW50ID0gdGV4dC5zdWJzdHJpbmcoY3VycmVudC5jb250ZW50U3RhcnQsIG1hdGNoLmluZGV4KTtcbiAgICAgIGN1cnJlbnQuYWxpYXNlZENvbnRlbnQgPSBhbGlhc05vZGVDb250ZW50KGN1cnJlbnQpO1xuXG4gICAgICBub2RlU3RhY2sucG9wKCk7XG4gICAgICBub2RlU3RhY2sucHVzaChuZXh0Q2hpbGQpO1xuICAgIH0gZWxzZSBpZiAoa2V5d29yZCkge1xuICAgICAgY29uc3QgYmxvY2s6IEdvQmxvY2sgPSB7XG4gICAgICAgIHR5cGU6IFwiYmxvY2tcIixcbiAgICAgICAgc3RhcnQ6IGlubGluZSxcbiAgICAgICAgZW5kOiBudWxsLFxuICAgICAgICBjaGlsZHJlbjoge30sXG4gICAgICAgIGtleXdvcmQ6IGtleXdvcmQgYXMgR29CbG9ja0tleXdvcmQsXG4gICAgICAgIGluZGV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