2019-03-30 07:48:21 -04:00
|
|
|
/**
|
|
|
|
* @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, normalize} from '@angular-devkit/core';
|
|
|
|
import {Rule, SchematicContext, SchematicsException, Tree} from '@angular-devkit/schematics';
|
|
|
|
import {dirname, relative} from 'path';
|
|
|
|
import * as ts from 'typescript';
|
|
|
|
|
2019-04-08 10:16:56 -04:00
|
|
|
import {NgComponentTemplateVisitor} from '../../utils/ng_component_template';
|
2019-03-30 07:48:21 -04:00
|
|
|
import {getProjectTsConfigPaths} from '../../utils/project_tsconfig_paths';
|
|
|
|
import {parseTsconfigFile} from '../../utils/typescript/parse_tsconfig';
|
2019-04-08 10:16:56 -04:00
|
|
|
import {visitAllNodes} from '../../utils/typescript/visit_nodes';
|
2019-03-30 07:48:21 -04:00
|
|
|
|
|
|
|
import {analyzeResolvedTemplate} from './analyze_template';
|
|
|
|
|
|
|
|
type Logger = logging.LoggerApi;
|
|
|
|
|
2019-04-04 06:52:29 -04:00
|
|
|
const README_URL =
|
|
|
|
'https://github.com/angular/angular/tree/master/packages/core/schematics/migrations/template-var-assignment/README.md';
|
|
|
|
const FAILURE_MESSAGE = `Found assignment to template variable.`;
|
|
|
|
|
2019-03-30 07:48:21 -04:00
|
|
|
/** Entry point for the V8 template variable assignment schematic. */
|
|
|
|
export default function(): Rule {
|
|
|
|
return (tree: Tree, context: SchematicContext) => {
|
2019-04-22 15:11:29 -04:00
|
|
|
const {buildPaths, testPaths} = getProjectTsConfigPaths(tree);
|
2019-03-30 07:48:21 -04:00
|
|
|
const basePath = process.cwd();
|
|
|
|
|
2019-04-22 15:11:29 -04:00
|
|
|
if (!buildPaths.length && !testPaths.length) {
|
2019-03-30 07:48:21 -04:00
|
|
|
throw new SchematicsException(
|
|
|
|
'Could not find any tsconfig file. Cannot check templates for template variable ' +
|
|
|
|
'assignments.');
|
|
|
|
}
|
|
|
|
|
2019-04-22 15:11:29 -04:00
|
|
|
for (const tsconfigPath of [...buildPaths, ...testPaths]) {
|
2019-03-30 07:48:21 -04:00
|
|
|
runTemplateVariableAssignmentCheck(tree, tsconfigPath, basePath, context.logger);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Runs the template variable assignment check. Warns developers
|
|
|
|
* if values are assigned to template variables within output bindings.
|
|
|
|
*/
|
|
|
|
function runTemplateVariableAssignmentCheck(
|
|
|
|
tree: Tree, tsconfigPath: string, basePath: string, logger: Logger) {
|
|
|
|
const parsed = parseTsconfigFile(tsconfigPath, dirname(tsconfigPath));
|
|
|
|
const host = ts.createCompilerHost(parsed.options, true);
|
|
|
|
|
|
|
|
// We need to overwrite the host "readFile" method, as we want the TypeScript
|
|
|
|
// program to be based on the file contents in the virtual file tree.
|
|
|
|
host.readFile = fileName => {
|
|
|
|
const buffer = tree.read(relative(basePath, fileName));
|
|
|
|
return buffer ? buffer.toString() : undefined;
|
|
|
|
};
|
|
|
|
|
|
|
|
const program = ts.createProgram(parsed.fileNames, parsed.options, host);
|
|
|
|
const typeChecker = program.getTypeChecker();
|
|
|
|
const templateVisitor = new NgComponentTemplateVisitor(typeChecker);
|
|
|
|
const rootSourceFiles = program.getRootFileNames().map(f => program.getSourceFile(f) !);
|
|
|
|
|
|
|
|
// Analyze source files by detecting HTML templates.
|
2019-04-08 10:16:56 -04:00
|
|
|
rootSourceFiles.forEach(sourceFile => visitAllNodes(sourceFile, [templateVisitor]));
|
2019-03-30 07:48:21 -04:00
|
|
|
|
|
|
|
const {resolvedTemplates} = templateVisitor;
|
2019-04-04 06:52:29 -04:00
|
|
|
const collectedFailures: string[] = [];
|
2019-03-30 07:48:21 -04:00
|
|
|
|
|
|
|
// Analyze each resolved template and print a warning for property writes to
|
|
|
|
// template variables.
|
2019-04-11 12:59:12 -04:00
|
|
|
resolvedTemplates.forEach(template => {
|
|
|
|
const filePath = template.filePath;
|
|
|
|
const nodes = analyzeResolvedTemplate(template);
|
2019-03-30 07:48:21 -04:00
|
|
|
|
|
|
|
if (!nodes) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const displayFilePath = normalize(relative(basePath, filePath));
|
|
|
|
|
|
|
|
nodes.forEach(n => {
|
|
|
|
const {line, character} = template.getCharacterAndLineOfPosition(n.start);
|
2019-04-04 06:52:29 -04:00
|
|
|
collectedFailures.push(`${displayFilePath}@${line + 1}:${character + 1}: ${FAILURE_MESSAGE}`);
|
2019-03-30 07:48:21 -04:00
|
|
|
});
|
|
|
|
});
|
2019-04-04 06:52:29 -04:00
|
|
|
|
|
|
|
if (collectedFailures.length) {
|
|
|
|
logger.info('---- Template Variable Assignment schematic ----');
|
|
|
|
logger.info('Assignments to template variables will no longer work with Ivy as');
|
|
|
|
logger.info('template variables are effectively constants in Ivy. Read more about');
|
|
|
|
logger.info(`this change here: ${README_URL}`);
|
|
|
|
logger.info('');
|
|
|
|
logger.info('The following template assignments were found:');
|
|
|
|
collectedFailures.forEach(failure => logger.warn(`⮑ ${failure}`));
|
|
|
|
logger.info('------------------------------------------------');
|
|
|
|
}
|
2019-03-30 07:48:21 -04:00
|
|
|
}
|