mirror of https://github.com/sveltejs/svelte
parent
1d8d17bc07
commit
50280232e8
@ -1,66 +1,67 @@
|
||||
import Component from '../Component';
|
||||
import MagicString from 'magic-string';
|
||||
import { Node } from '../../interfaces';
|
||||
import { nodes_match } from '../../utils/nodes_match';
|
||||
// import { nodes_match } from '../../utils/nodes_match';
|
||||
import { Scope } from './scope';
|
||||
|
||||
export function invalidate(component: Component, scope: Scope, code: MagicString, node: Node, names: Set<string>) {
|
||||
const [head, ...tail] = Array.from(names).filter(name => {
|
||||
const owner = scope.find_owner(name);
|
||||
if (owner && owner !== component.instance_scope) return false;
|
||||
export function invalidate(_component: Component, _scope: Scope, _node: Node, _names: Set<string>) {
|
||||
// const [head, ...tail] = Array.from(names).filter(name => {
|
||||
// const owner = scope.find_owner(name);
|
||||
// if (owner && owner !== component.instance_scope) return false;
|
||||
|
||||
const variable = component.var_lookup.get(name);
|
||||
// const variable = component.var_lookup.get(name);
|
||||
|
||||
return variable && (
|
||||
!variable.hoistable &&
|
||||
!variable.global &&
|
||||
!variable.module &&
|
||||
(
|
||||
variable.referenced ||
|
||||
variable.is_reactive_dependency ||
|
||||
variable.export_name ||
|
||||
variable.name[0] === '$'
|
||||
)
|
||||
);
|
||||
});
|
||||
// return variable && (
|
||||
// !variable.hoistable &&
|
||||
// !variable.global &&
|
||||
// !variable.module &&
|
||||
// (
|
||||
// variable.referenced ||
|
||||
// variable.is_reactive_dependency ||
|
||||
// variable.export_name ||
|
||||
// variable.name[0] === '$'
|
||||
// )
|
||||
// );
|
||||
// });
|
||||
|
||||
if (head) {
|
||||
component.has_reactive_assignments = true;
|
||||
// TODO
|
||||
|
||||
if (node.operator === '=' && nodes_match(node.left, node.right) && tail.length === 0) {
|
||||
code.overwrite(node.start, node.end, component.invalidate(head));
|
||||
} else {
|
||||
let suffix = ')';
|
||||
// if (head) {
|
||||
// component.has_reactive_assignments = true;
|
||||
|
||||
if (head[0] === '$') {
|
||||
code.prependRight(node.start, `${component.helper('set_store_value')}(${head.slice(1)}, `);
|
||||
} else {
|
||||
let prefix = `$$invalidate`;
|
||||
// if (node.operator === '=' && nodes_match(node.left, node.right) && tail.length === 0) {
|
||||
// code.overwrite(node.start, node.end, component.invalidate(head));
|
||||
// } else {
|
||||
// let suffix = ')';
|
||||
|
||||
const variable = component.var_lookup.get(head);
|
||||
if (variable.subscribable && variable.reassigned) {
|
||||
prefix = `$$subscribe_${head}($$invalidate`;
|
||||
suffix += `)`;
|
||||
}
|
||||
// if (head[0] === '$') {
|
||||
// code.prependRight(node.start, `${component.helper('set_store_value')}(${head.slice(1)}, `);
|
||||
// } else {
|
||||
// let prefix = `$$invalidate`;
|
||||
|
||||
code.prependRight(node.start, `${prefix}('${head}', `);
|
||||
}
|
||||
// const variable = component.var_lookup.get(head);
|
||||
// if (variable.subscribable && variable.reassigned) {
|
||||
// prefix = `$$subscribe_${head}($$invalidate`;
|
||||
// suffix += `)`;
|
||||
// }
|
||||
|
||||
const extra_args = tail.map(name => component.invalidate(name));
|
||||
// code.prependRight(node.start, `${prefix}('${head}', `);
|
||||
// }
|
||||
|
||||
const pass_value = (
|
||||
extra_args.length > 0 ||
|
||||
(node.type === 'AssignmentExpression' && node.left.type !== 'Identifier') ||
|
||||
(node.type === 'UpdateExpression' && !node.prefix)
|
||||
);
|
||||
// const extra_args = tail.map(name => component.invalidate(name));
|
||||
|
||||
if (pass_value) {
|
||||
extra_args.unshift(head);
|
||||
}
|
||||
// const pass_value = (
|
||||
// extra_args.length > 0 ||
|
||||
// (node.type === 'AssignmentExpression' && node.left.type !== 'Identifier') ||
|
||||
// (node.type === 'UpdateExpression' && !node.prefix)
|
||||
// );
|
||||
|
||||
suffix = `${extra_args.map(arg => `, ${arg}`).join('')}${suffix}`;
|
||||
// if (pass_value) {
|
||||
// extra_args.unshift(head);
|
||||
// }
|
||||
|
||||
code.appendLeft(node.end, suffix);
|
||||
}
|
||||
}
|
||||
// suffix = `${extra_args.map(arg => `, ${arg}`).join('')}${suffix}`;
|
||||
|
||||
// code.appendLeft(node.end, suffix);
|
||||
// }
|
||||
// }
|
||||
}
|
@ -1,57 +0,0 @@
|
||||
import MagicString from 'magic-string';
|
||||
import { Node } from '../interfaces';
|
||||
import { walk } from 'estree-walker';
|
||||
import repeat from './repeat';
|
||||
|
||||
export function remove_indentation(code: MagicString, node: Node) {
|
||||
const indent = code.getIndentString();
|
||||
const pattern = new RegExp(`^${indent}`, 'gm');
|
||||
|
||||
const excluded = [];
|
||||
|
||||
walk(node, {
|
||||
enter(node) {
|
||||
if (node.type === 'TemplateElement') {
|
||||
excluded.push(node);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const str = code.original.slice(node.start, node.end);
|
||||
|
||||
let match;
|
||||
while (match = pattern.exec(str)) {
|
||||
const index = node.start + match.index;
|
||||
while (excluded[0] && excluded[0].end < index) excluded.shift();
|
||||
if (excluded[0] && excluded[0].start < index) continue;
|
||||
|
||||
code.remove(index, index + indent.length);
|
||||
}
|
||||
}
|
||||
|
||||
export function add_indentation(code: MagicString, node: Node, levels = 1) {
|
||||
const base_indent = code.getIndentString();
|
||||
const indent = repeat(base_indent, levels);
|
||||
const pattern = /\n/gm;
|
||||
|
||||
const excluded = [];
|
||||
|
||||
walk(node, {
|
||||
enter(node) {
|
||||
if (node.type === 'TemplateElement') {
|
||||
excluded.push(node);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const str = code.original.slice(node.start, node.end);
|
||||
|
||||
let match;
|
||||
while (match = pattern.exec(str)) {
|
||||
const index = node.start + match.index;
|
||||
while (excluded[0] && excluded[0].end < index) excluded.shift();
|
||||
if (excluded[0] && excluded[0].start < index) continue;
|
||||
|
||||
code.appendLeft(index + 1, indent);
|
||||
}
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
<script>
|
||||
let name = 'world';
|
||||
export let name = 'world';
|
||||
</script>
|
||||
|
||||
<h1>Hello {name}!</h1>
|
Loading…
Reference in new issue