Skip to content

Commit

Permalink
fix #5427 - do not declare variable for member assignment in reactive…
Browse files Browse the repository at this point in the history
… declaration
  • Loading branch information
tanhauhau committed Aug 8, 2020
1 parent 1398aff commit ab85c2b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 28 deletions.
39 changes: 11 additions & 28 deletions src/compiler/compile/render_ssr/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import { string_literal } from '../utils/stringify';
import Renderer from './Renderer';
import { INode as TemplateNode } from '../nodes/interfaces'; // TODO
import Text from '../nodes/Text';
import { extract_names } from '../utils/scope';
import { LabeledStatement, Statement, ExpressionStatement, AssignmentExpression, Node } from 'estree';
import { LabeledStatement, Statement, Node } from 'estree';

export default function ssr(
component: Component,
Expand Down Expand Up @@ -72,37 +71,17 @@ export default function ssr(
})
: [];

const injected = Array.from(component.injected_reactive_declaration_vars).filter(name => {
const variable = component.var_lookup.get(name);
return variable.injected;
});

const reactive_declarations = component.reactive_declarations.map(d => {
const body: Statement = (d.node as LabeledStatement).body;

let statement = b`${body}`;

if (d.declaration) {
const declared = extract_names(d.declaration);
const injected = declared.filter(name => {
return name[0] !== '$' && component.var_lookup.get(name).injected;
});

const self_dependencies = injected.filter(name => d.dependencies.has(name));

if (injected.length) {
// in some cases we need to do `let foo; [expression]`, in
// others we can do `let [expression]`
const separate = (
self_dependencies.length > 0 ||
declared.length > injected.length
);

const { left, right } = (body as ExpressionStatement).expression as AssignmentExpression;

statement = separate
? b`
${injected.map(name => b`let ${name};`)}
${statement}`
: b`
let ${left} = ${right}`;
}
} else { // TODO do not add label if it's not referenced
if (!d.declaration) { // TODO do not add label if it's not referenced
statement = b`$: { ${statement} }`;
}

Expand All @@ -119,6 +98,8 @@ export default function ssr(
${reactive_store_values}
${injected.map(name => b`let ${name};`)}
${reactive_declarations}
$$rendered = ${literal};
Expand All @@ -129,6 +110,8 @@ export default function ssr(
: b`
${reactive_store_values}
${injected.map(name => b`let ${name};`)}
${reactive_declarations}
return ${literal};`;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
html: `
<h1>Hello world!</h1>
`,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<script>
$: user = {};
$: user.name = 'world';
</script>

<h1>Hello {user.name}!</h1>

0 comments on commit ab85c2b

Please sign in to comment.