prettier-plugin-marko
Advanced tools
+39
-30
@@ -38,7 +38,13 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); | ||
| }({}); | ||
| let CommentType = /* @__PURE__ */ function(CommentType) { | ||
| CommentType[CommentType["line"] = 0] = "line"; | ||
| CommentType[CommentType["block"] = 1] = "block"; | ||
| CommentType[CommentType["html"] = 2] = "html"; | ||
| return CommentType; | ||
| }({}); | ||
| function parse(code, filename = "index.marko") { | ||
| const builder = new Builder(code); | ||
| const parser = (0, htmljs_parser.createParser)(builder); | ||
| const { program } = builder; | ||
| parser.parse(code); | ||
| const program = builder.end(); | ||
| return { | ||
@@ -54,4 +60,4 @@ read: parser.read, | ||
| var Builder = class { | ||
| program; | ||
| #code; | ||
| #program; | ||
| #openTagStart; | ||
@@ -63,6 +69,5 @@ #parentNode; | ||
| this.#code = code; | ||
| this.#program = this.#parentNode = { | ||
| this.program = this.#parentNode = { | ||
| type: NodeType.Program, | ||
| parent: void 0, | ||
| static: [], | ||
| body: [], | ||
@@ -73,5 +78,2 @@ start: 0, | ||
| } | ||
| end() { | ||
| return this.#program; | ||
| } | ||
| onText(range) { | ||
@@ -113,6 +115,15 @@ pushBody(this.#parentNode, { | ||
| onComment(range) { | ||
| let commentType = CommentType.html; | ||
| switch (this.#code.charCodeAt(range.start + 1)) { | ||
| case 47: | ||
| commentType = CommentType.line; | ||
| break; | ||
| case 42: | ||
| commentType = CommentType.block; | ||
| break; | ||
| } | ||
| pushBody(this.#parentNode, { | ||
| type: NodeType.Comment, | ||
| parent: this.#parentNode, | ||
| block: this.#code.charAt(range.start + 1) !== "/", | ||
| commentType, | ||
| value: range.value, | ||
@@ -163,5 +174,5 @@ start: range.start, | ||
| const [{ length }, ext] = styleBlockMatch; | ||
| this.#program.static.push(this.#staticNode = { | ||
| this.program.body.push(this.#staticNode = { | ||
| type: NodeType.Style, | ||
| parent: this.#program, | ||
| parent: this.program, | ||
| ext: ext || void 0, | ||
@@ -182,5 +193,5 @@ value: { | ||
| case "class": | ||
| this.#program.static.push(this.#staticNode = { | ||
| this.program.body.push(this.#staticNode = { | ||
| type: NodeType.Class, | ||
| parent: this.#program, | ||
| parent: this.program, | ||
| start: range.start, | ||
@@ -191,5 +202,5 @@ end: UNFINISHED | ||
| case "export": | ||
| this.#program.static.push(this.#staticNode = { | ||
| this.program.body.push(this.#staticNode = { | ||
| type: NodeType.Export, | ||
| parent: this.#program, | ||
| parent: this.program, | ||
| start: range.start, | ||
@@ -200,5 +211,5 @@ end: UNFINISHED | ||
| case "import": | ||
| this.#program.static.push(this.#staticNode = { | ||
| this.program.body.push(this.#staticNode = { | ||
| type: NodeType.Import, | ||
| parent: this.#program, | ||
| parent: this.program, | ||
| start: range.start, | ||
@@ -211,5 +222,5 @@ end: UNFINISHED | ||
| case "static": | ||
| this.#program.static.push(this.#staticNode = { | ||
| this.program.body.push(this.#staticNode = { | ||
| type: NodeType.Static, | ||
| parent: this.#program, | ||
| parent: this.program, | ||
| target: nameText, | ||
@@ -729,3 +740,3 @@ start: range.start, | ||
| [NodeType.AttrTag]: tagVisitorKeys, | ||
| [NodeType.Program]: ["static", "body"], | ||
| [NodeType.Program]: ["body"], | ||
| [NodeType.AttrNamed]: ["args", "value"] | ||
@@ -826,3 +837,3 @@ }; | ||
| const code = read(node, opts); | ||
| if (node.block) { | ||
| if (node.commentType !== CommentType.line) { | ||
| if (code.includes("\n")) { | ||
@@ -852,7 +863,4 @@ const lines = code.split("\n"); | ||
| const body = printBody(path, opts, print); | ||
| const lastStatic = path.node.static.length - (body ? 0 : 1); | ||
| let programDoc = path.map((child, i) => i !== lastStatic && hasExplicitLine(child, opts) ? [child.call(print), b.hardline] : child.call(print), "static"); | ||
| if (body) if (body.inline) programDoc.push(wrapConciseText(body.content)); | ||
| else programDoc = [...programDoc, ...body.content]; | ||
| return [b.join(b.hardline, programDoc), b.hardline]; | ||
| if (!body) return [b.hardline]; | ||
| return [body.inline ? wrapConciseText(body.content) : b.join(b.hardline, body.content), b.hardline]; | ||
| }, | ||
@@ -1086,6 +1094,8 @@ [NodeType.Text]: (path, opts) => { | ||
| if (preserve) { | ||
| let inlineChild = false; | ||
| path.each((child) => { | ||
| const childDoc = child.call(print); | ||
| inlineChild = isInline(child.node) || inlineChild && child.node.type === NodeType.Comment && child.node.commentType !== CommentType.line; | ||
| content ||= []; | ||
| if (isInline(child.node)) { | ||
| if (inlineChild) { | ||
| if (!inline) { | ||
@@ -1119,2 +1129,3 @@ inline = []; | ||
| } else { | ||
| let inlineChild = false; | ||
| let isInlineTag = false; | ||
@@ -1124,8 +1135,8 @@ let isExplicitLine = false; | ||
| const wasInlineTag = isInlineTag; | ||
| const inlineChild = isInline(child.node); | ||
| let childDoc = child.call(print); | ||
| if (child.node.type === NodeType.Text && typeof childDoc === "string") childDoc = trimText(childDoc, child); | ||
| if (!childDoc) return; | ||
| content ||= []; | ||
| isInlineTag = false; | ||
| content ||= []; | ||
| inlineChild = isInline(child.node) || inlineChild && child.node.type === NodeType.Comment && child.node.commentType !== CommentType.line; | ||
| if (isExplicitLine) { | ||
@@ -1279,3 +1290,2 @@ const last = content.length - 1; | ||
| case NodeType.Placeholder: return true; | ||
| case NodeType.Comment: return node.block; | ||
| default: return false; | ||
@@ -1288,3 +1298,2 @@ } | ||
| case NodeType.Placeholder: return true; | ||
| case NodeType.Comment: return node.block; | ||
| case NodeType.Tag: return !!node.nameText && /^(?:a(?:bbr|cronym)?|b(?:do|ig|r)?|cite|code|dfn|em|i(?:mg)?|kbd|label|map|object|output|q|samp|small|span|strong|sub|sup|time|tt|var)$/.test(node.nameText); | ||
@@ -1291,0 +1300,0 @@ default: return false; |
+39
-30
@@ -37,7 +37,13 @@ import { doc } from "prettier"; | ||
| }({}); | ||
| let CommentType = /* @__PURE__ */ function(CommentType) { | ||
| CommentType[CommentType["line"] = 0] = "line"; | ||
| CommentType[CommentType["block"] = 1] = "block"; | ||
| CommentType[CommentType["html"] = 2] = "html"; | ||
| return CommentType; | ||
| }({}); | ||
| function parse(code, filename = "index.marko") { | ||
| const builder = new Builder(code); | ||
| const parser = createParser(builder); | ||
| const { program } = builder; | ||
| parser.parse(code); | ||
| const program = builder.end(); | ||
| return { | ||
@@ -53,4 +59,4 @@ read: parser.read, | ||
| var Builder = class { | ||
| program; | ||
| #code; | ||
| #program; | ||
| #openTagStart; | ||
@@ -62,6 +68,5 @@ #parentNode; | ||
| this.#code = code; | ||
| this.#program = this.#parentNode = { | ||
| this.program = this.#parentNode = { | ||
| type: NodeType.Program, | ||
| parent: void 0, | ||
| static: [], | ||
| body: [], | ||
@@ -72,5 +77,2 @@ start: 0, | ||
| } | ||
| end() { | ||
| return this.#program; | ||
| } | ||
| onText(range) { | ||
@@ -112,6 +114,15 @@ pushBody(this.#parentNode, { | ||
| onComment(range) { | ||
| let commentType = CommentType.html; | ||
| switch (this.#code.charCodeAt(range.start + 1)) { | ||
| case 47: | ||
| commentType = CommentType.line; | ||
| break; | ||
| case 42: | ||
| commentType = CommentType.block; | ||
| break; | ||
| } | ||
| pushBody(this.#parentNode, { | ||
| type: NodeType.Comment, | ||
| parent: this.#parentNode, | ||
| block: this.#code.charAt(range.start + 1) !== "/", | ||
| commentType, | ||
| value: range.value, | ||
@@ -162,5 +173,5 @@ start: range.start, | ||
| const [{ length }, ext] = styleBlockMatch; | ||
| this.#program.static.push(this.#staticNode = { | ||
| this.program.body.push(this.#staticNode = { | ||
| type: NodeType.Style, | ||
| parent: this.#program, | ||
| parent: this.program, | ||
| ext: ext || void 0, | ||
@@ -181,5 +192,5 @@ value: { | ||
| case "class": | ||
| this.#program.static.push(this.#staticNode = { | ||
| this.program.body.push(this.#staticNode = { | ||
| type: NodeType.Class, | ||
| parent: this.#program, | ||
| parent: this.program, | ||
| start: range.start, | ||
@@ -190,5 +201,5 @@ end: UNFINISHED | ||
| case "export": | ||
| this.#program.static.push(this.#staticNode = { | ||
| this.program.body.push(this.#staticNode = { | ||
| type: NodeType.Export, | ||
| parent: this.#program, | ||
| parent: this.program, | ||
| start: range.start, | ||
@@ -199,5 +210,5 @@ end: UNFINISHED | ||
| case "import": | ||
| this.#program.static.push(this.#staticNode = { | ||
| this.program.body.push(this.#staticNode = { | ||
| type: NodeType.Import, | ||
| parent: this.#program, | ||
| parent: this.program, | ||
| start: range.start, | ||
@@ -210,5 +221,5 @@ end: UNFINISHED | ||
| case "static": | ||
| this.#program.static.push(this.#staticNode = { | ||
| this.program.body.push(this.#staticNode = { | ||
| type: NodeType.Static, | ||
| parent: this.#program, | ||
| parent: this.program, | ||
| target: nameText, | ||
@@ -728,3 +739,3 @@ start: range.start, | ||
| [NodeType.AttrTag]: tagVisitorKeys, | ||
| [NodeType.Program]: ["static", "body"], | ||
| [NodeType.Program]: ["body"], | ||
| [NodeType.AttrNamed]: ["args", "value"] | ||
@@ -825,3 +836,3 @@ }; | ||
| const code = read(node, opts); | ||
| if (node.block) { | ||
| if (node.commentType !== CommentType.line) { | ||
| if (code.includes("\n")) { | ||
@@ -851,7 +862,4 @@ const lines = code.split("\n"); | ||
| const body = printBody(path, opts, print); | ||
| const lastStatic = path.node.static.length - (body ? 0 : 1); | ||
| let programDoc = path.map((child, i) => i !== lastStatic && hasExplicitLine(child, opts) ? [child.call(print), b.hardline] : child.call(print), "static"); | ||
| if (body) if (body.inline) programDoc.push(wrapConciseText(body.content)); | ||
| else programDoc = [...programDoc, ...body.content]; | ||
| return [b.join(b.hardline, programDoc), b.hardline]; | ||
| if (!body) return [b.hardline]; | ||
| return [body.inline ? wrapConciseText(body.content) : b.join(b.hardline, body.content), b.hardline]; | ||
| }, | ||
@@ -1085,6 +1093,8 @@ [NodeType.Text]: (path, opts) => { | ||
| if (preserve) { | ||
| let inlineChild = false; | ||
| path.each((child) => { | ||
| const childDoc = child.call(print); | ||
| inlineChild = isInline(child.node) || inlineChild && child.node.type === NodeType.Comment && child.node.commentType !== CommentType.line; | ||
| content ||= []; | ||
| if (isInline(child.node)) { | ||
| if (inlineChild) { | ||
| if (!inline) { | ||
@@ -1118,2 +1128,3 @@ inline = []; | ||
| } else { | ||
| let inlineChild = false; | ||
| let isInlineTag = false; | ||
@@ -1123,8 +1134,8 @@ let isExplicitLine = false; | ||
| const wasInlineTag = isInlineTag; | ||
| const inlineChild = isInline(child.node); | ||
| let childDoc = child.call(print); | ||
| if (child.node.type === NodeType.Text && typeof childDoc === "string") childDoc = trimText(childDoc, child); | ||
| if (!childDoc) return; | ||
| content ||= []; | ||
| isInlineTag = false; | ||
| content ||= []; | ||
| inlineChild = isInline(child.node) || inlineChild && child.node.type === NodeType.Comment && child.node.commentType !== CommentType.line; | ||
| if (isExplicitLine) { | ||
@@ -1278,3 +1289,2 @@ const last = content.length - 1; | ||
| case NodeType.Placeholder: return true; | ||
| case NodeType.Comment: return node.block; | ||
| default: return false; | ||
@@ -1287,3 +1297,2 @@ } | ||
| case NodeType.Placeholder: return true; | ||
| case NodeType.Comment: return node.block; | ||
| case NodeType.Tag: return !!node.nameText && /^(?:a(?:bbr|cronym)?|b(?:do|ig|r)?|cite|code|dfn|em|i(?:mg)?|kbd|label|map|object|output|q|samp|small|span|strong|sub|sup|time|tt|var)$/.test(node.nameText); | ||
@@ -1290,0 +1299,0 @@ default: return false; |
+13
-3
@@ -38,2 +38,7 @@ import { type Range, type Ranges, TagType } from "htmljs-parser"; | ||
| } | ||
| export declare enum CommentType { | ||
| line = 0, | ||
| block = 1, | ||
| html = 2 | ||
| } | ||
| export declare namespace Node { | ||
@@ -53,4 +58,3 @@ type AnyNode = Program | Tag | OpenTagName | ShorthandId | ShorthandClassName | TagTypeArgs | TagTypeParams | TagVar | TagArgs | TagParams | AttrNamed | AttrName | AttrArgs | AttrValue | AttrMethod | AttrSpread | AttrTag | Text | CDATA | Doctype | Declaration | Comment | Placeholder | Scriptlet | Import | Export | Class | Style | Static; | ||
| parent: undefined; | ||
| static: StaticNode[]; | ||
| body: ChildNode[]; | ||
| body: (ChildNode | StaticNode)[]; | ||
| } | ||
@@ -152,3 +156,3 @@ interface Tag extends Range { | ||
| parent: ParentNode; | ||
| block: boolean; | ||
| commentType: CommentType; | ||
| } | ||
@@ -229,1 +233,7 @@ interface Placeholder extends Ranges.Value { | ||
| }; | ||
| /** | ||
| * Used to check if a node should be ignored as the parent of an attribute tag. | ||
| * When control flow is the parent of an attribute tag, we add the attribute tag to | ||
| * the closest non control flow ancestor attrs instead. | ||
| */ | ||
| export declare function isControlFlowTag(node: Node.Tag): node is Node.ControlFlowTag; |
+1
-1
| { | ||
| "name": "prettier-plugin-marko", | ||
| "version": "4.0.7", | ||
| "version": "4.0.8", | ||
| "description": "A prettier plugin for parsing and printing Marko files", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
96595
0.98%2947
0.96%