Skip to content

[WIP] fix some TODOs #1193

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

Merged
merged 6 commits into from
Mar 8, 2018
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
10 changes: 2 additions & 8 deletions src/generators/Generator.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import MagicString, { Bundle } from 'magic-string';
import isReference from 'is-reference';
import { walk, childKeys } from 'estree-walker';
import { getLocator } from 'locate-character';
import deindent from '../utils/deindent';
import CodeBuilder from '../utils/CodeBuilder';
import getCodeFrame from '../utils/getCodeFrame';
import isReference from '../utils/isReference';
import flattenReference from '../utils/flattenReference';
import reservedNames from '../utils/reservedNames';
import namespaces from '../utils/namespaces';
Expand Down Expand Up @@ -168,7 +168,7 @@ export default class Generator {
if (options.customElement === true) {
this.customElement = {
tag: this.tag,
props: this.props // TODO autofill this in
props: this.props
}
} else {
this.customElement = options.customElement;
Expand Down Expand Up @@ -752,12 +752,6 @@ export default class Generator {
if (node.type === 'Element' && (node.name === ':Component' || node.name === ':Self' || generator.components.has(node.name))) {
node.type = 'Component';
Object.setPrototypeOf(node, nodes.Component.prototype);
} else if (node.name === ':Window') { // TODO do this in parse?
node.type = 'Window';
Object.setPrototypeOf(node, nodes.Window.prototype);
} else if (node.name === ':Head') { // TODO do this in parse?
node.type = 'Head';
Object.setPrototypeOf(node, nodes.Head.prototype);
} else if (node.type === 'Element' && node.name === 'title' && parentIsHead(parent)) { // TODO do this in parse?
node.type = 'Title';
Object.setPrototypeOf(node, nodes.Title.prototype);
Expand Down
9 changes: 3 additions & 6 deletions src/generators/dom/Block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,22 +312,19 @@ export default class Block {
if (this.hasOutroMethod) {
if (hasOutros) {
properties.addBlock(deindent`
o: function outro(${this.alias('outrocallback')}) {
o: function outro(#outrocallback) {
if (${outroing}) return;
${outroing} = true;
${hasIntros && `${introing} = false;`}

var ${this.alias('outros')} = ${this.outros};
var #outros = ${this.outros};

${this.builders.outro}
},
`);
} else {
// TODO should this be a helper?
properties.addBlock(deindent`
o: function outro(outrocallback) {
outrocallback();
},
o: @run,
`);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/generators/dom/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import MagicString from 'magic-string';
import isReference from 'is-reference';
import { parseExpressionAt } from 'acorn';
import annotateWithScopes from '../../utils/annotateWithScopes';
import isReference from '../../utils/isReference';
import { walk } from 'estree-walker';
import deindent from '../../utils/deindent';
import { stringify, escape } from '../../utils/stringify';
Expand Down
34 changes: 5 additions & 29 deletions src/generators/nodes/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ export default class Component extends Node {
${name} = new ${switch_vars.value}(${switch_vars.props}(state));
${name}._fragment.c();

${this.children.map(child => remount(generator, child, name))}
${this.children.map(child => child.remount(name))}
${name}._mount(${updateMountNode}, ${anchor});

${eventHandlers.map(handler => deindent`
Expand Down Expand Up @@ -398,6 +398,10 @@ export default class Component extends Node {
`);
}
}

remount(name: string) {
return `${this.var}._mount(${name}._slotted${this.generator.legacy ? `["default"]` : `.default`}, null);`;
}
}

function mungeAttribute(attribute: Node, block: Block): Attribute {
Expand Down Expand Up @@ -547,32 +551,4 @@ function isComputed(node: Node) {
}

return false;
}

function remount(generator: DomGenerator, node: Node, name: string) {
// TODO make this a method of the nodes

if (node.type === 'Component') {
return `${node.var}._mount(${name}._slotted${generator.legacy ? `["default"]` : `.default`}, null);`;
}

if (node.type === 'Element') {
const slot = node.attributes.find(attribute => attribute.name === 'slot');
if (slot) {
return `@appendNode(${node.var}, ${name}._slotted.${node.getStaticAttributeValue('slot')});`;
}

return `@appendNode(${node.var}, ${name}._slotted${generator.legacy ? `["default"]` : `.default`});`;
}

if (node.type === 'Text' || node.type === 'MustacheTag' || node.type === 'RawMustacheTag') {
return `@appendNode(${node.var}, ${name}._slotted${generator.legacy ? `["default"]` : `.default`});`;
}

if (node.type === 'EachBlock') {
// TODO consider keyed blocks
return `for (var #i = 0; #i < ${node.iterations}.length; #i += 1) ${node.iterations}[#i].m(${name}._slotted${generator.legacy ? `["default"]` : `.default`}, null);`;
}

return `${node.var}.m(${name}._slotted${generator.legacy ? `["default"]` : `.default`}, null);`;
}
5 changes: 5 additions & 0 deletions src/generators/nodes/EachBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -594,4 +594,9 @@ export default class EachBlock extends Node {

block.builders.destroy.addBlock(`@destroyEach(${iterations});`);
}

remount(name: string) {
// TODO consider keyed blocks
return `for (var #i = 0; #i < ${this.iterations}.length; #i += 1) ${this.iterations}[#i].m(${name}._slotted${this.generator.legacy ? `["default"]` : `.default`}, null);`;
}
}
9 changes: 9 additions & 0 deletions src/generators/nodes/Element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,15 @@ export default class Element extends Node {
isMediaNode() {
return this.name === 'audio' || this.name === 'video';
}

remount(name: string) {
const slot = this.attributes.find(attribute => attribute.name === 'slot');
if (slot) {
return `@appendNode(${this.var}, ${name}._slotted.${this.getStaticAttributeValue('slot')});`;
}

return `@appendNode(${this.var}, ${name}._slotted${this.generator.legacy ? `["default"]` : `.default`});`;
}
}

function getRenderStatement(
Expand Down
Loading