mirror of https://github.com/sveltejs/svelte
parent
1d8d17bc07
commit
50280232e8
@ -1,66 +1,67 @@
|
|||||||
import Component from '../Component';
|
import Component from '../Component';
|
||||||
import MagicString from 'magic-string';
|
|
||||||
import { Node } from '../../interfaces';
|
import { Node } from '../../interfaces';
|
||||||
import { nodes_match } from '../../utils/nodes_match';
|
// import { nodes_match } from '../../utils/nodes_match';
|
||||||
import { Scope } from './scope';
|
import { Scope } from './scope';
|
||||||
|
|
||||||
export function invalidate(component: Component, scope: Scope, code: MagicString, node: Node, names: Set<string>) {
|
export function invalidate(_component: Component, _scope: Scope, _node: Node, _names: Set<string>) {
|
||||||
const [head, ...tail] = Array.from(names).filter(name => {
|
// const [head, ...tail] = Array.from(names).filter(name => {
|
||||||
const owner = scope.find_owner(name);
|
// const owner = scope.find_owner(name);
|
||||||
if (owner && owner !== component.instance_scope) return false;
|
// if (owner && owner !== component.instance_scope) return false;
|
||||||
|
|
||||||
const variable = component.var_lookup.get(name);
|
// const variable = component.var_lookup.get(name);
|
||||||
|
|
||||||
return variable && (
|
// return variable && (
|
||||||
!variable.hoistable &&
|
// !variable.hoistable &&
|
||||||
!variable.global &&
|
// !variable.global &&
|
||||||
!variable.module &&
|
// !variable.module &&
|
||||||
(
|
// (
|
||||||
variable.referenced ||
|
// variable.referenced ||
|
||||||
variable.is_reactive_dependency ||
|
// variable.is_reactive_dependency ||
|
||||||
variable.export_name ||
|
// variable.export_name ||
|
||||||
variable.name[0] === '$'
|
// variable.name[0] === '$'
|
||||||
)
|
// )
|
||||||
);
|
// );
|
||||||
});
|
// });
|
||||||
|
|
||||||
if (head) {
|
// TODO
|
||||||
component.has_reactive_assignments = true;
|
|
||||||
|
|
||||||
if (node.operator === '=' && nodes_match(node.left, node.right) && tail.length === 0) {
|
// if (head) {
|
||||||
code.overwrite(node.start, node.end, component.invalidate(head));
|
// component.has_reactive_assignments = true;
|
||||||
} else {
|
|
||||||
let suffix = ')';
|
|
||||||
|
|
||||||
if (head[0] === '$') {
|
// if (node.operator === '=' && nodes_match(node.left, node.right) && tail.length === 0) {
|
||||||
code.prependRight(node.start, `${component.helper('set_store_value')}(${head.slice(1)}, `);
|
// code.overwrite(node.start, node.end, component.invalidate(head));
|
||||||
} else {
|
// } else {
|
||||||
let prefix = `$$invalidate`;
|
// let suffix = ')';
|
||||||
|
|
||||||
const variable = component.var_lookup.get(head);
|
// if (head[0] === '$') {
|
||||||
if (variable.subscribable && variable.reassigned) {
|
// code.prependRight(node.start, `${component.helper('set_store_value')}(${head.slice(1)}, `);
|
||||||
prefix = `$$subscribe_${head}($$invalidate`;
|
// } else {
|
||||||
suffix += `)`;
|
// 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 = (
|
// const extra_args = tail.map(name => component.invalidate(name));
|
||||||
extra_args.length > 0 ||
|
|
||||||
(node.type === 'AssignmentExpression' && node.left.type !== 'Identifier') ||
|
|
||||||
(node.type === 'UpdateExpression' && !node.prefix)
|
|
||||||
);
|
|
||||||
|
|
||||||
if (pass_value) {
|
// const pass_value = (
|
||||||
extra_args.unshift(head);
|
// 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>
|
<script>
|
||||||
let name = 'world';
|
export let name = 'world';
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<h1>Hello {name}!</h1>
|
<h1>Hello {name}!</h1>
|
Loading…
Reference in new issue