Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions packages/render/src/shared/utils/pretty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import * as html from 'prettier/plugins/html';
import { format } from 'prettier/standalone';

interface HtmlNode {
type: 'element' | 'text' | 'ieConditionalComment';
type?: 'element' | 'text' | 'ieConditionalComment';
kind?: 'element' | 'text' | 'ieConditionalComment' | 'root';
name?: string;
sourceSpan: {
start: { file: unknown[]; offset: number; line: number; col: number };
Expand All @@ -14,6 +15,18 @@ interface HtmlNode {
parent?: HtmlNode;
}

function getHtmlNode(path: {
node?: HtmlNode;
stack?: Array<Record<string, unknown>>;
}) {
const topNode = path.node;
if (topNode) {
return topNode;
}

return path.stack?.[path.stack.length - 1] as HtmlNode;
}

function recursivelyMapDoc(
doc: builders.Doc,
callback: (innerDoc: string | builders.DocCommand) => builders.Doc,
Expand All @@ -23,6 +36,10 @@ function recursivelyMapDoc(
}

if (typeof doc === 'object') {
if (doc.type === 'line') {
return callback(doc.soft ? '' : ' ');
}

if (doc.type === 'group') {
return {
...doc,
Expand Down Expand Up @@ -55,6 +72,15 @@ function recursivelyMapDoc(
flatContents: recursivelyMapDoc(doc.flatContents, callback),
};
}

const nextDoc = { ...doc } as Record<string, unknown>;
for (const [key, value] of Object.entries(nextDoc)) {
if (value && typeof value === 'object') {
nextDoc[key] = recursivelyMapDoc(value as builders.Doc, callback);
}
}

return nextDoc as builders.Doc;
}

return callback(doc);
Expand All @@ -64,11 +90,16 @@ const modifiedHtml = { ...html } as Plugin;
if (modifiedHtml.printers) {
const previousPrint = modifiedHtml.printers.html.print;
modifiedHtml.printers.html.print = (path, options, print, args) => {
const node = path.getNode() as HtmlNode;
const node = getHtmlNode(
path as Parameters<Plugin['printers']['html']['print']>[0],
);

const rawPrintingResult = previousPrint(path, options, print, args);

if (node.type === 'ieConditionalComment') {
if (
node?.type === 'ieConditionalComment' ||
node?.kind === 'ieConditionalComment'
) {
const printingResult = recursivelyMapDoc(rawPrintingResult, (doc) => {
if (typeof doc === 'object' && doc.type === 'line') {
return doc.soft ? '' : ' ';
Expand Down
5 changes: 2 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading