Skip to content

Commit

Permalink
improve static considertaion for contextual variable
Browse files Browse the repository at this point in the history
  • Loading branch information
tanhauhau committed Apr 16, 2022
1 parent 563fc88 commit c2ed80b
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 9 deletions.
9 changes: 9 additions & 0 deletions src/compiler/compile/nodes/shared/Expression.ts
Expand Up @@ -175,6 +175,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?
manipulate(block?: Block, ctx?: string | void) {
// TODO ideally we wouldn't end up calling this method
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/compile/nodes/shared/Tag.ts
Expand Up @@ -19,7 +19,7 @@ export default class Tag extends Node {
);
}
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() {
if (!this.is_dependencies_static()) {
Expand Down
4 changes: 2 additions & 2 deletions test/helpers.ts
Expand Up @@ -4,7 +4,7 @@ import glob from 'tiny-glob/sync';
import * as path from 'path';
import * as fs from 'fs';
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,
// but for sanity purposes, we need to test dist files
Expand Down Expand Up @@ -172,7 +172,7 @@ export function setupHtmlEqual(options: { removeDataSvelte?: boolean } = {}) {
);
};
// 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(
withoutNormalizeHtml
? normalizeNewline(actual).replace(/(\sdata-svelte="[^"]+")/g, options.removeDataSvelte ? '' : '$1')
Expand Down
4 changes: 1 addition & 3 deletions test/js/samples/each-block-changed-check/expected.js
Expand Up @@ -27,7 +27,6 @@ function get_each_context(ctx, list, i) {
function create_each_block(ctx) {
let div;
let strong;
let t0;
let t1;
let span;
let t2_value = /*comment*/ ctx[4].author + "";
Expand All @@ -44,7 +43,7 @@ function create_each_block(ctx) {
c() {
div = element("div");
strong = element("strong");
t0 = text(/*i*/ ctx[6]);
strong.textContent = `${/*i*/ ctx[6]}`;
t1 = space();
span = element("span");
t2 = text(t2_value);
Expand All @@ -60,7 +59,6 @@ function create_each_block(ctx) {
m(target, anchor) {
insert(target, div, anchor);
append(div, strong);
append(strong, t0);
append(div, t1);
append(div, span);
append(span, t2);
Expand Down
4 changes: 2 additions & 2 deletions test/runtime/samples/fragment-trailing-whitespace/_config.js
Expand Up @@ -8,9 +8,9 @@ export default {

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

const secondSpanList = target.children[1];
assert.equal(secondSpanList.innerHTML, expected);
assert.htmlEqualWithOptions(secondSpanList.innerHTML, expected, { withoutNormalizeHtml: true });
}
};
2 changes: 1 addition & 1 deletion test/runtime/samples/raw-mustaches-preserved/_config.js
Expand Up @@ -11,7 +11,7 @@ export default {
const p = target.querySelector('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);
}
};

0 comments on commit c2ed80b

Please sign in to comment.