Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support {#key} block #147

Merged
merged 1 commit into from
Sep 26, 2020
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
6 changes: 3 additions & 3 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"rollup-plugin-commonjs": "9.2.0",
"rollup-plugin-node-resolve": "4.0.0",
"rollup-plugin-typescript": "1.0.0",
"svelte": "^3.20.0",
"svelte": "^3.28.0",
"ts-node": "^7.0.1",
"tslib": "^1.9.3",
"typescript": "3.2.4"
Expand Down
16 changes: 14 additions & 2 deletions src/print/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,18 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D

return group(concat(block));
}
case 'KeyBlock': {
const def: Doc[] = [
'{#key ',
printJS(path, print, 'expression'),
'}',
printIndentedWithNewlines(path, print),
];

def.push('{/key}');

return concat([group(concat(def)), breakParent]);
}
case 'ThenBlock':
case 'PendingBlock':
case 'CatchBlock':
Expand Down Expand Up @@ -439,7 +451,7 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D
case 'Comment': {
/**
* If there is no sibling node that starts right after us but the parent indicates
* that there used to be, that means that node was actually an embedded `<style>`
* that there used to be, that means that node was actually an embedded `<style>`
* or `<script>` node that was cut out.
* If so, the comment does not refer to the next line we will see.
* The `embed` function handles printing the comment in the right place.
Expand Down Expand Up @@ -545,7 +557,7 @@ function printChildren(path: FastPath, print: PrintFn): Doc[] {
*/
function outputChildDoc(childDoc?: Doc, fromNode?: Node) {
if (!isPreformat) {
if ((!childDoc || !fromNode || canBreakBefore(fromNode))) {
if (!childDoc || !fromNode || canBreakBefore(fromNode)) {
linebreakPossible();

const lastChild = childDocs[childDocs.length - 1];
Expand Down
7 changes: 7 additions & 0 deletions src/print/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ export interface AwaitBlockNode extends BaseNode {
catch: CatchBlockNode;
}

export interface KeyBlockNode extends BaseNode {
type: 'KeyBlock';
expression: Node;
children: Node[];
}

export interface ThenBlockNode extends BaseNode {
type: 'ThenBlock';
children: Node[];
Expand Down Expand Up @@ -257,6 +263,7 @@ export type Node =
| ElseBlockNode
| EachBlockNode
| AwaitBlockNode
| KeyBlockNode
| ThenBlockNode
| PendingBlockNode
| CatchBlockNode
Expand Down
10 changes: 10 additions & 0 deletions test/printer/samples/key-block.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{#key value}
<p>hello</p>
<p>hello</p>
{/key}
{#key $store}
<p>hello</p>
{/key}
{#key expr.obj}
<p>hello</p>
{/key}