avoid unnecessary if (changed.foo....) condiitons

pull/1839/head
Rich Harris 7 years ago
parent f288259c34
commit 5290988ed1

@ -65,8 +65,9 @@ export default class Expression {
node: any;
snippet: string;
references: Set<string>;
dependencies: Set<string>;
contextual_dependencies: Set<string>;
dependencies: Set<string> = new Set();
contextual_dependencies: Set<string> = new Set();
dynamic_dependencies: Set<string> = new Set();
template_scope: TemplateScope;
scope: Scope;
@ -99,17 +100,31 @@ export default class Expression {
this.owner = owner;
this.is_synthetic = owner.isSynthetic;
const expression_dependencies = new Set();
const expression_contextual_dependencies = new Set();
let dependencies = expression_dependencies;
let contextual_dependencies = expression_contextual_dependencies;
const { dependencies, contextual_dependencies, dynamic_dependencies } = this;
let { map, scope } = createScopes(info);
this.scope = scope;
this.scope_map = map;
const expression = this;
let function_expression;
function add_dependency(name) {
dependencies.add(name);
if (!function_expression) {
// dynamic_dependencies is used to create `if (changed.foo || ...)`
// conditions — it doesn't apply if the dependency is inside a
// function, and it only applies if the dependency is writable
if (component.instance_script) {
if (component.writable_declarations.has(name)) {
dynamic_dependencies.add(name);
}
} else {
dynamic_dependencies.add(name);
}
}
}
// discover dependencies, but don't change the code yet
walk(info, {
@ -121,6 +136,10 @@ export default class Expression {
scope = map.get(node);
}
if (!function_expression && /FunctionExpression/.test(node.type)) {
function_expression = node;
}
if (isReference(node, parent)) {
const { name } = flattenReference(node);
@ -132,21 +151,26 @@ export default class Expression {
contextual_dependencies.add(name);
template_scope.dependenciesForName.get(name).forEach(dependency => {
dependencies.add(dependency);
});
template_scope.dependenciesForName.get(name).forEach(add_dependency);
} else {
dependencies.add(name);
add_dependency(name);
component.template_references.add(name);
}
this.skip();
}
},
leave(node) {
if (map.has(node)) {
scope = scope.parent;
}
});
this.dependencies = dependencies;
this.contextual_dependencies = contextual_dependencies;
if (node === function_expression) {
function_expression = null;
}
}
});
}
getPrecedence() {

@ -14,7 +14,7 @@ export default function addActions(
let snippet, dependencies;
if (expression) {
snippet = expression.render();
dependencies = expression.dependencies;
dependencies = expression.dynamic_dependencies;
expression.declarations.forEach(declaration => {
block.builders.init.addBlock(declaration);

@ -1,5 +1,5 @@
/* generated by Svelte vX.Y.Z-alpha1 */
import { SvelteComponent as SvelteComponent_1, addListener, createElement, detachNode, flush, init, insert, noop, removeListener, run, safe_not_equal } from "svelte/internal.js";
import { SvelteComponent as SvelteComponent_1, createElement, detachNode, flush, init, insert, noop, run, safe_not_equal } from "svelte/internal.js";
function create_fragment(component, ctx) {
var button, foo_action, current;

Loading…
Cancel
Save