more tidying up

pull/1367/head
Rich Harris 6 years ago
parent 515392ac1d
commit 11fdb086f9

@ -26,6 +26,7 @@ export default class Attribute extends Node {
isTrue: boolean;
isDynamic: boolean;
isSynthetic: boolean;
shouldCache: boolean;
expression?: Expression;
chunks: (Text | Expression)[];
dependencies: Set<string>;
@ -44,6 +45,7 @@ export default class Attribute extends Node {
this.chunks = null;
this.isDynamic = true; // TODO not necessarily
this.shouldCache = false; // TODO does this mean anything here?
}
else {
@ -69,6 +71,12 @@ export default class Attribute extends Node {
this.isDynamic = this.chunks.length === 1
? this.chunks[0].type !== 'Text'
: this.chunks.length > 1;
this.shouldCache = this.isDynamic
? this.chunks.length === 1
? this.chunks[0].node.type !== 'Identifier' || scope.names.has(this.chunks[0].node.name)
: true
: false;
}
}
@ -140,21 +148,11 @@ export default class Attribute extends Node {
if (this.isDynamic) {
let value;
let shouldCache;
// 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.chunks.length === 1) {
// single {tag} — may be a non-string
const expression = this.chunks[0];
const { snippet } = expression;
value = snippet;
shouldCache = (
expression.node.type !== 'Identifier' ||
block.contexts.has(expression.node.name)
);
value = this.chunks[0].snippet;
} else {
// '{foo} {bar}' — treat as string concatenation
value =
@ -170,14 +168,12 @@ export default class Attribute extends Node {
}
})
.join(' + ');
shouldCache = true;
}
const isSelectValueAttribute =
name === 'value' && node.name === 'select';
if (isSelectValueAttribute) shouldCache = true;
const shouldCache = this.shouldCache || isSelectValueAttribute;
const last = shouldCache && block.getUniqueName(
`${node.var}_${name.replace(/[^a-zA-Z_$]/g, '_')}_value`

@ -49,8 +49,7 @@ export default class Binding extends Node {
}
munge(
block: Block,
allUsedContexts: Set<string>
block: Block
) {
const node: Element = this.parent;
@ -60,7 +59,7 @@ export default class Binding extends Node {
let updateCondition: string;
const { name } = getObject(this.value.node);
const { contexts, snippet } = this.value;
const { snippet } = this.value;
// special case: if you have e.g. `<input type=checkbox bind:checked=selected.done>`
// and `selected` is an object chosen with a <select>, then when `checked` changes,
@ -77,10 +76,6 @@ export default class Binding extends Node {
}
});
contexts.forEach(context => {
allUsedContexts.add(context);
});
// view to model
const valueFromDom = getValueFromDom(this.compiler, node, this);
const handler = getEventHandler(this, this.compiler, block, name, snippet, dependencies, valueFromDom);

@ -127,7 +127,6 @@ export default class Component extends Node {
});
}
const allContexts = new Set();
const statements: string[] = [];
const name_initial_data = block.getUniqueName(`${name}_initial_data`);
@ -135,10 +134,6 @@ export default class Component extends Node {
let name_updating: string;
let beforecreate: string = null;
// const eventHandlers = this.attributes
// .filter((a: Node) => a.type === 'EventHandler')
// .map(a => mungeEventHandler(compiler, this, a, block, allContexts));
const updates: string[] = [];
const usesSpread = !!this.attributes.find(a => a.isSpread);
@ -146,7 +141,6 @@ export default class Component extends Node {
const attributeObject = usesSpread
? '{}'
: stringifyProps(
// this.attributes.map(attr => `${attr.name}: ${attr.value}`)
this.attributes.map(attr => `${attr.name}: ${attr.getValue()}`)
);
@ -237,10 +231,6 @@ export default class Component extends Node {
this.bindings.forEach((binding: Binding) => {
let { name: key } = getObject(binding.value.node);
binding.value.contexts.forEach(context => {
allContexts.add(context);
});
let setFromChild;
if (block.contexts.has(key)) { // TODO remove block.contexts
@ -492,51 +482,6 @@ export default class Component extends Node {
}
}
function mungeEventHandler(compiler: DomGenerator, node: Node, handler: Node, block: Block, allContexts: Set<string>) {
let body;
if (handler.expression) {
compiler.addSourcemapLocations(handler.expression);
// TODO try out repetition between this and element counterpart
const flattened = flattenReference(handler.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)
compiler.code.prependRight(
handler.expression.start,
`${block.alias('component')}.`
);
}
let usesState = false;
handler.expression.arguments.forEach((arg: Node) => {
const { contexts } = block.contextualise(arg, null, true);
if (contexts.has('state')) usesState = true;
contexts.forEach(context => {
allContexts.add(context);
});
});
body = deindent`
${usesState && `const ctx = #component.get();`}
[${handler.expression.start}-${handler.expression.end}];
`;
} else {
body = deindent`
#component.fire('${handler.name}', event);
`;
}
return {
name: handler.name,
var: block.getUniqueName(`${node.var}_${handler.name}`),
body
};
}
function isComputed(node: Node) {
while (node.type === 'MemberExpression') {
if (node.computed) return true;

@ -250,7 +250,6 @@ export default class Element extends Node {
};
const name = this.var;
const allUsedContexts: Set<string> = new Set();
const slot = this.attributes.find((attribute: Node) => attribute.name === 'slot');
const initialMountNode = this.slotted ?
@ -355,8 +354,8 @@ export default class Element extends Node {
}
}
this.addBindings(block, allUsedContexts);
this.addEventHandlers(block, allUsedContexts);
this.addBindings(block);
this.addEventHandlers(block);
if (this.ref) this.addRef(block);
this.addAttributes(block);
this.addTransitions(block);
@ -400,8 +399,7 @@ export default class Element extends Node {
}
addBindings(
block: Block,
allUsedContexts: Set<string>
block: Block
) {
if (this.bindings.length === 0) return;
@ -410,7 +408,7 @@ export default class Element extends Node {
const needsLock = this.name !== 'input' || !/radio|checkbox|range|color/.test(this.getStaticAttributeValue('type'));
// TODO munge in constructor
const mungedBindings = this.bindings.map(binding => binding.munge(block, allUsedContexts));
const mungedBindings = this.bindings.map(binding => binding.munge(block));
const lock = mungedBindings.some(binding => binding.needsLock) ?
block.getUniqueName(`${this.var}_updating`) :
@ -572,7 +570,7 @@ export default class Element extends Node {
`);
}
addEventHandlers(block: Block, allUsedContexts) {
addEventHandlers(block: Block) {
const { compiler } = this;
this.handlers.forEach(handler => {

@ -61,7 +61,6 @@ export default class Expression {
usesContext: boolean;
references: Set<string>;
dependencies: Set<string>;
contexts: Set<string>;
thisReferences: Array<{ start: number, end: number }>;
@ -158,8 +157,6 @@ export default class Expression {
});
this.dependencies = dependencies;
this.contexts = new Set(); // TODO...
}
getPrecedence() {

Loading…
Cancel
Save