UNPKG

prettier-plugin-firebase-database

Version:
125 lines (119 loc) 3.94 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name2 in all) __defProp(target, name2, { get: all[name2], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { languages: () => languages, parsers: () => parsers, printers: () => printers }); module.exports = __toCommonJS(src_exports); // package.json var name = "prettier-plugin-firebase-database"; // src/printer.ts var import_prettier = require("prettier"); var MIN_PRINT_WIDTH = 30; var print = async (node, options) => { const lineEnding = { auto: "\n", crlf: "\n\r", cr: "\r", lf: "\n" }[options.endOfLine] ?? "\n"; const indentSymbol = options.useTabs ? " " : " "; const formattedStructure = await (0, import_prettier.format)(node.structure, { ...options, parser: "json-stringify" }); const rules = Array.from(node.rules); const output = formattedStructure.split(lineEnding).map(async (line) => { let rulePosition = line.indexOf(ruleString); if (rulePosition === -1) return line; const rule = rules.shift(); if (typeof rule === "undefined") throw new Error("Failed printing the structure"); const formattedRule = (await (0, import_prettier.format)(`${rule}`, { ...options, parser: "babel", singleQuote: true, trailingComma: "es5", printWidth: options.printWidth - rulePosition, semi: true })).trim(); const trimmedRule = formattedRule.endsWith(";") ? formattedRule.slice(0, -1) : formattedRule; const splitRule = trimmedRule.split(lineEnding); const wrapLine = options.printWidth - rulePosition < MIN_PRINT_WIDTH; if (wrapLine) rulePosition = line.search(/\S/) + options.tabWidth; const indentString = Array.from({ length: options.useTabs ? Math.floor(rulePosition / options.tabWidth) : rulePosition }).fill(indentSymbol).join(""); const indentedRule = wrapLine ? `${lineEnding}${splitRule.map((ruleLine) => `${indentString}${ruleLine}`).join(lineEnding)}` : `${splitRule[0]}${splitRule.length === 1 ? "" : lineEnding}${splitRule.slice(1).map((ruleLine) => `${indentString}${ruleLine}`).join(lineEnding)}`; return `${line.slice(0, rulePosition)}${indentedRule}",`; }); if (rules.length > 0) throw new Error("Failed printing the structure"); return (await Promise.all(output)).join(lineEnding); }; var printer = { print: ({ stack: [node] }) => node.printed }; // src/parser.ts var regex = /: ?"([^"]+)"/g; var ruleString = "~~rule~~"; var parse = async (text, options) => { const rules = []; const structure = text.replace(regex, (_match, rule) => { rules.push(rule); return `: "${ruleString}"`; }); const node = { structure, rules, length: text.length, printed: "" }; node.printed = await print(node, options); return node; }; var parser = { parse, astFormat: name, locStart: () => 0, locEnd: (node) => node.length }; // src/index.ts var languages = [ { name, parsers: [name], extensions: [".rules"], vscodeLanguageIds: ["firebasedatabaserules"] } ]; var parsers = { [name]: parser }; var printers = { [name]: printer }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { languages, parsers, printers });