Skip to content

Commit

Permalink
feat: pass through invalid embedded code as is
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Oct 11, 2021
1 parent 0b4700c commit 7582c5c
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
style {
invalid { color blue; }
}

<style>
body
color: blue;
</style>
<script>
console->log(
'a'
);
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
style {
invalid { color blue; }
}

style
--
body
color: blue;
--
script
--
console->log(
'a'
);
--
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
style {
invalid { color blue; }
}

<style>
body
color: blue;
</style>
<script>
console->log(
'a'
);
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
style {
invalid { color blue; }
}

<style>
body
color: blue;
</style>
<script>
console->log(
'a'
);
</script>
14 changes: 14 additions & 0 deletions src/__tests__/fixtures/ignore-embedded-script-errors.marko
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
style {
invalid { color blue; }
}

<style>
body
color: blue;
</style>
<script>
console->log(
'a'
);
</script>
75 changes: 36 additions & 39 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import asLiteralTextContent from "./utils/as-literal-text-content";

const defaultFilePath = resolve("index.marko");
const { builders: b } = doc;
const identity = <T extends unknown>(val: T) => val;

export const languages: SupportLanguage[] = [
{
Expand Down Expand Up @@ -587,46 +588,37 @@ export const printers: Record<string, Printer<Node>> = {
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)}`,
Expand All @@ -643,21 +635,26 @@ export const printers: Record<string, Printer<Node>> = {
}

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)];
}
}
},
},
Expand Down

0 comments on commit 7582c5c

Please sign in to comment.