From c2ed80b09ef0fcc8ce41d5f1a90646e0c79beec0 Mon Sep 17 00:00:00 2001 From: tanhauhau Date: Sat, 16 Apr 2022 08:27:10 +0800 Subject: [PATCH] improve static considertaion for contextual variable --- src/compiler/compile/nodes/shared/Expression.ts | 9 +++++++++ src/compiler/compile/nodes/shared/Tag.ts | 2 +- test/helpers.ts | 4 ++-- test/js/samples/each-block-changed-check/expected.js | 4 +--- .../samples/fragment-trailing-whitespace/_config.js | 4 ++-- test/runtime/samples/raw-mustaches-preserved/_config.js | 2 +- 6 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/compiler/compile/nodes/shared/Expression.ts b/src/compiler/compile/nodes/shared/Expression.ts index a773355e3117..8914f3868628 100644 --- a/src/compiler/compile/nodes/shared/Expression.ts +++ b/src/compiler/compile/nodes/shared/Expression.ts @@ -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 diff --git a/src/compiler/compile/nodes/shared/Tag.ts b/src/compiler/compile/nodes/shared/Tag.ts index afb09d83ced8..23d94ce01e02 100644 --- a/src/compiler/compile/nodes/shared/Tag.ts +++ b/src/compiler/compile/nodes/shared/Tag.ts @@ -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()) { diff --git a/test/helpers.ts b/test/helpers.ts index 6498619e17a7..ce3571344750 100644 --- a/test/helpers.ts +++ b/test/helpers.ts @@ -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 @@ -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') diff --git a/test/js/samples/each-block-changed-check/expected.js b/test/js/samples/each-block-changed-check/expected.js index 19947edbbb26..c070f733e9fb 100644 --- a/test/js/samples/each-block-changed-check/expected.js +++ b/test/js/samples/each-block-changed-check/expected.js @@ -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 + ""; @@ -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); @@ -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); diff --git a/test/runtime/samples/fragment-trailing-whitespace/_config.js b/test/runtime/samples/fragment-trailing-whitespace/_config.js index 549cb1be68ea..f9ba16207cde 100644 --- a/test/runtime/samples/fragment-trailing-whitespace/_config.js +++ b/test/runtime/samples/fragment-trailing-whitespace/_config.js @@ -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 }); } }; diff --git a/test/runtime/samples/raw-mustaches-preserved/_config.js b/test/runtime/samples/raw-mustaches-preserved/_config.js index 184bb6fe4d0b..3bb61208b938 100644 --- a/test/runtime/samples/raw-mustaches-preserved/_config.js +++ b/test/runtime/samples/raw-mustaches-preserved/_config.js @@ -11,7 +11,7 @@ export default { const p = target.querySelector('p'); component.raw = '

does not change

'; - assert.htmlEqual(target.innerHTML, '

does not change

'); + assert.htmlEqualWithOptions(target.innerHTML, '

does not change

', { withoutNormalizeHtml: true }); assert.strictEqual(target.querySelector('p'), p); } };