Skip to content
Closed
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
32 changes: 32 additions & 0 deletions src/compiler/compile/nodes/CaseBlock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import AbstractBlock from './shared/AbstractBlock';
import Component from '../Component';
import TemplateScope from './shared/TemplateScope';
import { TemplateNode } from '../../interfaces';
import ConstTag from './ConstTag';
import get_const_tags from './shared/get_const_tags';
import Expression from './shared/Expression';
import SwitchBlock from './SwitchBlock';

export default class CaseBlock extends AbstractBlock {
type: 'CaseBlock';
is_default: boolean;
test?: Expression;
scope: TemplateScope;
const_tags: ConstTag[];

constructor(component: Component, parent: SwitchBlock, scope: TemplateScope, info: TemplateNode) {
super(component, parent, scope, info);
this.scope = scope.child();

this.test = info.test;
this.is_default = info.isdefault ?? false;

if (!this.is_default) {
this.test = new Expression(component, this, this.scope, info.test);
}

([this.const_tags, this.children] = get_const_tags(info.children, component, this, this));

this.warn_if_empty_block();
}
}
27 changes: 27 additions & 0 deletions src/compiler/compile/nodes/SwitchBlock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import AbstractBlock from './shared/AbstractBlock';
import Component from '../Component';
import TemplateScope from './shared/TemplateScope';
import { TemplateNode } from '../../interfaces';
import Node from './shared/Node';
import CaseBlock from './CaseBlock';
import Expression from './shared/Expression';

export default class SwitchBlock extends AbstractBlock {
type: 'SwitchBlock';
cases: CaseBlock[];
discriminant: Expression;
scope: TemplateScope;

constructor(component: Component, parent: Node, scope: TemplateScope, info: TemplateNode) {
super(component, parent, scope, info);
this.scope = scope.child();

this.discriminant = new Expression(component, this, this.scope, info.disccriminant);

this.cases = info.cases?.length
? info.cases.map(c => new CaseBlock(component, this, scope, c))
: [];

this.warn_if_empty_block();
}
}
8 changes: 7 additions & 1 deletion src/compiler/compile/nodes/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Attribute from './Attribute';
import AwaitBlock from './AwaitBlock';
import Binding from './Binding';
import Body from './Body';
import CaseBlock from './CaseBlock';
import CatchBlock from './CatchBlock';
import Class from './Class';
import StyleDirective from './StyleDirective';
Expand All @@ -28,6 +29,7 @@ import PendingBlock from './PendingBlock';
import RawMustacheTag from './RawMustacheTag';
import Slot from './Slot';
import SlotTemplate from './SlotTemplate';
import SwitchBlock from './SwitchBlock';
import Text from './Text';
import ThenBlock from './ThenBlock';
import Title from './Title';
Expand All @@ -43,6 +45,7 @@ export type INode = Action
| Binding
| Body
| CatchBlock
| CaseBlock
| Class
| Comment
| ConstTag
Expand All @@ -64,6 +67,7 @@ export type INode = Action
| Slot
| SlotTemplate
| StyleDirective
| SwitchBlock
| Tag
| Text
| ThenBlock
Expand All @@ -73,9 +77,11 @@ export type INode = Action

export type INodeAllowConstTag =
| IfBlock
| CaseBlock
| ElseBlock
| EachBlock
| CatchBlock
| ThenBlock
| InlineComponent
| SlotTemplate;
| SlotTemplate
| SwitchBlock;
1 change: 1 addition & 0 deletions src/compiler/compile/nodes/shared/Expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ export default class Expression {
template_scope,
owner
} = this;

let scope = this.scope;

let function_expression;
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/compile/nodes/shared/map_children.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import Title from '../Title';
import Window from '../Window';
import { TemplateNode } from '../../../interfaces';
import { push_array } from '../../../utils/push_array';
import SwitchBlock from '../SwitchBlock';
import CaseBlock from '../CaseBlock';

export type Children = ReturnType<typeof map_children>;

Expand All @@ -27,6 +29,7 @@ function get_constructor(type) {
case 'AwaitBlock': return AwaitBlock;
case 'Body': return Body;
case 'Comment': return Comment;
case 'CaseBlock': return CaseBlock;
case 'ConstTag': return ConstTag;
case 'EachBlock': return EachBlock;
case 'Element': return Element;
Expand All @@ -40,6 +43,7 @@ function get_constructor(type) {
case 'DebugTag': return DebugTag;
case 'Slot': return Slot;
case 'SlotTemplate': return SlotTemplate;
case 'SwitchBlock': return SwitchBlock;
case 'Text': return Text;
case 'Title': return Title;
case 'Window': return Window;
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/compile/render_dom/wrappers/Fragment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import EachBlock from './EachBlock';
import Element from './Element/index';
import Head from './Head';
import IfBlock from './IfBlock';
import SwitchBlock from './SwitchBlock';
import KeyBlock from './KeyBlock';
import InlineComponent from './InlineComponent/index';
import MustacheTag from './MustacheTag';
Expand All @@ -32,6 +33,7 @@ const wrappers = {
Head,
IfBlock,
InlineComponent,
SwitchBlock,
KeyBlock,
MustacheTag,
Options: null,
Expand Down
Loading