Skip to content

Commit

Permalink
remove some unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Sep 23, 2018
1 parent 7e75e2d commit 2600acf
Show file tree
Hide file tree
Showing 6 changed files with 1 addition and 249 deletions.
3 changes: 0 additions & 3 deletions src/compile/nodes/Binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ import Node from './shared/Node';
import getObject from '../../utils/getObject';
import Expression from './shared/Expression';

// TODO a lot of this element-specific stuff should live in Element —
// Binding should ideally be agnostic between Element and InlineComponent

export default class Binding extends Node {
name: string;
value: Expression;
Expand Down
20 changes: 0 additions & 20 deletions src/compile/nodes/Head.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,4 @@ export default class Head extends Node {
return (child.type !== 'Text' || /\S/.test(child.data));
}));
}

init(
block: Block,
stripWhitespace: boolean,
nextSibling: Node
) {
this.initChildren(block, true, null);
}

build(
block: Block,
parentNode: string,
parentNodes: string
) {
this.var = 'document.head';

this.children.forEach((child: Node) => {
child.build(block, 'document.head', null);
});
}
}
54 changes: 0 additions & 54 deletions src/compile/nodes/Text.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,4 @@
import { stringify } from '../../utils/stringify';
import Node from './shared/Node';
import Block from '../render-dom/Block';

// Whitespace inside one of these elements will not result in
// a whitespace node being created in any circumstances. (This
// list is almost certainly very incomplete)
const elementsWithoutText = new Set([
'audio',
'datalist',
'dl',
'optgroup',
'select',
'video',
]);

function shouldSkip(node: Text) {
if (/\S/.test(node.data)) return false;

const parentElement = node.findNearest(/(?:Element|InlineComponent|Head)/);
if (!parentElement) return false;

if (parentElement.type === 'Head') return true;
if (parentElement.type === 'InlineComponent') return parentElement.children.length === 1 && node === parentElement.children[0];

return parentElement.namespace || elementsWithoutText.has(parentElement.name);
}

export default class Text extends Node {
type: 'Text';
Expand All @@ -35,32 +9,4 @@ export default class Text extends Node {
super(component, parent, scope, info);
this.data = info.data;
}

init(block: Block) {
if (shouldSkip(this)) {
this.shouldSkip = true;
return;
}

this.var = block.getUniqueName(`text`);
}

build(
block: Block,
parentNode: string,
parentNodes: string
) {
if (this.shouldSkip) return;

block.addElement(
this.var,
`@createText(${stringify(this.data)})`,
parentNodes && `@claimText(${parentNodes}, ${stringify(this.data)})`,
parentNode
);
}

remount(name: string) {
return `@append(${name}._slotted.default, ${this.var});`;
}
}
84 changes: 0 additions & 84 deletions src/compile/nodes/Title.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { stringify } from '../../utils/stringify';
import Node from './shared/Node';
import Block from '../render-dom/Block';
import mapChildren from './shared/mapChildren';

export default class Title extends Node {
Expand Down Expand Up @@ -35,86 +33,4 @@ export default class Title extends Node {
)
: true;
}

