improve static considertaion for contextual variable

pull/7426/head
tanhauhau 4 years ago
parent 2aab40cc55
commit ff179a7b3d

@ -197,6 +197,15 @@ export default class Expression {
}); });
} }
dynamic_contextual_dependencies() {
return Array.from(this.contextual_dependencies).filter(name => {
return Array.from(this.template_scope.dependencies_for_name.get(name)).some(variable_name => {
const variable = this.component.var_lookup.get(variable_name);
return is_dynamic(variable);
});
});
}
// TODO move this into a render-dom wrapper? // TODO move this into a render-dom wrapper?
manipulate(block?: Block, ctx?: string | void) { manipulate(block?: Block, ctx?: string | void) {
// TODO ideally we wouldn't end up calling this method // TODO ideally we wouldn't end up calling this method

@ -19,7 +19,7 @@ export default class Tag extends Node {
); );
} }
is_dependencies_static() { is_dependencies_static() {
return this.expression.contextual_dependencies.size === 0 && this.expression.dynamic_dependencies().length === 0; return this.expression.dynamic_contextual_dependencies().length === 0 && this.expression.dynamic_dependencies().length === 0;
} }
check_if_content_dynamic() { check_if_content_dynamic() {
if (!this.is_dependencies_static()) { if (!this.is_dependencies_static()) {

@ -4,7 +4,7 @@ import glob from 'tiny-glob/sync';
import * as path from 'path'; import * as path from 'path';
import * as fs from 'fs'; import * as fs from 'fs';
import * as colors from 'kleur'; import * as colors from 'kleur';
export const assert = (assert$1 as unknown) as typeof assert$1 & { htmlEqual: (actual, expected, message?) => void, htmlEqualWithOptions: (actual, expected, options, message?) => void }; 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 };
// for coverage purposes, we need to test source files, // for coverage purposes, we need to test source files,
// but for sanity purposes, we need to test dist files // but for sanity purposes, we need to test dist files
@ -172,7 +172,7 @@ export function setupHtmlEqual(options: { removeDataSvelte?: boolean } = {}) {
); );
}; };
// eslint-disable-next-line no-import-assign // eslint-disable-next-line no-import-assign
assert.htmlEqualWithOptions = (actual, expected, { preserveComments, withoutNormalizeHtml }, message) => { assert.htmlEqualWithOptions = (actual: string, expected: string, { preserveComments, withoutNormalizeHtml }: { preserveComments?: boolean, withoutNormalizeHtml?: boolean }, message?: string) => {
assert.deepEqual( assert.deepEqual(
withoutNormalizeHtml withoutNormalizeHtml
? normalizeNewline(actual).replace(/(\sdata-svelte="[^"]+")/g, options.removeDataSvelte ? '' : '$1') ? normalizeNewline(actual).replace(/(\sdata-svelte="[^"]+")/g, options.removeDataSvelte ? '' : '$1')

@ -27,7 +27,6 @@ function get_each_context(ctx, list, i) {
function create_each_block(ctx) { function create_each_block(ctx) {
let div; let div;
let strong; let strong;
let t0;
let t1; let t1;
let span; let span;
let t2_value = /*comment*/ ctx[4].author + ""; let t2_value = /*comment*/ ctx[4].author + "";
@ -44,7 +43,7 @@ function create_each_block(ctx) {
c() { c() {
div = element("div"); div = element("div");
strong = element("strong"); strong = element("strong");
t0 = text(/*i*/ ctx[6]); strong.textContent = `${/*i*/ ctx[6]}`;
t1 = space(); t1 = space();
span = element("span"); span = element("span");
t2 = text(t2_value); t2 = text(t2_value);
@ -60,7 +59,6 @@ function create_each_block(ctx) {
m(target, anchor) { m(target, anchor) {
insert(target, div, anchor); insert(target, div, anchor);
append(div, strong); append(div, strong);
append(strong, t0);
append(div, t1); append(div, t1);
append(div, span); append(div, span);
append(span, t2); append(span, t2);

@ -8,9 +8,9 @@ export default {
async test({ assert, target }) { async test({ assert, target }) {
const firstSpanList = target.children[0]; const firstSpanList = target.children[0];
assert.equal(firstSpanList.innerHTML, expected); assert.htmlEqualWithOptions(firstSpanList.innerHTML, expected, { withoutNormalizeHtml: true });
const secondSpanList = target.children[1]; const secondSpanList = target.children[1];
assert.equal(secondSpanList.innerHTML, expected); assert.htmlEqualWithOptions(secondSpanList.innerHTML, expected, { withoutNormalizeHtml: true });
} }
}; };

@ -11,7 +11,7 @@ export default {
const p = target.querySelector('p'); const p = target.querySelector('p');
component.raw = '<p>does not change</p>'; component.raw = '<p>does not change</p>';
assert.htmlEqual(target.innerHTML, '<div><p>does not change</p></div>'); assert.htmlEqualWithOptions(target.innerHTML, '<div><p>does not change</p></div>', { withoutNormalizeHtml: true });
assert.strictEqual(target.querySelector('p'), p); assert.strictEqual(target.querySelector('p'), p);
} }
}; };

Loading…
Cancel
Save