refactor(core): static-query migration should always use template strategy (#30628)

We are removing the prompt for the `static-query` migration and make the
template strategy the migration strategy for the migration. The usage
strategy is good for best-practices, but for now we want to ensure that
the migration is a seamless as possible and that is only achievable my
re-using the same logic that View Engine uses for determining the
timing of a query.

PR Close #30628
This commit is contained in:
Paul Gschwendtner 2019-05-23 00:55:18 +02:00 committed by Jason Aden
parent faac51fd2e
commit 5640cedc82
3 changed files with 12 additions and 73 deletions

View File

@ -21,9 +21,14 @@ import {QueryTemplateStrategy} from './strategies/template_strategy/template_str
import {QueryTestStrategy} from './strategies/test_strategy/test_strategy';
import {TimingStrategy} from './strategies/timing-strategy';
import {QueryUsageStrategy} from './strategies/usage_strategy/usage_strategy';
import {SELECTED_STRATEGY, promptForMigrationStrategy} from './strategy_prompt';
import {getTransformedQueryCallExpr} from './transform';
enum SELECTED_STRATEGY {
TEMPLATE,
USAGE,
TESTS,
}
interface AnalyzedProject {
program: ts.Program;
host: ts.CompilerHost;
@ -50,8 +55,9 @@ async function runMigration(tree: Tree, context: SchematicContext) {
const logger = context.logger;
logger.info('------ Static Query migration ------');
logger.info('In preparation for Ivy, developers can now explicitly specify the');
logger.info('timing of their queries. Read more about this here:');
logger.info('With Angular version 8, developers need to');
logger.info('explicitly specify the timing of ViewChild or');
logger.info('ContentChild queries. Read more about this here:');
logger.info('https://github.com/angular/angular/pull/28810');
if (!buildPaths.length && !testPaths.length) {
@ -63,6 +69,9 @@ async function runMigration(tree: Tree, context: SchematicContext) {
const analyzedFiles = new Set<string>();
const buildProjects = new Set<AnalyzedProject>();
const failures = [];
const strategy = process.env['NG_STATIC_QUERY_USAGE_STRATEGY'] === 'true' ?
SELECTED_STRATEGY.USAGE :
SELECTED_STRATEGY.TEMPLATE;
for (const tsconfigPath of buildPaths) {
const project = analyzeProject(tree, tsconfigPath, basePath, analyzedFiles);
@ -71,10 +80,7 @@ async function runMigration(tree: Tree, context: SchematicContext) {
}
}
// In case there are projects which contain queries that need to be migrated,
// we want to prompt for the migration strategy and run the migration.
if (buildProjects.size) {
const strategy = await promptForMigrationStrategy(logger);
for (let project of Array.from(buildProjects.values())) {
failures.push(...await runStaticQueryMigration(tree, project, strategy, logger));
}

View File

@ -1,50 +0,0 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {logging} from '@angular-devkit/core';
import {getInquirer, supportsPrompt} from '../../utils/schematics_prompt';
export enum SELECTED_STRATEGY {
TEMPLATE,
USAGE,
TESTS,
}
/**
* Prompts the user for the migration strategy that should be used. Defaults to the
* template strategy as it provides a migration with rare manual corrections.
* */
export async function promptForMigrationStrategy(logger: logging.LoggerApi) {
if (supportsPrompt()) {
logger.info('There are two available migration strategies that can be selected:');
logger.info(' • Template strategy - migration tool (short-term gains, rare corrections)');
logger.info(' • Usage strategy - best practices (long-term gains, manual corrections)');
logger.info('For an easy migration, the template strategy is recommended. The usage');
logger.info('strategy can be used for best practices and a code base that will be more');
logger.info('flexible to changes going forward.');
const {strategyName} = await getInquirer().prompt<{strategyName: string}>({
type: 'list',
name: 'strategyName',
message: 'What migration strategy do you want to use?',
choices: [
{name: 'Template strategy', value: 'template'}, {name: 'Usage strategy', value: 'usage'}
],
default: 'template',
});
logger.info('');
return strategyName === 'usage' ? SELECTED_STRATEGY.USAGE : SELECTED_STRATEGY.TEMPLATE;
} else {
// In case prompts are not supported, we still want to allow developers to opt
// into the usage strategy by specifying an environment variable. The tests also
// use the environment variable as there is no headless way to select via prompt.
return !!process.env['NG_STATIC_QUERY_USAGE_STRATEGY'] ? SELECTED_STRATEGY.USAGE :
SELECTED_STRATEGY.TEMPLATE;
}
}

View File

@ -1506,23 +1506,6 @@ describe('static-queries migration with usage strategy', () => {
.toContain(`@${queryType}('test', { static: false }) query: any;`);
});
it(`should not prompt for migration strategy if no @${queryType} query is used`, async() => {
writeFile('/index.ts', `
import {Component, ${queryType}} from '@angular/core';
@Component({template: '<span #test></span>'})
export class NoQueriesDeclared {
}
`);
const testModule = require('../migrations/static-queries/strategy_prompt');
spyOn(testModule, 'promptForMigrationStrategy').and.callThrough();
await runMigration();
expect(testModule.promptForMigrationStrategy).toHaveBeenCalledTimes(0);
});
it('should support function call with default parameter value', async() => {
writeFile('/index.ts', `
import {Component, ${queryType}} from '@angular/core';