build(
block: Block,
parentNode: string,
parentNodes: string
) {
const isDynamic = !!this.children.find(node => node.type !== 'Text');

if (isDynamic) {
let value;

const allDependencies = new Set();

// TODO some of this code is repeated in Tag.ts — would be good to
// DRY it out if that's possible without introducing crazy indirection
if (this.children.length === 1) {
// single {tag} — may be a non-string
const { expression } = this.children[0];
const { dependencies, snippet } = this.children[0].expression;

value = snippet;
dependencies.forEach(d => {
allDependencies.add(d);
});
} else {
// '{foo} {bar}' — treat as string concatenation
value =
(this.children[0].type === 'Text' ? '' : `"" + `) +
this.children
.map((chunk: Node) => {
if (chunk.type === 'Text') {
return stringify(chunk.data);
} else {
const { dependencies, snippet } = chunk.expression;

dependencies.forEach(d => {
allDependencies.add(d);
});

return chunk.expression.getPrecedence() <= 13 ? `(${snippet})` : snippet;
}
})
.join(' + ');
}

const last = this.shouldCache && block.getUniqueName(
`title_value`
);

if (this.shouldCache) block.addVariable(last);

let updater;
const init = this.shouldCache ? `${last} = ${value}` : value;

block.builders.init.addLine(
`document.title = ${init};`
);
updater = `document.title = ${this.shouldCache ? last : value};`;

if (allDependencies.size) {
const dependencies = Array.from(allDependencies);
const changedCheck = (
( block.hasOutros ? `!#current || ` : '' ) +
dependencies.map(dependency => `changed.${dependency}`).join(' || ')
);

const updateCachedValue = `${last} !== (${last} = ${value})`;

const condition = this.shouldCache ?
( dependencies.length ? `(${changedCheck}) && ${updateCachedValue}` : updateCachedValue ) :
changedCheck;

block.builders.update.addConditional(
condition,
updater
);
}
} else {
const value = stringify(this.children[0].data);
block.builders.hydrate.addLine(`document.title = ${value};`);
}
}
}
88 changes: 0 additions & 88 deletions src/compile/nodes/shared/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,94 +39,6 @@ export default class Node {
}
}

init(
block: Block,
stripWhitespace: boolean,
nextSibling: Node
) {
// implemented by subclasses
}

initChildren(
block: Block,
stripWhitespace: boolean,
nextSibling: Node
) {
// glue text nodes together
const cleaned: Node[] = [];
let lastChild: Node;

let windowComponent;

this.children.forEach((child: Node) => {
if (child.type === 'Comment') return;

// special case — this is an easy way to remove whitespace surrounding
// <svelte:window/>. lil hacky but it works
if (child.type === 'Window') {
windowComponent = child;
return;
}

if (child.type === 'Text' && lastChild && lastChild.type === 'Text') {
lastChild.data += child.data;
lastChild.end = child.end;
} else {
if (child.type === 'Text' && stripWhitespace && cleaned.length === 0) {
child.data = trimStart(child.data);
if (child.data) cleaned.push(child);
} else {
cleaned.push(child);
}
}

lastChild = child;
});

lastChild = null;

cleaned.forEach((child: Node, i: number) => {
child.canUseInnerHTML = !this.component.options.hydratable;

child.init(block, stripWhitespace, cleaned[i + 1] || nextSibling);

if (child.shouldSkip) return;

if (lastChild) lastChild.next = child;
child.prev = lastChild;

lastChild = child;
});

// We want to remove trailing whitespace inside an element/component/block,
// *unless* there is no whitespace between this node and its next sibling
if (stripWhitespace && lastChild && lastChild.type === 'Text') {
const shouldTrim = (
nextSibling ? (nextSibling.type === 'Text' && /^\s/.test(nextSibling.data)) : !this.hasAncestor('EachBlock')
);

if (shouldTrim) {
lastChild.data = trimEnd(lastChild.data);
if (!lastChild.data) {
cleaned.pop();
lastChild = cleaned[cleaned.length - 1];
lastChild.next = null;
}
}
}

this.children = cleaned;
if (windowComponent) cleaned.unshift(windowComponent);
}

build(
block: Block,
parentNode: string,
parentNodes: string
) {
// implemented by subclasses
}

hasAncestor(type: string) {
return this.parent ?
this.parent.type === type || this.parent.hasAncestor(type) :
Expand Down
1 change: 1 addition & 0 deletions src/compile/render-dom/Block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import CodeBuilder from '../../utils/CodeBuilder';
import deindent from '../../utils/deindent';
import { escape } from '../../utils/stringify';
import Renderer from './Renderer';
import Wrapper from './wrappers/shared/Wrapper';

export interface BlockOptions {
parent?: Block;
Expand Down

0 comments on commit 2600acf

Please sign in to comment.