Skip to content
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

big old refactor #992

Merged
merged 32 commits into from
Dec 10, 2017
Merged
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
2e56d10
start moving logic into Node and its subclasses
Rich-Harris Dec 6, 2017
4b31992
remove unused code
Rich-Harris Dec 6, 2017
39f1af2
start implementing build() methods
Rich-Harris Dec 7, 2017
8e9e323
start moving build logic into individual nodes
Rich-Harris Dec 9, 2017
c6d49b7
move some code around
Rich-Harris Dec 9, 2017
daaad80
remove some unused code
Rich-Harris Dec 9, 2017
e8e05e7
move Slot logic
Rich-Harris Dec 9, 2017
425d019
move attribute logic
Rich-Harris Dec 9, 2017
8072f6a
move binding logic
Rich-Harris Dec 9, 2017
596c432
move shared MustacheTag/RawMustacheTag logic
Rich-Harris Dec 9, 2017
56f34c7
remove elementStack
Rich-Harris Dec 9, 2017
fd24a5c
remove componentStack
Rich-Harris Dec 9, 2017
0cf9259
remove inEachBlock
Rich-Harris Dec 9, 2017
67aca17
Merge branch 'master' into gh-979
Rich-Harris Dec 9, 2017
f4888dd
fix a few typescript issues
Rich-Harris Dec 9, 2017
7f77a10
remove confusing state object from init
Rich-Harris Dec 9, 2017
f6ac3a4
remove some unused stuff
Rich-Harris Dec 9, 2017
e30dc8e
put namespace on Element nodes
Rich-Harris Dec 9, 2017
8eacc25
tidy up
Rich-Harris Dec 9, 2017
48ed685
more tidying up
Rich-Harris Dec 9, 2017
05d63e6
simplify
Rich-Harris Dec 9, 2017
16bac3b
more simplification
Rich-Harris Dec 9, 2017
433623c
rename some stuff to be clearer
Rich-Harris Dec 9, 2017
afa996c
remove unused imports
Rich-Harris Dec 9, 2017
e9dc123
DRY out
Rich-Harris Dec 9, 2017
ea418cd
more tidying up
Rich-Harris Dec 9, 2017
3fa83e3
rename _block to block
Rich-Harris Dec 9, 2017
9b2a7e1
remove preprocess from ssr renderer
Rich-Harris Dec 9, 2017
e974fdc
more tidying up
Rich-Harris Dec 9, 2017
a72a6ca
small tweak, possibly ahead of a much larger one
Rich-Harris Dec 9, 2017
f7d4994
tidy up
Rich-Harris Dec 9, 2017
cf94217
remove unused code
Rich-Harris Dec 10, 2017
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
Prev Previous commit
Next Next commit
start moving build logic into individual nodes
  • Loading branch information
