start tidying up

pull/2252/head
Richard Harris 7 years ago
parent e5050a3621
commit 4d1c0c5800

@ -12,27 +12,27 @@ type Timing = {
children: Timing[];
}
function collapseTimings(timings) {
function collapse_timings(timings) {
const result = {};
timings.forEach(timing => {
result[timing.label] = Object.assign({
total: timing.end - timing.start
}, timing.children && collapseTimings(timing.children));
}, timing.children && collapse_timings(timing.children));
});
return result;
}
export default class Stats {
startTime: number;
currentTiming: Timing;
currentChildren: Timing[];
start_time: number;
current_timing: Timing;
current_children: Timing[];
timings: Timing[];
stack: Timing[];
constructor() {
this.startTime = now();
this.start_time = now();
this.stack = [];
this.currentChildren = this.timings = [];
this.current_children = this.timings = [];
}
start(label) {
@ -43,28 +43,28 @@ export default class Stats {
children: []
};
this.currentChildren.push(timing);
this.current_children.push(timing);
this.stack.push(timing);
this.currentTiming = timing;
this.currentChildren = timing.children;
this.current_timing = timing;
this.current_children = timing.children;
}
stop(label) {
if (label !== this.currentTiming.label) {
throw new Error(`Mismatched timing labels (expected ${this.currentTiming.label}, got ${label})`);
if (label !== this.current_timing.label) {
throw new Error(`Mismatched timing labels (expected ${this.current_timing.label}, got ${label})`);
}
this.currentTiming.end = now();
this.current_timing.end = now();
this.stack.pop();
this.currentTiming = this.stack[this.stack.length - 1];
this.currentChildren = this.currentTiming ? this.currentTiming.children : this.timings;
this.current_timing = this.stack[this.stack.length - 1];
this.current_children = this.current_timing ? this.current_timing.children : this.timings;
}
render() {
const timings = Object.assign({
total: now() - this.startTime
}, collapseTimings(this.timings));
total: now() - this.start_time
}, collapse_timings(this.timings));
return {
timings

@ -6,7 +6,7 @@ import reservedNames from '../utils/reservedNames';
import { namespaces, validNamespaces } from '../utils/namespaces';
import { removeNode } from '../utils/removeNode';
import wrapModule from './wrapModule';
import { createScopes, extractNames, Scope } from '../utils/annotateWithScopes';
import { create_scopes, extract_names, Scope } from './utils/scope';
import Stylesheet from './css/Stylesheet';
import { test } from '../config';
import Fragment from './nodes/Fragment';
@ -15,7 +15,7 @@ import { Node, Ast, CompileOptions, Var, Warning } from '../interfaces';
import error from '../utils/error';
import getCodeFrame from '../utils/getCodeFrame';
import flattenReference from '../utils/flattenReference';
import isReference from 'is-reference';
import is_reference from 'is-reference';
import TemplateScope from './nodes/shared/TemplateScope';
import fuzzymatch from '../utils/fuzzymatch';
import { remove_indentation, add_indentation } from '../utils/indentation';
@ -439,7 +439,7 @@ export default class Component {
if (node.declaration) {
if (node.declaration.type === 'VariableDeclaration') {
node.declaration.declarations.forEach(declarator => {
extractNames(declarator.id).forEach(name => {
extract_names(declarator.id).forEach(name => {
const variable = this.var_lookup.get(name);
variable.export_name = name;
});
@ -511,7 +511,7 @@ export default class Component {
this.addSourcemapLocations(script.content);
let { scope, globals } = createScopes(script.content);
let { scope, globals } = create_scopes(script.content);
this.module_scope = scope;
scope.declarations.forEach((node, name) => {
@ -569,7 +569,7 @@ export default class Component {
}
});
let { scope: instance_scope, map, globals } = createScopes(script.content);
let { scope: instance_scope, map, globals } = create_scopes(script.content);
this.instance_scope = instance_scope;
this.instance_scope_map = map;
@ -663,14 +663,14 @@ export default class Component {
names = deep
? [getObject(node.left).name]
: extractNames(node.left);
: extract_names(node.left);
} else if (node.type === 'UpdateExpression') {
names = [getObject(node.argument).name];
}
if (names) {
names.forEach(name => {
if (scope.findOwner(name) === instance_scope) {
if (scope.find_owner(name) === instance_scope) {
const variable = component.var_lookup.get(name);
variable[deep ? 'mutated' : 'reassigned'] = true;
}
@ -698,7 +698,7 @@ export default class Component {
scope = map.get(node);
}
if (isReference(node, parent)) {
if (is_reference(node, parent)) {
const object = getObject(node);
const { name } = object;
@ -757,7 +757,7 @@ export default class Component {
if (declarator.id.type !== 'Identifier') {
const inserts = [];
extractNames(declarator.id).forEach(name => {
extract_names(declarator.id).forEach(name => {
const variable = component.var_lookup.get(name);
if (variable.export_name) {
@ -946,9 +946,9 @@ export default class Component {
scope = map.get(node);
}
if (isReference(node, parent)) {
if (is_reference(node, parent)) {
const { name } = flattenReference(node);
const owner = scope.findOwner(name);
const owner = scope.find_owner(name);
if (name[0] === '$' && !owner) {
hoistable = false;
@ -1034,11 +1034,11 @@ export default class Component {
} else if (node.type === 'UpdateExpression') {
const identifier = getObject(node.argument);
assignees.add(identifier.name);
} else if (isReference(node, parent)) {
} else if (is_reference(node, parent)) {
const identifier = getObject(node);
if (!assignee_nodes.has(identifier)) {
const { name } = identifier;
const owner = scope.findOwner(name);
const owner = scope.find_owner(name);
if (
(!owner || owner === component.instance_scope) &&
(name[0] === '$' || component.var_lookup.has(name) && component.var_lookup.get(name).writable)

@ -1,5 +1,5 @@
import { stringify } from '../../utils/stringify';
import addToSet from '../../utils/addToSet';
import add_to_set from '../utils/add_to_set';
import Component from '../Component';
import Node from './shared/Node';
import Element from './Element';
@ -59,7 +59,7 @@ export default class Attribute extends Node {
const expression = new Expression(component, this, scope, node.expression);
addToSet(this.dependencies, expression.dependencies);
add_to_set(this.dependencies, expression.dependencies);
return expression;
});
@ -79,7 +79,7 @@ export default class Attribute extends Node {
const dependencies = new Set();
this.chunks.forEach(chunk => {
if (chunk.type === 'Expression') {
addToSet(dependencies, chunk.dynamic_dependencies());
add_to_set(dependencies, chunk.dynamic_dependencies());
}
});

@ -1,8 +1,8 @@
import Component from '../../Component';
import { walk } from 'estree-walker';
import isReference from 'is-reference';
import is_reference from 'is-reference';
import flattenReference from '../../../utils/flattenReference';
import { createScopes, Scope, extractNames } from '../../../utils/annotateWithScopes';
import { create_scopes, Scope, extract_names } from '../../utils/scope';
import { Node } from '../../../interfaces';
import globalWhitelist from '../../../utils/globalWhitelist';
import deindent from '../../../utils/deindent';
@ -97,7 +97,7 @@ export default class Expression {
const { dependencies, contextual_dependencies } = this;
let { map, scope } = createScopes(info);
let { map, scope } = create_scopes(info);
this.scope = scope;
this.scope_map = map;
@ -118,7 +118,7 @@ export default class Expression {
function_expression = node;
}
if (isReference(node, parent)) {
if (is_reference(node, parent)) {
const { name, nodes } = flattenReference(node);
if (scope.has(name)) return;
@ -165,7 +165,7 @@ export default class Expression {
deep = node.left.type === 'MemberExpression';
names = deep
? [getObject(node.left).name]
: extractNames(node.left);
: extract_names(node.left);
} else if (node.type === 'UpdateExpression') {
const { name } = getObject(node.argument);
names = [name];
@ -253,7 +253,7 @@ export default class Expression {
scope = map.get(node);
}
if (isReference(node, parent)) {
if (is_reference(node, parent)) {
const { name, nodes } = flattenReference(node);
if (scope.has(name)) return;
@ -290,7 +290,7 @@ export default class Expression {
if (node.type === 'AssignmentExpression') {
const names = node.left.type === 'MemberExpression'
? [getObject(node.left).name]
: extractNames(node.left);
: extract_names(node.left);
if (node.operator === '=' && nodes_match(node.left, node.right)) {
const dirty = names.filter(name => {

@ -5,10 +5,10 @@ import Component from '../Component';
import Renderer from './Renderer';
import { CompileOptions } from '../../interfaces';
import { walk } from 'estree-walker';
import stringifyProps from '../../utils/stringifyProps';
import addToSet from '../../utils/addToSet';
import { stringify_props } from '../utils/stringify_props';
import add_to_set from '../utils/add_to_set';
import getObject from '../../utils/getObject';
import { extractNames } from '../../utils/annotateWithScopes';
import { extract_names } from '../utils/scope';
import { nodes_match } from '../../utils/nodes_match';
import sanitize from '../../utils/sanitize';
@ -171,12 +171,12 @@ export default function dom(
const left_object_name = getObject(node.left).name;
left_object_name && (names = [left_object_name]);
} else {
names = extractNames(node.left);
names = extract_names(node.left);
}
if (node.operator === '=' && nodes_match(node.left, node.right)) {
const dirty = names.filter(name => {
return scope.findOwner(name) === component.instance_scope;
return scope.find_owner(name) === component.instance_scope;
});
if (dirty.length) component.has_reactive_assignments = true;
@ -184,7 +184,7 @@ export default function dom(
code.overwrite(node.start, node.end, dirty.map(n => component.invalidate(n)).join('; '));
} else {
names.forEach(name => {
const owner = scope.findOwner(name);
const owner = scope.find_owner(name);
if (owner && owner !== component.instance_scope) return;
const variable = component.var_lookup.get(name);
@ -199,7 +199,7 @@ export default function dom(
else if (node.type === 'UpdateExpression') {
const { name } = getObject(node.argument);
if (scope.findOwner(name) !== component.instance_scope) return;
if (scope.find_owner(name) !== component.instance_scope) return;
const variable = component.var_lookup.get(name);
if (variable && variable.hoistable) return;
@ -322,7 +322,7 @@ export default function dom(
const all_reactive_dependencies = new Set();
component.reactive_declarations.forEach(d => {
addToSet(all_reactive_dependencies, d.dependencies);
add_to_set(all_reactive_dependencies, d.dependencies);
});
const reactive_store_subscriptions = reactive_stores
@ -404,7 +404,7 @@ export default function dom(
};
`}
return ${stringifyProps(filtered_declarations)};
return ${stringify_props(filtered_declarations)};
}
`);
}

@ -2,7 +2,7 @@ import Renderer from '../Renderer';
import Wrapper from './shared/Wrapper';
import Block from '../Block';
import DebugTag from '../../nodes/DebugTag';
import addToSet from '../../../utils/addToSet';
import add_to_set from '../../utils/add_to_set';
import deindent from '../../../utils/deindent';
export default class DebugTagWrapper extends Wrapper {
@ -45,7 +45,7 @@ export default class DebugTagWrapper extends Wrapper {
const dependencies = new Set();
this.node.expressions.forEach(expression => {
addToSet(dependencies, expression.dependencies);
add_to_set(dependencies, expression.dependencies);
});
const condition = Array.from(dependencies).map(d => `changed.${d}`).join(' || ');

@ -4,7 +4,7 @@ import AttributeWrapper from './Attribute';
import Node from '../../../nodes/shared/Node';
import ElementWrapper from '.';
import { stringify } from '../../../../utils/stringify';
import addToSet from '../../../../utils/addToSet';
import add_to_set from '../../../utils/add_to_set';
export interface StyleProp {
key: string;
@ -35,7 +35,7 @@ export default class StyleAttributeWrapper extends AttributeWrapper {
} else {
const snippet = chunk.render();
addToSet(propDependencies, chunk.dependencies);
add_to_set(propDependencies, chunk.dependencies);
return chunk.getPrecedence() <= 13 ? `(${snippet})` : snippet;
}

@ -16,7 +16,7 @@ import StyleAttributeWrapper from './StyleAttribute';
import { dimensions } from '../../../../utils/patterns';
import Binding from './Binding';
import InlineComponentWrapper from '../InlineComponent';
import addToSet from '../../../../utils/addToSet';
import add_to_set from '../../../utils/add_to_set';
import addEventHandlers from '../shared/addEventHandlers';
import addActions from '../shared/addActions';
import createDebuggingComment from '../../../../utils/createDebuggingComment';
@ -420,9 +420,9 @@ export default class ElementWrapper extends Wrapper {
group.bindings.forEach(binding => {
// TODO this is a mess
addToSet(dependencies, binding.get_dependencies());
addToSet(contextual_dependencies, binding.node.expression.contextual_dependencies);
addToSet(contextual_dependencies, binding.handler.contextual_dependencies);
add_to_set(dependencies, binding.get_dependencies());
add_to_set(contextual_dependencies, binding.node.expression.contextual_dependencies);
add_to_set(contextual_dependencies, binding.handler.contextual_dependencies);
binding.render(block, lock);
});

@ -4,8 +4,8 @@ import Block from '../../Block';
import InlineComponent from '../../../nodes/InlineComponent';
import FragmentWrapper from '../Fragment';
import { quoteNameIfNecessary, quotePropIfNecessary } from '../../../../utils/quoteIfNecessary';
import stringifyProps from '../../../../utils/stringifyProps';
import addToSet from '../../../../utils/addToSet';
import { stringify_props } from '../../../utils/stringify_props';
import add_to_set from '../../../utils/add_to_set';
import deindent from '../../../../utils/deindent';
import Attribute from '../../../nodes/Attribute';
import getObject from '../../../../utils/getObject';
@ -124,8 +124,8 @@ export default class InlineComponentWrapper extends Wrapper {
if (slot_props.length > 0) slot_props.push(`$$scope: { ctx }`);
const attributeObject = usesSpread
? stringifyProps(slot_props)
: stringifyProps(
? stringify_props(slot_props)
: stringify_props(
this.node.attributes.map(attr => `${quoteNameIfNecessary(attr.name)}: ${attr.getValue(block)}`).concat(slot_props)
);
@ -184,7 +184,7 @@ export default class InlineComponentWrapper extends Wrapper {
const allDependencies = new Set();
this.node.attributes.forEach(attr => {
addToSet(allDependencies, attr.dependencies);
add_to_set(allDependencies, attr.dependencies);
});
this.node.attributes.forEach(attr => {
@ -378,7 +378,7 @@ export default class InlineComponentWrapper extends Wrapper {
${(this.node.attributes.length || this.node.bindings.length) && deindent`
${props && `let ${props} = ${attributeObject};`}`}
${statements}
return ${stringifyProps(component_opts)};
return ${stringify_props(component_opts)};
}
if (${switch_value}) {
@ -469,7 +469,7 @@ export default class InlineComponentWrapper extends Wrapper {
${(this.node.attributes.length || this.node.bindings.length) && deindent`
${props && `let ${props} = ${attributeObject};`}`}
${statements}
var ${name} = new ${expression}(${stringifyProps(component_opts)});
var ${name} = new ${expression}(${stringify_props(component_opts)});
${munged_bindings}
${munged_handlers}

@ -5,9 +5,9 @@ import Slot from '../../nodes/Slot';
import FragmentWrapper from './Fragment';
import deindent from '../../../utils/deindent';
import sanitize from '../../../utils/sanitize';
import addToSet from '../../../utils/addToSet';
import add_to_set from '../../utils/add_to_set';
import get_slot_data from '../../../utils/get_slot_data';
import stringifyProps from '../../../utils/stringifyProps';
import { stringify_props } from '../../utils/stringify_props';
import Expression from '../../nodes/shared/Expression';
export default class SlotWrapper extends Wrapper {
@ -38,7 +38,7 @@ export default class SlotWrapper extends Wrapper {
);
this.node.attributes.forEach(attribute => {
addToSet(this.dependencies, attribute.dependencies);
add_to_set(this.dependencies, attribute.dependencies);
});
block.addDependencies(this.dependencies);
@ -71,8 +71,8 @@ export default class SlotWrapper extends Wrapper {
attributes.forEach(attribute => {
attribute.chunks.forEach(chunk => {
if ((chunk as Expression).dependencies) {
addToSet(dependencies, (chunk as Expression).dependencies);
addToSet(dependencies, (chunk as Expression).contextual_dependencies);
add_to_set(dependencies, (chunk as Expression).dependencies);
add_to_set(dependencies, (chunk as Expression).contextual_dependencies);
}
});
@ -84,8 +84,8 @@ export default class SlotWrapper extends Wrapper {
const arg = dependencies.size > 0 ? `{ ${Array.from(dependencies).join(', ')} }` : '{}';
renderer.blocks.push(deindent`
const ${get_slot_changes} = (${arg}) => (${stringifyProps(changes_props)});
const ${get_slot_context} = (${arg}) => (${stringifyProps(context_props)});
const ${get_slot_changes} = (${arg}) => (${stringify_props(changes_props)});
const ${get_slot_context} = (${arg}) => (${stringify_props(context_props)});
`);
} else {
get_slot_context = 'null';

@ -4,7 +4,7 @@ import Block from '../Block';
import Title from '../../nodes/Title';
import FragmentWrapper from './Fragment';
import { stringify } from '../../../utils/stringify';
import addToSet from '../../../utils/addToSet';
import add_to_set from '../../utils/add_to_set';
export default class TitleWrapper extends Wrapper {
node: Title;
@ -34,7 +34,7 @@ export default class TitleWrapper extends Wrapper {
// single {tag} — may be a non-string
const { expression } = this.node.children[0];
value = expression.render(block);
addToSet(allDependencies, expression.dependencies);
add_to_set(allDependencies, expression.dependencies);
} else {
// '{foo} {bar}' — treat as string concatenation
value =

@ -2,7 +2,7 @@ import { escape, escapeTemplate, stringify } from '../../../utils/stringify';
import { quoteNameIfNecessary } from '../../../utils/quoteIfNecessary';
import { snip } from '../../../utils/snip';
import Renderer from '../Renderer';
import stringifyProps from '../../../utils/stringifyProps';
import { stringify_props } from '../../utils/stringify_props';
import { get_slot_scope } from './shared/get_slot_scope';
type AppendTarget = any; // TODO
@ -63,14 +63,14 @@ export default function(node, renderer: Renderer, options) {
.join(', ')
})`;
} else {
props = stringifyProps(
props = stringify_props(
node.attributes
.map(attribute => `${attribute.name}: ${getAttributeValue(attribute)}`)
.concat(binding_props)
);
}
const bindings = stringifyProps(binding_fns);
const bindings = stringify_props(binding_fns);
const expression = (
node.name === 'svelte:self'
@ -108,7 +108,7 @@ export default function(node, renderer: Renderer, options) {
renderer.targets.pop();
}
const slots = stringifyProps(slot_fns);
const slots = stringify_props(slot_fns);
renderer.append(`\${@validate_component(${expression}, '${node.name}').$$render($$result, ${props}, ${bindings}, ${slots})}`);
}

@ -3,8 +3,6 @@ import Component from '../Component';
import { CompileOptions } from '../../interfaces';
import { stringify } from '../../utils/stringify';
import Renderer from './Renderer';
import { walk } from 'estree-walker';
import { extractNames } from '../../utils/annotateWithScopes';
export default function ssr(
component: Component,

@ -0,0 +1,5 @@
export default function add_to_set(a: Set<any>, b: Set<any>) {
b.forEach(item => {
a.add(item);
});
}

@ -1,8 +1,8 @@
import { walk } from 'estree-walker';
import isReference from 'is-reference';
import { Node } from '../interfaces';
import is_reference from 'is-reference';
import { Node } from '../../interfaces';
export function createScopes(expression: Node) {
export function create_scopes(expression: Node) {
const map = new WeakMap();
const globals: Map<string, Node> = new Map();
@ -26,7 +26,7 @@ export function createScopes(expression: Node) {
}
node.params.forEach((param: Node) => {
extractNames(param).forEach(name => {
extract_names(param).forEach(name => {
scope.declarations.set(name, node);
});
});
@ -37,8 +37,8 @@ export function createScopes(expression: Node) {
scope = new Scope(scope, true);
map.set(node, scope);
} else if (/(Class|Variable)Declaration/.test(node.type)) {
scope.addDeclaration(node);
} else if (node.type === 'Identifier' && isReference(node, parent)) {
scope.add_declaration(node);
} else if (node.type === 'Identifier' && is_reference(node, parent)) {
if (!scope.has(node.name) && !globals.has(node.name)) {
globals.set(node.name, node);
}
@ -71,14 +71,14 @@ export class Scope {
this.block = block;
}
addDeclaration(node: Node) {
add_declaration(node: Node) {
if (node.kind === 'var' && this.block && this.parent) {
this.parent.addDeclaration(node);
this.parent.add_declaration(node);
} else if (node.type === 'VariableDeclaration') {
const initialised = !!node.init;
node.declarations.forEach((declarator: Node) => {
extractNames(declarator.id).forEach(name => {
extract_names(declarator.id).forEach(name => {
this.declarations.set(name, node);
if (initialised) this.initialised_declarations.add(name);
});
@ -88,9 +88,9 @@ export class Scope {
}
}
findOwner(name: string): Scope {
find_owner(name: string): Scope {
if (this.declarations.has(name)) return this;
return this.parent && this.parent.findOwner(name);
return this.parent && this.parent.find_owner(name);
}
has(name: string): boolean {
@ -100,7 +100,7 @@ export class Scope {
}
}
export function extractNames(param: Node) {
export function extract_names(param: Node) {
const names: string[] = [];
extractors[param.type](names, param);
return names;
@ -133,5 +133,5 @@ const extractors = {
AssignmentPattern(names: string[], param: Node) {
extractors[param.left.type](names, param.left);
},
}
};

@ -1,4 +1,4 @@
export default function stringifyProps(props: string[]) {
export function stringify_props(props: string[]) {
if (!props.length) return '{}';
const joined = props.join(', ');

@ -1,5 +0,0 @@
export default function addToSet(a: Set<any>, b: Set<any>) {
b.forEach(item => {
a.add(item);
});
}
Loading…
Cancel
Save