more tidying up

pull/992/head
Rich Harris 8 years ago
parent 9b2a7e17ad
commit e974fdc40b

@ -9,7 +9,7 @@ import flattenReference from '../utils/flattenReference';
import reservedNames from '../utils/reservedNames';
import namespaces from '../utils/namespaces';
import { removeNode, removeObjectKey } from '../utils/removeNode';
import wrapModule from './shared/utils/wrapModule';
import wrapModule from './wrapModule';
import annotateWithScopes, { Scope } from '../utils/annotateWithScopes';
import getName from '../utils/getName';
import clone from '../utils/clone';

@ -10,7 +10,7 @@ import Block from '../dom/Block';
import Attribute from './Attribute';
export default class Component extends Node {
type: 'Component'; // TODO fix this?
type: 'Component';
name: string;
attributes: Attribute[];
children: Node[];
@ -408,10 +408,6 @@ export default class Component extends Node {
block.builders.update.addBlock(updates);
}
}
nearestComponent() {
return this;
}
}
function mungeAttribute(attribute: Node, block: Block): Attribute {

@ -87,7 +87,7 @@ export default class EachBlock extends Node {
if (this.else) {
this.else.block = block.child({
comment: '// TODO', // createDebuggingComment(this.else, generator),
comment: createDebuggingComment(this.else, this.generator),
name: this.generator.getUniqueName(`${this.block.name}_else`),
});
@ -256,9 +256,8 @@ export default class EachBlock extends Node {
block.addVariable(head);
block.addVariable(last);
if (this.children[0] && this.children[0].type === 'Element') {
// TODO or text/tag/raw
this.block.first = this.children[0].var; // TODO this is highly confusing
if (this.children[0].isDomNode()) {
this.block.first = this.children[0].var;
} else {
this.block.first = this.block.getUniqueName('first');
this.block.addElement(

@ -129,11 +129,11 @@ export default class Element extends Node {
}
const slot = this.getStaticAttributeValue('slot');
if (slot && this.isChildOfComponent()) {
if (slot && this.hasAncestor('Component')) {
this.cannotUseInnerHTML();
this.slotted = true;
// TODO validate slots — no nesting, no dynamic names...
const component = this.nearestComponent();
const component = this.findNearest('Component');
component._slots.add(slot);
}
@ -171,7 +171,7 @@ export default class Element extends Node {
const slot = this.attributes.find((attribute: Node) => attribute.name === 'slot');
const initialMountNode = this.slotted ?
`${this.nearestComponent().var}._slotted.${slot.value[0].data}` : // TODO this looks bonkers
`${this.findNearest('Component').var}._slotted.${slot.value[0].data}` : // TODO this looks bonkers
parentNode;
block.addVariable(name);

@ -123,11 +123,6 @@ export default class Node {
// implemented by subclasses
}
isChildOfComponent() {
// TODO remove this method
return this.hasAncestor('Component');
}
isDomNode() {
return this.type === 'Element' || this.type === 'Text' || this.type === 'MustacheTag';
}
@ -138,11 +133,6 @@ export default class Node {
false;
}
nearestComponent() { // TODO remove this method
return this.findNearest('Component');
// if (this.parent) return this.parent.nearestComponent();
}
findNearest(type: string) {
if (this.type === type) return this;
if (this.parent) return this.parent.findNearest(type);

@ -36,7 +36,7 @@ export default function visitElement(
let textareaContents; // awkward special case
const slot = node.getStaticAttributeValue('slot');
if (slot && node.isChildOfComponent()) {
if (slot && node.hasAncestor('Component')) {
const slot = node.attributes.find((attribute: Node) => attribute.name === 'slot');
const slotName = slot.value[0].data;
const appendTarget = generator.appendTargets[generator.appendTargets.length - 1];

@ -8,7 +8,6 @@ export default function visitSlot(
block: Block,
node: Node
) {
// TODO named slots
const name = node.attributes.find((attribute: Node) => attribute.name);
const slotName = name && name.value[0].data || 'default';

@ -1,10 +0,0 @@
import { Node } from '../../../interfaces';
import Generator from '../../Generator';
export default function isChildOfComponent(node: Node, generator: Generator) {
while (node = node.parent) {
if (node.type !== 'Element') continue;
if (node.name === ':Self' || node.name === ':Component' || generator.components.has(node.name)) return true; // TODO extract this out into a helper
if (/-/.test(node.name)) return false;
}
}

@ -1,20 +0,0 @@
import { Node, Visitor } from '../../../interfaces';
export default function walkHtml(html: Node, visitors: Record<string, Visitor>) {
function visit(node: Node) {
const visitor = visitors[node.type];
if (!visitor) throw new Error(`Not implemented: ${node.type}`);
if (visitor.enter) visitor.enter(node);
if (node.children) {
node.children.forEach((child: Node) => {
visit(child);
});
}
if (visitor.leave) visitor.leave(node);
}
visit(html);
}

@ -1,6 +1,6 @@
import deindent from '../../../utils/deindent';
import list from '../../../utils/list';
import { CompileOptions, ModuleFormat, Node } from '../../../interfaces';
import deindent from '../utils/deindent';
import list from '../utils/list';
import { CompileOptions, ModuleFormat, Node } from '../interfaces';
interface Dependency {
name: string;
Loading…
Cancel
Save