Skip to content

Commit c2ed80b

Browse files
committed
improve static considertaion for contextual variable
1 parent 563fc88 commit c2ed80b

File tree

6 files changed

+16
-9
lines changed

6 files changed

+16
-9
lines changed

src/compiler/compile/nodes/shared/Expression.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,15 @@ export default class Expression {
175175
});
176176
}
177177

178+
dynamic_contextual_dependencies() {
179+
return Array.from(this.contextual_dependencies).filter(name => {
180+
return Array.from(this.template_scope.dependencies_for_name.get(name)).some(variable_name => {
181+
const variable = this.component.var_lookup.get(variable_name);
182+
return is_dynamic(variable);
183+
});
184+
});
185+
}
186+
178187
// TODO move this into a render-dom wrapper?
179188
manipulate(block?: Block, ctx?: string | void) {
180189
// TODO ideally we wouldn't end up calling this method

src/compiler/compile/nodes/shared/Tag.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export default class Tag extends Node {
1919
);
2020
}
2121
is_dependencies_static() {
22-
return this.expression.contextual_dependencies.size === 0 && this.expression.dynamic_dependencies().length === 0;
22+
return this.expression.dynamic_contextual_dependencies().length === 0 && this.expression.dynamic_dependencies().length === 0;
2323
}
2424
check_if_content_dynamic() {
2525
if (!this.is_dependencies_static()) {

test/helpers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import glob from 'tiny-glob/sync';
44
import * as path from 'path';
55
import * as fs from 'fs';
66
import * as colors from 'kleur';
7-
export const assert = (assert$1 as unknown) as typeof assert$1 & { htmlEqual: (actual, expected, message?) => void, htmlEqualWithOptions: (actual, expected, options, message?) => void };
7+
export const assert = (assert$1 as unknown) as typeof assert$1 & { htmlEqual: (actual: string, expected: string, message?: string) => void, htmlEqualWithOptions: (actual: string, expected: string, options: { preserveComments?: boolean, withoutNormalizeHtml?: boolean }, message?: string) => void };
88

99
// for coverage purposes, we need to test source files,
1010
// but for sanity purposes, we need to test dist files
@@ -172,7 +172,7 @@ export function setupHtmlEqual(options: { removeDataSvelte?: boolean } = {}) {
172172
);
173173
};
174174
// eslint-disable-next-line no-import-assign
175-
assert.htmlEqualWithOptions = (actual, expected, { preserveComments, withoutNormalizeHtml }, message) => {
175+
assert.htmlEqualWithOptions = (actual: string, expected: string, { preserveComments, withoutNormalizeHtml }: { preserveComments?: boolean, withoutNormalizeHtml?: boolean }, message?: string) => {
176176
assert.deepEqual(
177177
withoutNormalizeHtml
178178
? normalizeNewline(actual).replace(/(\sdata-svelte="[^"]+")/g, options.removeDataSvelte ? '' : '$1')

test/js/samples/each-block-changed-check/expected.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ function get_each_context(ctx, list, i) {
2727
function create_each_block(ctx) {
2828
let div;
2929
let strong;
30-
let t0;
3130
let t1;
3231
let span;
3332
let t2_value = /*comment*/ ctx[4].author + "";
@@ -44,7 +43,7 @@ function create_each_block(ctx) {
4443
c() {
4544
div = element("div");
4645
strong = element("strong");
47-
t0 = text(/*i*/ ctx[6]);
46+
strong.textContent = `${/*i*/ ctx[6]}`;
4847
t1 = space();
4948
span = element("span");
5049
t2 = text(t2_value);
@@ -60,7 +59,6 @@ function create_each_block(ctx) {
6059
m(target, anchor) {
6160
insert(target, div, anchor);
6261
append(div, strong);
63-
append(strong, t0);
6462
append(div, t1);
6563
append(div, span);
6664
append(span, t2);

test/runtime/samples/fragment-trailing-whitespace/_config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ export default {
88

99
async test({ assert, target }) {
1010
const firstSpanList = target.children[0];
11-
assert.equal(firstSpanList.innerHTML, expected);
11+
assert.htmlEqualWithOptions(firstSpanList.innerHTML, expected, { withoutNormalizeHtml: true });
1212

1313
const secondSpanList = target.children[1];
14-
assert.equal(secondSpanList.innerHTML, expected);
14+
assert.htmlEqualWithOptions(secondSpanList.innerHTML, expected, { withoutNormalizeHtml: true });
1515
}
1616
};

test/runtime/samples/raw-mustaches-preserved/_config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default {
1111
const p = target.querySelector('p');
1212

1313
component.raw = '<p>does not change</p>';
14-
assert.htmlEqual(target.innerHTML, '<div><p>does not change</p></div>');
14+
assert.htmlEqualWithOptions(target.innerHTML, '<div><p>does not change</p></div>', { withoutNormalizeHtml: true });
1515
assert.strictEqual(target.querySelector('p'), p);
1616
}
1717
};

0 commit comments

Comments
 (0)