From 29cec40e945b3f4056315bfe9637c979070d8050 Mon Sep 17 00:00:00 2001 From: dpiercey Date: Wed, 23 Oct 2024 18:47:39 -0700 Subject: [PATCH] fix: improve fallback content printint --- .../auto.marko | 8 +------- .../concise.marko | 8 +------- .../html.marko | 8 +------- .../with-parens.marko | 8 +------- .../script-with-type.expected/auto.marko | 8 ++++++++ .../script-with-type.expected/concise.marko | 9 +++++++++ .../script-with-type.expected/html.marko | 8 ++++++++ .../with-parens.marko | 8 ++++++++ src/__tests__/fixtures/script-with-type.marko | 8 ++++++++ src/index.ts | 18 ++++++++++++++---- 10 files changed, 59 insertions(+), 32 deletions(-) create mode 100644 src/__tests__/__snapshots__/script-with-type.expected/auto.marko create mode 100644 src/__tests__/__snapshots__/script-with-type.expected/concise.marko create mode 100644 src/__tests__/__snapshots__/script-with-type.expected/html.marko create mode 100644 src/__tests__/__snapshots__/script-with-type.expected/with-parens.marko create mode 100644 src/__tests__/fixtures/script-with-type.marko diff --git a/src/__tests__/__snapshots__/ignore-embedded-script-errors.expected/auto.marko b/src/__tests__/__snapshots__/ignore-embedded-script-errors.expected/auto.marko index 16e124a..80d9acf 100644 --- a/src/__tests__/__snapshots__/ignore-embedded-script-errors.expected/auto.marko +++ b/src/__tests__/__snapshots__/ignore-embedded-script-errors.expected/auto.marko @@ -1,18 +1,12 @@ -style { - invalid { color blue; } -} +style { invalid { color blue; } } diff --git a/src/__tests__/__snapshots__/ignore-embedded-script-errors.expected/concise.marko b/src/__tests__/__snapshots__/ignore-embedded-script-errors.expected/concise.marko index 8806934..811abcd 100644 --- a/src/__tests__/__snapshots__/ignore-embedded-script-errors.expected/concise.marko +++ b/src/__tests__/__snapshots__/ignore-embedded-script-errors.expected/concise.marko @@ -1,20 +1,14 @@ -style { - invalid { color blue; } -} +style { invalid { color blue; } } style -- - body color: blue; - -- script -- - console->log( 'a' ); - -- diff --git a/src/__tests__/__snapshots__/ignore-embedded-script-errors.expected/html.marko b/src/__tests__/__snapshots__/ignore-embedded-script-errors.expected/html.marko index 16e124a..80d9acf 100644 --- a/src/__tests__/__snapshots__/ignore-embedded-script-errors.expected/html.marko +++ b/src/__tests__/__snapshots__/ignore-embedded-script-errors.expected/html.marko @@ -1,18 +1,12 @@ -style { - invalid { color blue; } -} +style { invalid { color blue; } } diff --git a/src/__tests__/__snapshots__/ignore-embedded-script-errors.expected/with-parens.marko b/src/__tests__/__snapshots__/ignore-embedded-script-errors.expected/with-parens.marko index 16e124a..80d9acf 100644 --- a/src/__tests__/__snapshots__/ignore-embedded-script-errors.expected/with-parens.marko +++ b/src/__tests__/__snapshots__/ignore-embedded-script-errors.expected/with-parens.marko @@ -1,18 +1,12 @@ -style { - invalid { color blue; } -} +style { invalid { color blue; } } diff --git a/src/__tests__/__snapshots__/script-with-type.expected/auto.marko b/src/__tests__/__snapshots__/script-with-type.expected/auto.marko new file mode 100644 index 0000000..92dcfc9 --- /dev/null +++ b/src/__tests__/__snapshots__/script-with-type.expected/auto.marko @@ -0,0 +1,8 @@ + diff --git a/src/__tests__/__snapshots__/script-with-type.expected/concise.marko b/src/__tests__/__snapshots__/script-with-type.expected/concise.marko new file mode 100644 index 0000000..593d4e4 --- /dev/null +++ b/src/__tests__/__snapshots__/script-with-type.expected/concise.marko @@ -0,0 +1,9 @@ +script nonce=$global.cspNonce type="speculationrules" + -- + { + "prerender": [{ + "where": { "href_matches": "/*" }, + "eagerness": "moderate" + }] + } + -- diff --git a/src/__tests__/__snapshots__/script-with-type.expected/html.marko b/src/__tests__/__snapshots__/script-with-type.expected/html.marko new file mode 100644 index 0000000..92dcfc9 --- /dev/null +++ b/src/__tests__/__snapshots__/script-with-type.expected/html.marko @@ -0,0 +1,8 @@ + diff --git a/src/__tests__/__snapshots__/script-with-type.expected/with-parens.marko b/src/__tests__/__snapshots__/script-with-type.expected/with-parens.marko new file mode 100644 index 0000000..92dcfc9 --- /dev/null +++ b/src/__tests__/__snapshots__/script-with-type.expected/with-parens.marko @@ -0,0 +1,8 @@ + diff --git a/src/__tests__/fixtures/script-with-type.marko b/src/__tests__/fixtures/script-with-type.marko new file mode 100644 index 0000000..92dcfc9 --- /dev/null +++ b/src/__tests__/fixtures/script-with-type.marko @@ -0,0 +1,8 @@ + diff --git a/src/index.ts b/src/index.ts index 37087b4..1a87426 100644 --- a/src/index.ts +++ b/src/index.ts @@ -734,7 +734,11 @@ export const printers: Record> = { : b.ifBreak("--", " --", { groupId }), opts.markoSyntax === "html" ? "" : b.line, replaceEmbeddedPlaceholders( - await toDoc(embeddedCode, { parser: scriptParser }), + await toDoc(embeddedCode, { + parser: scriptParser, + }).catch(() => + asLiteralTextContent(embeddedCode.trim()), + ), placeholders, ), opts.markoSyntax === "html" @@ -781,7 +785,12 @@ export const printers: Record> = { "style", !lang || lang === ".css" ? "" : lang, " {", - b.indent([b.line, await toDoc(code, { parser })]), + b.indent([ + b.line, + await toDoc(code, { parser }).catch(() => + asLiteralTextContent(code.trim()), + ), + ]), b.line, "}", ]), @@ -888,14 +897,15 @@ export const printers: Record> = { : b.ifBreak("--", " --", { groupId }), opts.markoSyntax === "html" ? "" : b.line, replaceEmbeddedPlaceholders( - await toDoc(embeddedCode, { parser }), + await toDoc(embeddedCode, { parser }).catch(() => + asLiteralTextContent(embeddedCode.trim()), + ), placeholders, ), opts.markoSyntax === "html" ? "" : b.ifBreak([b.softline, "--"]), ]); - doc.push(b.indent([wrapSep, bodyDoc])); if (opts.markoSyntax === "html") {