From 7582c5cbce38af9575db1d50fa452e1a3a45c0f9 Mon Sep 17 00:00:00 2001 From: Dylan Piercey Date: Mon, 11 Oct 2021 16:12:13 -0700 Subject: [PATCH] feat: pass through invalid embedded code as is --- .../auto.marko | 20 +++++ .../concise.marko | 22 ++++++ .../html.marko | 20 +++++ .../with-parens.marko | 20 +++++ .../ignore-embedded-script-errors.marko | 14 ++++ src/index.ts | 75 +++++++++---------- 6 files changed, 132 insertions(+), 39 deletions(-) create mode 100644 src/__tests__/__snapshots__/ignore-embedded-script-errors.expected/auto.marko create mode 100644 src/__tests__/__snapshots__/ignore-embedded-script-errors.expected/concise.marko create mode 100644 src/__tests__/__snapshots__/ignore-embedded-script-errors.expected/html.marko create mode 100644 src/__tests__/__snapshots__/ignore-embedded-script-errors.expected/with-parens.marko create mode 100644 src/__tests__/fixtures/ignore-embedded-script-errors.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 new file mode 100644 index 0000000..6a7574e --- /dev/null +++ b/src/__tests__/__snapshots__/ignore-embedded-script-errors.expected/auto.marko @@ -0,0 +1,20 @@ +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 new file mode 100644 index 0000000..11b7bef --- /dev/null +++ b/src/__tests__/__snapshots__/ignore-embedded-script-errors.expected/concise.marko @@ -0,0 +1,22 @@ +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 new file mode 100644 index 0000000..6a7574e --- /dev/null +++ b/src/__tests__/__snapshots__/ignore-embedded-script-errors.expected/html.marko @@ -0,0 +1,20 @@ +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 new file mode 100644 index 0000000..6a7574e --- /dev/null +++ b/src/__tests__/__snapshots__/ignore-embedded-script-errors.expected/with-parens.marko @@ -0,0 +1,20 @@ +style { + + invalid { color blue; } + +} + + + + diff --git a/src/__tests__/fixtures/ignore-embedded-script-errors.marko b/src/__tests__/fixtures/ignore-embedded-script-errors.marko new file mode 100644 index 0000000..5e85742 --- /dev/null +++ b/src/__tests__/fixtures/ignore-embedded-script-errors.marko @@ -0,0 +1,14 @@ +style { + invalid { color blue; } +} + + + + diff --git a/src/index.ts b/src/index.ts index 5072741..3760092 100644 --- a/src/index.ts +++ b/src/index.ts @@ -30,6 +30,7 @@ import asLiteralTextContent from "./utils/as-literal-text-content"; const defaultFilePath = resolve("index.marko"); const { builders: b } = doc; +const identity = (val: T) => val; export const languages: SupportLanguage[] = [ { @@ -587,46 +588,37 @@ export const printers: Record> = { case "_MarkoEmbed": switch (node.mode) { case "var": { - const doc = (toDoc as any)( + return tryPrintEmbed( `var ${node.code}=_`, - { parser: opts.markoScriptParser }, - { stripTrailingHardline: true } + opts.markoScriptParser, + (doc: any) => doc[0].contents[1].contents[0] ); - - return doc[0].contents[1].contents[0]; } case "params": { - const doc = (toDoc as any)( + return tryPrintEmbed( `(${node.code})=>_`, - { parser: "__js_expression" }, - { stripTrailingHardline: true } - ); - - const { contents } = doc.contents[0]; - if (Array.isArray(contents) && contents[0] === "(") { - return contents.slice(1, -1); - } + "__js_expression", + (doc: any) => { + const { contents } = doc.contents[0]; + if (Array.isArray(contents) && contents[0] === "(") { + return contents.slice(1, -1); + } - return contents; + return contents; + } + ); } case "script": - return (toDoc as any)( - node.code, - { parser: opts.markoScriptParser }, - { stripTrailingHardline: true } - ); + return tryPrintEmbed(node.code, opts.markoScriptParser); default: { if (!node.mode.startsWith("style.")) { - throw new Error(`Invalid Marko Embed mode: ${node.mode}`); + return [b.trim, asLiteralTextContent(node.code)]; } - return (toDoc as any)( - node.code, - { parser: node.mode.slice("style.".length) }, - { stripTrailingHardline: true } - ); + return tryPrintEmbed(node.code, node.mode.slice("style.".length)); } } + case "MarkoClass": return (toDoc as any)( `class ${getOriginalCode(opts, node.body)}`, @@ -643,21 +635,26 @@ export const printers: Record> = { } if (t.isStatement(node)) { - return withLineIfNeeded( - node, - opts, - (toDoc as any)( - getOriginalCode(opts, node), - { parser: opts.markoScriptParser }, - { stripTrailingHardline: true } - ) - ); - } else { - return (toDoc as any)( + return tryPrintEmbed( getOriginalCode(opts, node), - { parser: "__js_expression" }, - { stripTrailingHardline: true } + opts.markoScriptParser ); + } else { + return tryPrintEmbed(getOriginalCode(opts, node), "__js_expression"); + } + + function tryPrintEmbed( + code: string, + parser: string, + normalize: (doc: Doc) => Doc = identity + ) { + try { + return normalize( + (toDoc as any)(code, { parser }, { stripTrailingHardline: true }) + ); + } catch { + return [b.trim, asLiteralTextContent(code)]; + } } }, },