Rich-Harris committed Dec 9, 2017
commit 8e9e323b13fd011db140637cdc4eb77fb523b1a8
3 changes: 3 additions & 0 deletions src/generators/Generator.ts
Original file line number Diff line number Diff line change
@@ -715,6 +715,9 @@ export default class Generator {
if (node.type === 'Element' && (node.name === ':Component' || node.name === ':Self' || generator.components.has(node.name))) {
node.type = 'Component';
node.__proto__ = nodes.Component.prototype;
} else if (node.name === ':Window') { // TODO do this in parse?
node.type = 'Window';
node.__proto__ = nodes.Window.prototype;
} else if (node.type in nodes) {
node.__proto__ = nodes[node.type].prototype;
}
2 changes: 1 addition & 1 deletion src/generators/dom/State.ts
Original file line number Diff line number Diff line change
@@ -25,7 +25,7 @@ export default class State {
assign(this, data)
}

child(data: StateData) {
child(data?: StateData) {
return new State(assign({}, this, {
parentNode: null,
parentNodes: 'nodes'
2 changes: 1 addition & 1 deletion src/generators/dom/index.ts
Original file line number Diff line number Diff line change
@@ -103,7 +103,7 @@ export default function dom(
// parsed.html.children.forEach((node: Node) => {
// visit(generator, block, state, node, [], []);
// });
parsed.html.build();
parsed.html.build(block, state);

const builder = new CodeBuilder();
const computationBuilder = new CodeBuilder();
2 changes: 1 addition & 1 deletion src/generators/dom/visitors/shared/Tag.ts
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ import deindent from '../../../../utils/deindent';
import { DomGenerator } from '../../index';
import Block from '../../Block';
import { Node } from '../../../../interfaces';
import { State } from '../../interfaces';
import State from '../../State';

export default function visitTag(
generator: DomGenerator,
12 changes: 11 additions & 1 deletion src/generators/nodes/AwaitBlock.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import Node from './shared/Node';
import { DomGenerator } from '../dom/index';
import Block from '../dom/Block';
import visitAwaitBlock from '../dom/visitors/AwaitBlock';
import PendingBlock from './PendingBlock';
import ThenBlock from './ThenBlock';
import CatchBlock from './CatchBlock';
import { State } from '../dom/interfaces';
import State from '../dom/State';
import createDebuggingComment from '../../utils/createDebuggingComment';

export default class AwaitBlock extends Node {
@@ -65,4 +66,13 @@ export default class AwaitBlock extends Node {
this.then._block.hasUpdateMethod = dynamic;
this.catch._block.hasUpdateMethod = dynamic;
}

build(
block: Block,
state: State,
elementStack: Node[],
componentStack: Node[]
) {
visitAwaitBlock(this.generator, block, state, this, elementStack, componentStack);
}
}
10 changes: 10 additions & 0 deletions src/generators/nodes/Component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Node from './shared/Node';
import Block from '../dom/Block';
import State from '../dom/State';
import visitComponent from '../dom/visitors/Component';

export default class Component extends Node {
type: 'Component'; // TODO fix this?
@@ -58,4 +59,13 @@ export default class Component extends Node {
});
}
}

build(
block: Block,
state: State,
elementStack: Node[],
componentStack: Node[]
) {
visitComponent(this.generator, block, state, this, elementStack, componentStack);
}
}
10 changes: 10 additions & 0 deletions src/generators/nodes/EachBlock.ts
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ import ElseBlock from './ElseBlock';
import { DomGenerator } from '../dom/index';
import Block from '../dom/Block';
import State from '../dom/State';
import visitEachBlock from '../dom/visitors/EachBlock';
import createDebuggingComment from '../../utils/createDebuggingComment';

export default class EachBlock extends Node {
@@ -112,4 +113,13 @@ export default class EachBlock extends Node {
this.else._block.hasUpdateMethod = this.else._block.dependencies.size > 0;
}
}

build(
block: Block,
state: State,
elementStack: Node[],
componentStack: Node[]
) {
visitEachBlock(this.generator, block, state, this, elementStack, componentStack);
}
}
45 changes: 25 additions & 20 deletions src/generators/nodes/Element.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import deindent from '../../utils/deindent';
import { stringify } from '../../utils/stringify';
import flattenReference from '../../utils/flattenReference';
import isVoidElementName from '../../utils/isVoidElementName';
import validCalleeObjects from '../../utils/validCalleeObjects';
import reservedNames from '../../utils/reservedNames';
import Node from './shared/Node';
import Block from '../dom/Block';
import State from '../dom/State';
import Attribute from './Attribute';
import * as namespaces from '../../utils/namespaces';

const meta: Record<string, any> = {
':Window': {}, // TODO this should be dealt with in walkTemplate
};
// temp - move this logic in here
import addBindings from '../dom/visitors/Element/addBindings';
import addTransitions from '../dom/visitors/Element/addTransitions';
import visitAttribute from '../dom/visitors/Element/Attribute';
import visitSlot from '../dom/visitors/Slot';

export default class Element extends Node {
type: 'Element';
@@ -141,17 +149,14 @@ export default class Element extends Node {
build(
block: Block,
state: State,
node: Node,
elementStack: Node[],
componentStack: Node[]
) {
if (this.name in meta) {
return meta[this.name](generator, block, this);
}
const { generator } = this;

if (this.name === 'slot') { // TODO deal with in walkTemplate
if (this.generator.customElement) {
const slotName = getStaticAttributeValue(this, 'name') || 'default';
const slotName = this.getStaticAttributeValue('name') || 'default';
this.generator.slots.add(slotName);
} else {
return visitSlot(this.generator, block, state, this, elementStack, componentStack);
@@ -177,7 +182,7 @@ export default class Element extends Node {

if (this.generator.hydratable) {
block.builders.claim.addBlock(deindent`
${name} = ${getClaimStatement(generator, childState.namespace, state.parentNodes, node)};
${name} = ${getClaimStatement(generator, childState.namespace, state.parentNodes, this)};
var ${childState.parentNodes} = @children(${name});
`);
}
@@ -195,8 +200,8 @@ export default class Element extends Node {
}

// add CSS encapsulation attribute
if (this._needsCssAttribute && !generator.customElement) {
generator.needsEncapsulateHelper = true;
if (this._needsCssAttribute && !this.generator.customElement) {
this.generator.needsEncapsulateHelper = true;
block.builders.hydrate.addLine(
`@encapsulateStyles(${name});`
);
@@ -235,32 +240,32 @@ export default class Element extends Node {
}
} else {
this.children.forEach((child: Node) => {
visit(generator, block, childState, child, elementStack.concat(this), componentStack);
child.build(block, childState, elementStack.concat(this), componentStack);
});
}

addBindings(generator, block, childState, this);
addBindings(this.generator, block, childState, this);

this.attributes.filter((a: Node) => a.type === 'Attribute').forEach((attribute: Node) => {
visitAttribute(generator, block, childState, this, attribute);
visitAttribute(this.generator, block, childState, this, attribute);
});

// event handlers
this.attributes.filter((a: Node) => a.type === 'EventHandler').forEach((attribute: Node) => {
const isCustomEvent = this.generator.events.has(attribute.name);
const isCustomEvent = generator.events.has(attribute.name);
const shouldHoist = !isCustomEvent && state.inEachBlock;

const context = shouldHoist ? null : name;
const usedContexts: string[] = [];

if (attribute.expression) {
this.generator.addSourcemapLocations(attribute.expression);
generator.addSourcemapLocations(attribute.expression);

const flattened = flattenReference(attribute.expression.callee);
if (!validCalleeObjects.has(flattened.name)) {
// allow event.stopPropagation(), this.select() etc
// TODO verify that it's a valid callee (i.e. built-in or declared method)
this.generator.code.prependRight(
generator.code.prependRight(
attribute.expression.start,
`${block.alias('component')}.`
);
@@ -294,7 +299,7 @@ export default class Element extends Node {

// get a name for the event handler that is globally unique
// if hoisted, locally unique otherwise
const handlerName = (shouldHoist ? generator : block).getUniqueName(
const handlerName = (shouldHoist ? this.generator : block).getUniqueName(
`${attribute.name.replace(/[^a-zA-Z0-9_$]/g, '_')}_handler`
);

@@ -344,7 +349,7 @@ export default class Element extends Node {
});

// refs
this.node.attributes.filter((a: Node) => a.type === 'Ref').forEach((attribute: Node) => {
this.attributes.filter((a: Node) => a.type === 'Ref').forEach((attribute: Node) => {
const ref = `#component.refs.${attribute.name}`;

block.builders.mount.addLine(
@@ -358,7 +363,7 @@ export default class Element extends Node {
this.generator.usesRefs = true; // so component.refs object is created
});

addTransitions(this.generator, block, childState, node);
addTransitions(this.generator, block, childState, this);

if (childState.allUsedContexts.length || childState.usesComponent) {
const initialProps: string[] = [];
6 changes: 6 additions & 0 deletions src/generators/nodes/ElseBlock.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import Node from './shared/Node';
import Block from '../dom/Block';
import State from '../dom/State';

export default class ElseBlock extends Node {
type: 'ElseBlock';
children: Node[];

_block: Block;
_state: State;
}
8 changes: 2 additions & 6 deletions src/generators/nodes/Fragment.ts
Original file line number Diff line number Diff line change
@@ -40,15 +40,11 @@ export default class Fragment extends Node {
}

build(
generator: DomGenerator,
block: Block,
state: State,
node: Node,
elementStack: Node[],
componentStack: Node[]
state: State
) {
this.children.forEach(child => {
child.build(block, state, node, elementStack, componentStack);
child.build(block, state, [], []);
});
}
}
Loading