Merge pull request #2608 from thollander/feat/abstract-block-class

Create a new abstraction level to handle `Block`
pull/2675/head
Rich Harris 5 years ago committed by GitHub
commit 05428252bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,12 +1,9 @@
import Node from './shared/Node';
import Block from '../render-dom/Block';
import map_children from './shared/map_children';
import TemplateScope from './shared/TemplateScope';
import AbstractBlock from './shared/AbstractBlock';
export default class CatchBlock extends Node {
block: Block;
export default class CatchBlock extends AbstractBlock {
scope: TemplateScope;
children: Node[];
constructor(component, parent, scope, info) {
super(component, parent, scope, info);

@ -1,9 +1,9 @@
import Node from './shared/Node';
import ElseBlock from './ElseBlock';
import Block from '../render-dom/Block';
import Expression from './shared/Expression';
import map_children from './shared/map_children';
import TemplateScope from './shared/TemplateScope';
import AbstractBlock from './shared/AbstractBlock';
import { Node as INode } from '../../interfaces';
function unpack_destructuring(contexts: Array<{ name: string, tail: string }>, node: INode, tail: string) {
@ -25,10 +25,9 @@ function unpack_destructuring(contexts: Array<{ name: string, tail: string }>, n
}
}
export default class EachBlock extends Node {
export default class EachBlock extends AbstractBlock {
type: 'EachBlock';
block: Block;
expression: Expression;
context_node: Node;
@ -41,7 +40,6 @@ export default class EachBlock extends Node {
has_animation: boolean;
has_binding = false;
children: Node[];
else?: ElseBlock;
constructor(component, parent, scope, info) {
@ -85,7 +83,7 @@ export default class EachBlock extends Node {
}
}
this.warn_if_empty_block(); // TODO would be better if EachBlock, IfBlock etc extended an abstract Block class
this.warn_if_empty_block();
this.else = info.else
? new ElseBlock(component, this, this.scope, info.else)

@ -1,11 +1,8 @@
import Node from './shared/Node';
import Block from '../render-dom/Block';
import map_children from './shared/map_children';
import AbstractBlock from './shared/AbstractBlock';
export default class ElseBlock extends Node {
export default class ElseBlock extends AbstractBlock {
type: 'ElseBlock';
children: Node[];
block: Block;
constructor(component, parent, scope, info) {
super(component, parent, scope, info);
@ -13,4 +10,4 @@ export default class ElseBlock extends Node {
this.warn_if_empty_block();
}
}
}

@ -1,17 +1,13 @@
import Node from './shared/Node';
import ElseBlock from './ElseBlock';
import Block from '../render-dom/Block';
import Expression from './shared/Expression';
import map_children from './shared/map_children';
import AbstractBlock from './shared/AbstractBlock';
export default class IfBlock extends Node {
export default class IfBlock extends AbstractBlock {
type: 'IfBlock';
expression: Expression;
children: any[];
else: ElseBlock;
block: Block;
constructor(component, parent, scope, info) {
super(component, parent, scope, info);
@ -24,4 +20,4 @@ export default class IfBlock extends Node {
this.warn_if_empty_block();
}
}
}

@ -1,10 +1,7 @@
import Node from './shared/Node';
import Block from '../render-dom/Block';
import map_children from './shared/map_children';
import AbstractBlock from './shared/AbstractBlock';
export default class PendingBlock extends Node {
block: Block;
children: Node[];
export default class PendingBlock extends AbstractBlock {
constructor(component, parent, scope, info) {
super(component, parent, scope, info);

@ -1,12 +1,9 @@
import Node from './shared/Node';
import Block from '../render-dom/Block';
import map_children from './shared/map_children';
import TemplateScope from './shared/TemplateScope';
import AbstractBlock from './shared/AbstractBlock';
export default class ThenBlock extends Node {
block: Block;
export default class ThenBlock extends AbstractBlock {
scope: TemplateScope;
children: Node[];
constructor(component, parent, scope, info) {
super(component, parent, scope, info);

@ -0,0 +1,25 @@
import Block from '../../render-dom/Block';
import Component from './../../Component';
import Node from './Node';
export default class AbstractBlock extends Node {
block: Block;
children: Node[];
constructor(component: Component, parent, scope, info: any) {
super(component, parent, scope, info);
}
warn_if_empty_block() {
if (!this.children || this.children.length > 1) return;
const child = this.children[0];
if (!child || (child.type === 'Text' && !/[^ \r\n\f\v\t]/.test(child.data))) {
this.component.warn(this, {
code: 'empty-block',
message: 'Empty block'
});
}
}
}

@ -1,3 +1,4 @@
import Attribute from './../Attribute';
import Component from './../../Component';
export default class Node {
@ -12,6 +13,7 @@ export default class Node {
can_use_innerhtml: boolean;
var: string;
attributes: Attribute[];
constructor(component: Component, parent, scope, info: any) {
this.start = info.start;
@ -64,18 +66,4 @@ export default class Node {
this.parent.type === type || this.parent.has_ancestor(type) :
false;
}
warn_if_empty_block() {
if (!/Block$/.test(this.type) || !this.children) return;
if (this.children.length > 1) return;
const child = this.children[0];
if (!child || (child.type === 'Text' && !/[^ \r\n\f\v\t]/.test(child.data))) {
this.component.warn(this, {
code: 'empty-block',
message: 'Empty block'
});
}
}
}

Loading…
Cancel
Save