From 7f6429d802f0fbebd3b8d3489df86912b5b779b0 Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Fri, 4 Oct 2019 09:38:51 +0200 Subject: [PATCH] refactor: re-enable dynamic queries migration (#32992) Re-enables the dynamic queries migration, now that we have all of the necessary framework changes in place. Also moves the logic that identifies static queries out of the compiler and into the static queries migration, because that's the only place left that's using it. PR Close #32992 --- packages/compiler/src/compiler.ts | 2 +- .../src/view_compiler/view_compiler.ts | 51 ------------------ packages/core/schematics/migrations.json | 5 ++ .../template_strategy/template_strategy.ts | 54 ++++++++++++++++++- packages/core/schematics/test/BUILD.bazel | 1 - .../test/dynamic_queries_migration_spec.ts | 2 +- .../core/schematics/test/test-migrations.json | 9 ---- 7 files changed, 60 insertions(+), 64 deletions(-) delete mode 100644 packages/core/schematics/test/test-migrations.json diff --git a/packages/compiler/src/compiler.ts b/packages/compiler/src/compiler.ts index ae81738dfe..98831a3127 100644 --- a/packages/compiler/src/compiler.ts +++ b/packages/compiler/src/compiler.ts @@ -86,7 +86,7 @@ export * from './schema/dom_element_schema_registry'; export * from './selector'; export * from './style_compiler'; export * from './template_parser/template_parser'; -export {ViewCompiler, findStaticQueryIds, staticViewQueryIds} from './view_compiler/view_compiler'; +export {ViewCompiler} from './view_compiler/view_compiler'; export {getParseErrors, isSyntaxError, syntaxError, Version} from './util'; export {SourceMap} from './output/source_map'; export * from './injectable_compiler_2'; diff --git a/packages/compiler/src/view_compiler/view_compiler.ts b/packages/compiler/src/view_compiler/view_compiler.ts index b3cfd9eff7..a0ede7a73e 100644 --- a/packages/compiler/src/view_compiler/view_compiler.ts +++ b/packages/compiler/src/view_compiler/view_compiler.ts @@ -1013,57 +1013,6 @@ function callUnwrapValue(nodeIndex: number, bindingIdx: number, expr: o.Expressi ]); } -interface StaticAndDynamicQueryIds { - staticQueryIds: Set; - dynamicQueryIds: Set; -} - - -export function findStaticQueryIds( - nodes: TemplateAst[], result = new Map()): - Map { - nodes.forEach((node) => { - const staticQueryIds = new Set(); - const dynamicQueryIds = new Set(); - let queryMatches: QueryMatch[] = undefined !; - if (node instanceof ElementAst) { - findStaticQueryIds(node.children, result); - node.children.forEach((child) => { - const childData = result.get(child) !; - childData.staticQueryIds.forEach(queryId => staticQueryIds.add(queryId)); - childData.dynamicQueryIds.forEach(queryId => dynamicQueryIds.add(queryId)); - }); - queryMatches = node.queryMatches; - } else if (node instanceof EmbeddedTemplateAst) { - findStaticQueryIds(node.children, result); - node.children.forEach((child) => { - const childData = result.get(child) !; - childData.staticQueryIds.forEach(queryId => dynamicQueryIds.add(queryId)); - childData.dynamicQueryIds.forEach(queryId => dynamicQueryIds.add(queryId)); - }); - queryMatches = node.queryMatches; - } - if (queryMatches) { - queryMatches.forEach((match) => staticQueryIds.add(match.queryId)); - } - dynamicQueryIds.forEach(queryId => staticQueryIds.delete(queryId)); - result.set(node, {staticQueryIds, dynamicQueryIds}); - }); - return result; -} - -export function staticViewQueryIds(nodeStaticQueryIds: Map): - StaticAndDynamicQueryIds { - const staticQueryIds = new Set(); - const dynamicQueryIds = new Set(); - Array.from(nodeStaticQueryIds.values()).forEach((entry) => { - entry.staticQueryIds.forEach(queryId => staticQueryIds.add(queryId)); - entry.dynamicQueryIds.forEach(queryId => dynamicQueryIds.add(queryId)); - }); - dynamicQueryIds.forEach(queryId => staticQueryIds.delete(queryId)); - return {staticQueryIds, dynamicQueryIds}; -} - function elementEventNameAndTarget( eventAst: BoundEventAst, dirAst: DirectiveAst | null): {name: string, target: string | null} { if (eventAst.isAnimation) { diff --git a/packages/core/schematics/migrations.json b/packages/core/schematics/migrations.json index aac7cb8d82..30fd5090c3 100644 --- a/packages/core/schematics/migrations.json +++ b/packages/core/schematics/migrations.json @@ -34,6 +34,11 @@ "version": "9-beta", "description": "Adds an Angular decorator to undecorated classes that have decorated fields", "factory": "./migrations/undecorated-classes-with-decorated-fields/index" + }, + "migration-v9-dynamic-queries": { + "version": "9-beta", + "description": "Removes the `static` flag from dynamic queries.", + "factory": "./migrations/dynamic-queries/index" } } } diff --git a/packages/core/schematics/migrations/static-queries/strategies/template_strategy/template_strategy.ts b/packages/core/schematics/migrations/static-queries/strategies/template_strategy/template_strategy.ts index ba354de43a..c614961b67 100644 --- a/packages/core/schematics/migrations/static-queries/strategies/template_strategy/template_strategy.ts +++ b/packages/core/schematics/migrations/static-queries/strategies/template_strategy/template_strategy.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {AotCompiler, CompileDirectiveMetadata, CompileMetadataResolver, CompileNgModuleMetadata, CompileStylesheetMetadata, NgAnalyzedModules, StaticSymbol, TemplateAst, findStaticQueryIds, staticViewQueryIds} from '@angular/compiler'; +import {AotCompiler, CompileDirectiveMetadata, CompileMetadataResolver, CompileNgModuleMetadata, CompileStylesheetMetadata, ElementAst, EmbeddedTemplateAst, NgAnalyzedModules, QueryMatch, StaticSymbol, TemplateAst} from '@angular/compiler'; import {Diagnostic, createProgram, readConfiguration} from '@angular/compiler-cli'; import {resolve} from 'path'; import * as ts from 'typescript'; @@ -188,3 +188,55 @@ export class QueryTemplateStrategy implements TimingStrategy { return `${resolve(filePath)}#${className}-${propName}`; } } + +interface StaticAndDynamicQueryIds { + staticQueryIds: Set; + dynamicQueryIds: Set; +} + +/** Figures out which queries are static and which ones are dynamic. */ +function findStaticQueryIds( + nodes: TemplateAst[], result = new Map()): + Map { + nodes.forEach((node) => { + const staticQueryIds = new Set(); + const dynamicQueryIds = new Set(); + let queryMatches: QueryMatch[] = undefined !; + if (node instanceof ElementAst) { + findStaticQueryIds(node.children, result); + node.children.forEach((child) => { + const childData = result.get(child) !; + childData.staticQueryIds.forEach(queryId => staticQueryIds.add(queryId)); + childData.dynamicQueryIds.forEach(queryId => dynamicQueryIds.add(queryId)); + }); + queryMatches = node.queryMatches; + } else if (node instanceof EmbeddedTemplateAst) { + findStaticQueryIds(node.children, result); + node.children.forEach((child) => { + const childData = result.get(child) !; + childData.staticQueryIds.forEach(queryId => dynamicQueryIds.add(queryId)); + childData.dynamicQueryIds.forEach(queryId => dynamicQueryIds.add(queryId)); + }); + queryMatches = node.queryMatches; + } + if (queryMatches) { + queryMatches.forEach((match) => staticQueryIds.add(match.queryId)); + } + dynamicQueryIds.forEach(queryId => staticQueryIds.delete(queryId)); + result.set(node, {staticQueryIds, dynamicQueryIds}); + }); + return result; +} + +/** Splits queries into static and dynamic. */ +function staticViewQueryIds(nodeStaticQueryIds: Map): + StaticAndDynamicQueryIds { + const staticQueryIds = new Set(); + const dynamicQueryIds = new Set(); + Array.from(nodeStaticQueryIds.values()).forEach((entry) => { + entry.staticQueryIds.forEach(queryId => staticQueryIds.add(queryId)); + entry.dynamicQueryIds.forEach(queryId => dynamicQueryIds.add(queryId)); + }); + dynamicQueryIds.forEach(queryId => staticQueryIds.delete(queryId)); + return {staticQueryIds, dynamicQueryIds}; +} diff --git a/packages/core/schematics/test/BUILD.bazel b/packages/core/schematics/test/BUILD.bazel index f924396f97..c14f4c8010 100644 --- a/packages/core/schematics/test/BUILD.bazel +++ b/packages/core/schematics/test/BUILD.bazel @@ -5,7 +5,6 @@ ts_library( testonly = True, srcs = glob(["**/*.ts"]), data = [ - "test-migrations.json", "//packages/core/schematics:migrations.json", ], deps = [ diff --git a/packages/core/schematics/test/dynamic_queries_migration_spec.ts b/packages/core/schematics/test/dynamic_queries_migration_spec.ts index d662024a75..31644d345f 100644 --- a/packages/core/schematics/test/dynamic_queries_migration_spec.ts +++ b/packages/core/schematics/test/dynamic_queries_migration_spec.ts @@ -20,7 +20,7 @@ describe('dynamic queries migration', () => { let previousWorkingDir: string; beforeEach(() => { - runner = new SchematicTestRunner('test', require.resolve('./test-migrations.json')); + runner = new SchematicTestRunner('test', require.resolve('../migrations.json')); host = new TempScopedNodeJsSyncHost(); tree = new UnitTestTree(new HostTree(host)); diff --git a/packages/core/schematics/test/test-migrations.json b/packages/core/schematics/test/test-migrations.json deleted file mode 100644 index 684a6073e3..0000000000 --- a/packages/core/schematics/test/test-migrations.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "schematics": { - "migration-v9-dynamic-queries": { - "version": "9-beta", - "description": "Removes the `static` flag from dynamic queries.", - "factory": "../migrations/dynamic-queries/index" - } - } -}