prettier-plugin-marko
Advanced tools
| import { type Doc, doc as d } from "prettier"; | ||
| export declare function toValidAttrValue(doc: Doc, concise: boolean): d.builders.Doc; | ||
| export declare function toValidScriptlet(doc: Doc): d.builders.Doc; | ||
| export declare function toValidStatement(doc: Doc): d.builders.Doc; |
+57
-55
@@ -588,31 +588,2 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); | ||
| } | ||
| const b$3 = prettier.doc.builders; | ||
| const singleQuoteSpace = "${' '}"; | ||
| const doubleQuoteSpace = "${\" \"}"; | ||
| const singleQuoteSpaceIfBreak = b$3.ifBreak([singleQuoteSpace, b$3.line], " "); | ||
| const doubleQuoteSpaceIfBreak = b$3.ifBreak([doubleQuoteSpace, b$3.line], " "); | ||
| function ensureVisibleSpace(parts, opts) { | ||
| if (parts[0] === b$3.line) parts[0] = getVisibleSpace(opts); | ||
| if (parts[parts.length - 1] === b$3.line) parts[parts.length - 1] = getVisibleSpace(opts); | ||
| } | ||
| function ensureVisibleTrailingSpace(parts, opts) { | ||
| const last = parts.length - 1; | ||
| if (typeof parts[last] === "string" && /[ \t]$/.test(parts[last])) parts[last] = parts[last].slice(0, -1) + getVisibleSpace(opts); | ||
| } | ||
| function ensureVisibleSpaceBetweenTags(parts, opts) { | ||
| const last = parts.length - 1; | ||
| if (last > 0 && parts[last] === b$3.line) { | ||
| if (typeof parts[last - 1] !== "string") parts[last] = opts.singleQuote ? singleQuoteSpaceIfBreak : doubleQuoteSpaceIfBreak; | ||
| } | ||
| } | ||
| function getVisibleSpace(opts) { | ||
| return opts.singleQuote ? singleQuoteSpace : doubleQuoteSpace; | ||
| } | ||
| function isVisibleSpace(code) { | ||
| switch (code) { | ||
| case singleQuoteSpace: | ||
| case doubleQuoteSpace: return true; | ||
| default: return false; | ||
| } | ||
| } | ||
| const DocCache = /* @__PURE__ */ new WeakMap(); | ||
@@ -651,3 +622,3 @@ function printDoc(doc) { | ||
| case "indent-if-break": | ||
| cached = " "; | ||
| cached = " " + printDoc(doc.contents) + printDoc(doc.negate); | ||
| break; | ||
@@ -669,4 +640,27 @@ case "label": | ||
| const { builders: b$2 } = prettier.doc; | ||
| function withBlockIfNeeded(doc) { | ||
| if ((0, htmljs_parser.isValidStatement)(printDoc(doc).trim()) !== htmljs_parser.Validity.invalid) return doc; | ||
| function toValidAttrValue(doc, concise) { | ||
| switch ((0, htmljs_parser.isValidAttrValue)(printDoc(doc).trim(), concise)) { | ||
| case htmljs_parser.Validity.enclosed: return doc; | ||
| case htmljs_parser.Validity.valid: return b$2.group([ | ||
| b$2.ifBreak("("), | ||
| b$2.indent([b$2.softline, doc]), | ||
| b$2.softline, | ||
| b$2.ifBreak(")") | ||
| ]); | ||
| default: return b$2.group([ | ||
| "(", | ||
| b$2.indent([b$2.softline, doc]), | ||
| b$2.softline, | ||
| ")" | ||
| ]); | ||
| } | ||
| } | ||
| function toValidScriptlet(doc) { | ||
| return toValidBlock(doc, htmljs_parser.isValidScriptlet); | ||
| } | ||
| function toValidStatement(doc) { | ||
| return toValidBlock(doc, htmljs_parser.isValidStatement); | ||
| } | ||
| function toValidBlock(doc, check) { | ||
| if (check(printDoc(doc).trim()) === htmljs_parser.Validity.enclosed) return doc; | ||
| return b$2.group([ | ||
@@ -679,23 +673,31 @@ b$2.ifBreak("{"), | ||
| } | ||
| const { builders: b$1 } = prettier.doc; | ||
| function withParensIfNeeded(doc, concise) { | ||
| switch ((0, htmljs_parser.isValidAttrValue)(printDoc(doc).trim(), concise)) { | ||
| case htmljs_parser.Validity.enclosed: return doc; | ||
| case htmljs_parser.Validity.valid: return b$1.group([ | ||
| b$1.ifBreak("("), | ||
| b$1.indent([b$1.softline, doc]), | ||
| b$1.softline, | ||
| b$1.ifBreak(")") | ||
| ]); | ||
| default: return b$1.group([ | ||
| b$1.indent([ | ||
| "(", | ||
| b$1.softline, | ||
| doc | ||
| ]), | ||
| b$1.softline, | ||
| ")" | ||
| ]); | ||
| const b$1 = prettier.doc.builders; | ||
| const singleQuoteSpace = "${' '}"; | ||
| const doubleQuoteSpace = "${\" \"}"; | ||
| const singleQuoteSpaceIfBreak = b$1.ifBreak([singleQuoteSpace, b$1.line], " "); | ||
| const doubleQuoteSpaceIfBreak = b$1.ifBreak([doubleQuoteSpace, b$1.line], " "); | ||
| function ensureVisibleSpace(parts, opts) { | ||
| if (parts[0] === b$1.line) parts[0] = getVisibleSpace(opts); | ||
| if (parts[parts.length - 1] === b$1.line) parts[parts.length - 1] = getVisibleSpace(opts); | ||
| } | ||
| function ensureVisibleTrailingSpace(parts, opts) { | ||
| const last = parts.length - 1; | ||
| if (typeof parts[last] === "string" && /[ \t]$/.test(parts[last])) parts[last] = parts[last].slice(0, -1) + getVisibleSpace(opts); | ||
| } | ||
| function ensureVisibleSpaceBetweenTags(parts, opts) { | ||
| const last = parts.length - 1; | ||
| if (last > 0 && parts[last] === b$1.line) { | ||
| if (typeof parts[last - 1] !== "string") parts[last] = opts.singleQuote ? singleQuoteSpaceIfBreak : doubleQuoteSpaceIfBreak; | ||
| } | ||
| } | ||
| function getVisibleSpace(opts) { | ||
| return opts.singleQuote ? singleQuoteSpace : doubleQuoteSpace; | ||
| } | ||
| function isVisibleSpace(code) { | ||
| switch (code) { | ||
| case singleQuoteSpace: | ||
| case doubleQuoteSpace: return true; | ||
| default: return false; | ||
| } | ||
| } | ||
| const b = prettier.doc.builders; | ||
@@ -871,3 +873,3 @@ const traverseDoc = prettier.doc.utils.traverseDoc; | ||
| const code = opts._markoParsed.code.slice(node.start + node.target.length + 1, node.end).replace(/^\s*\{([\s\S]*)\}\s*$/, "$1").trim(); | ||
| return code ? [`${node.target} `, withBlockIfNeeded(await toDoc(code, stmtParse))] : []; | ||
| return code ? [`${node.target} `, toValidStatement(await toDoc(code, stmtParse))] : []; | ||
| }, | ||
@@ -879,3 +881,3 @@ [NodeType.Scriptlet]: async (toDoc, _print, path, opts) => { | ||
| "$ ", | ||
| withBlockIfNeeded(await toDoc(code, stmtParse)) | ||
| toValidScriptlet(await toDoc(code, stmtParse)) | ||
| ] : []; | ||
@@ -912,7 +914,7 @@ }, | ||
| } else return unexpectedDoc(opts, node); | ||
| } else attrDoc.push(node.value.bound ? ":=" : "=", withParensIfNeeded(await toDoc(read(node.value.value, opts), exprParse), isConcise(opts))); | ||
| } else attrDoc.push(node.value.bound ? ":=" : "=", toValidAttrValue(await toDoc(read(node.value.value, opts), exprParse), isConcise(opts))); | ||
| return b.group(attrDoc); | ||
| }, | ||
| [NodeType.AttrSpread]: async (toDoc, _print, path, opts) => { | ||
| return b.group(["...", withParensIfNeeded(await toDoc(read(path.node.value, opts), exprParse), isConcise(opts))]); | ||
| return b.group(["...", toValidAttrValue(await toDoc(read(path.node.value, opts), exprParse), isConcise(opts))]); | ||
| }, | ||
@@ -919,0 +921,0 @@ [NodeType.ShorthandId]: async (toDoc, _print, path, opts) => ["#", await templateToDoc(toDoc, path, opts)], |
+58
-56
| import { doc } from "prettier"; | ||
| import { TagType, TagType as TagType$1, Validity, createParser, isValidAttrValue, isValidStatement } from "htmljs-parser"; | ||
| import { TagType, TagType as TagType$1, Validity, createParser, isValidAttrValue, isValidScriptlet, isValidStatement } from "htmljs-parser"; | ||
| const styleBlockReg = /((?:\.[^\s\\/:*?"<>|({]+)*)\s*\{/y; | ||
@@ -587,31 +587,2 @@ const UNFINISHED = Number.MAX_SAFE_INTEGER; | ||
| } | ||
| const b$3 = doc.builders; | ||
| const singleQuoteSpace = "${' '}"; | ||
| const doubleQuoteSpace = "${\" \"}"; | ||
| const singleQuoteSpaceIfBreak = b$3.ifBreak([singleQuoteSpace, b$3.line], " "); | ||
| const doubleQuoteSpaceIfBreak = b$3.ifBreak([doubleQuoteSpace, b$3.line], " "); | ||
| function ensureVisibleSpace(parts, opts) { | ||
| if (parts[0] === b$3.line) parts[0] = getVisibleSpace(opts); | ||
| if (parts[parts.length - 1] === b$3.line) parts[parts.length - 1] = getVisibleSpace(opts); | ||
| } | ||
| function ensureVisibleTrailingSpace(parts, opts) { | ||
| const last = parts.length - 1; | ||
| if (typeof parts[last] === "string" && /[ \t]$/.test(parts[last])) parts[last] = parts[last].slice(0, -1) + getVisibleSpace(opts); | ||
| } | ||
| function ensureVisibleSpaceBetweenTags(parts, opts) { | ||
| const last = parts.length - 1; | ||
| if (last > 0 && parts[last] === b$3.line) { | ||
| if (typeof parts[last - 1] !== "string") parts[last] = opts.singleQuote ? singleQuoteSpaceIfBreak : doubleQuoteSpaceIfBreak; | ||
| } | ||
| } | ||
| function getVisibleSpace(opts) { | ||
| return opts.singleQuote ? singleQuoteSpace : doubleQuoteSpace; | ||
| } | ||
| function isVisibleSpace(code) { | ||
| switch (code) { | ||
| case singleQuoteSpace: | ||
| case doubleQuoteSpace: return true; | ||
| default: return false; | ||
| } | ||
| } | ||
| const DocCache = /* @__PURE__ */ new WeakMap(); | ||
@@ -650,3 +621,3 @@ function printDoc(doc) { | ||
| case "indent-if-break": | ||
| cached = " "; | ||
| cached = " " + printDoc(doc.contents) + printDoc(doc.negate); | ||
| break; | ||
@@ -668,4 +639,27 @@ case "label": | ||
| const { builders: b$2 } = doc; | ||
| function withBlockIfNeeded(doc) { | ||
| if (isValidStatement(printDoc(doc).trim()) !== Validity.invalid) return doc; | ||
| function toValidAttrValue(doc, concise) { | ||
| switch (isValidAttrValue(printDoc(doc).trim(), concise)) { | ||
| case Validity.enclosed: return doc; | ||
| case Validity.valid: return b$2.group([ | ||
| b$2.ifBreak("("), | ||
| b$2.indent([b$2.softline, doc]), | ||
| b$2.softline, | ||
| b$2.ifBreak(")") | ||
| ]); | ||
| default: return b$2.group([ | ||
| "(", | ||
| b$2.indent([b$2.softline, doc]), | ||
| b$2.softline, | ||
| ")" | ||
| ]); | ||
| } | ||
| } | ||
| function toValidScriptlet(doc) { | ||
| return toValidBlock(doc, isValidScriptlet); | ||
| } | ||
| function toValidStatement(doc) { | ||
| return toValidBlock(doc, isValidStatement); | ||
| } | ||
| function toValidBlock(doc, check) { | ||
| if (check(printDoc(doc).trim()) === Validity.enclosed) return doc; | ||
| return b$2.group([ | ||
@@ -678,23 +672,31 @@ b$2.ifBreak("{"), | ||
| } | ||
| const { builders: b$1 } = doc; | ||
| function withParensIfNeeded(doc, concise) { | ||
| switch (isValidAttrValue(printDoc(doc).trim(), concise)) { | ||
| case Validity.enclosed: return doc; | ||
| case Validity.valid: return b$1.group([ | ||
| b$1.ifBreak("("), | ||
| b$1.indent([b$1.softline, doc]), | ||
| b$1.softline, | ||
| b$1.ifBreak(")") | ||
| ]); | ||
| default: return b$1.group([ | ||
| b$1.indent([ | ||
| "(", | ||
| b$1.softline, | ||
| doc | ||
| ]), | ||
| b$1.softline, | ||
| ")" | ||
| ]); | ||
| const b$1 = doc.builders; | ||
| const singleQuoteSpace = "${' '}"; | ||
| const doubleQuoteSpace = "${\" \"}"; | ||
| const singleQuoteSpaceIfBreak = b$1.ifBreak([singleQuoteSpace, b$1.line], " "); | ||
| const doubleQuoteSpaceIfBreak = b$1.ifBreak([doubleQuoteSpace, b$1.line], " "); | ||
| function ensureVisibleSpace(parts, opts) { | ||
| if (parts[0] === b$1.line) parts[0] = getVisibleSpace(opts); | ||
| if (parts[parts.length - 1] === b$1.line) parts[parts.length - 1] = getVisibleSpace(opts); | ||
| } | ||
| function ensureVisibleTrailingSpace(parts, opts) { | ||
| const last = parts.length - 1; | ||
| if (typeof parts[last] === "string" && /[ \t]$/.test(parts[last])) parts[last] = parts[last].slice(0, -1) + getVisibleSpace(opts); | ||
| } | ||
| function ensureVisibleSpaceBetweenTags(parts, opts) { | ||
| const last = parts.length - 1; | ||
| if (last > 0 && parts[last] === b$1.line) { | ||
| if (typeof parts[last - 1] !== "string") parts[last] = opts.singleQuote ? singleQuoteSpaceIfBreak : doubleQuoteSpaceIfBreak; | ||
| } | ||
| } | ||
| function getVisibleSpace(opts) { | ||
| return opts.singleQuote ? singleQuoteSpace : doubleQuoteSpace; | ||
| } | ||
| function isVisibleSpace(code) { | ||
| switch (code) { | ||
| case singleQuoteSpace: | ||
| case doubleQuoteSpace: return true; | ||
| default: return false; | ||
| } | ||
| } | ||
| const b = doc.builders; | ||
@@ -870,3 +872,3 @@ const traverseDoc = doc.utils.traverseDoc; | ||
| const code = opts._markoParsed.code.slice(node.start + node.target.length + 1, node.end).replace(/^\s*\{([\s\S]*)\}\s*$/, "$1").trim(); | ||
| return code ? [`${node.target} `, withBlockIfNeeded(await toDoc(code, stmtParse))] : []; | ||
| return code ? [`${node.target} `, toValidStatement(await toDoc(code, stmtParse))] : []; | ||
| }, | ||
@@ -878,3 +880,3 @@ [NodeType.Scriptlet]: async (toDoc, _print, path, opts) => { | ||
| "$ ", | ||
| withBlockIfNeeded(await toDoc(code, stmtParse)) | ||
| toValidScriptlet(await toDoc(code, stmtParse)) | ||
| ] : []; | ||
@@ -911,7 +913,7 @@ }, | ||
| } else return unexpectedDoc(opts, node); | ||
| } else attrDoc.push(node.value.bound ? ":=" : "=", withParensIfNeeded(await toDoc(read(node.value.value, opts), exprParse), isConcise(opts))); | ||
| } else attrDoc.push(node.value.bound ? ":=" : "=", toValidAttrValue(await toDoc(read(node.value.value, opts), exprParse), isConcise(opts))); | ||
| return b.group(attrDoc); | ||
| }, | ||
| [NodeType.AttrSpread]: async (toDoc, _print, path, opts) => { | ||
| return b.group(["...", withParensIfNeeded(await toDoc(read(path.node.value, opts), exprParse), isConcise(opts))]); | ||
| return b.group(["...", toValidAttrValue(await toDoc(read(path.node.value, opts), exprParse), isConcise(opts))]); | ||
| }, | ||
@@ -918,0 +920,0 @@ [NodeType.ShorthandId]: async (toDoc, _print, path, opts) => ["#", await templateToDoc(toDoc, path, opts)], |
+2
-2
| { | ||
| "name": "prettier-plugin-marko", | ||
| "version": "4.0.6", | ||
| "version": "4.0.7", | ||
| "description": "A prettier plugin for parsing and printing Marko files", | ||
@@ -47,3 +47,3 @@ "keywords": [ | ||
| "dependencies": { | ||
| "htmljs-parser": "^5.9.0" | ||
| "htmljs-parser": "^5.10.0" | ||
| }, | ||
@@ -50,0 +50,0 @@ "devDependencies": { |
| import { type Doc, doc as d } from "prettier"; | ||
| export default function withBlockIfNeeded(doc: Doc): d.builders.Doc; |
| import { type Doc, doc as d } from "prettier"; | ||
| export default function withParensIfNeeded(doc: Doc, concise: boolean): d.builders.Doc; |
95659
0.35%2919
0.14%13
-7.14%Updated