Skip to content

Commit 8aa38ac

Browse files
devversionjelbourn
authored andcommitted
refactor: vendor consumed typescript ast utils from @schematics/angular (#18155)
Instead of directly accessing utility functions from `@schematics/angular`, we should vendor the AST utils we need. This is necessary because `@schematics/angular` vendors TypeScript, and this causes a type mismatch if we try to pass in `ts.Node`'s which are created from the TypeScript version available to `@angular/cdk/schematics`. Using the vendored TS version from `@schematics/angular` is not an option as it complicates our setup, gives us less control, and will be hard to consistently enforce in the schematic code. We tried to do this with the `version agnostic typescript` code, but it had downsides in terms of type safety and convenience.
1 parent 5df0916 commit 8aa38ac

File tree

9 files changed

+593
-66
lines changed

9 files changed

+593
-66
lines changed

src/cdk/schematics/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ export * from './utils';
1010
export * from './ng-update/public-api';
1111
export * from './update-tool/public-api';
1212

13+
// Re-exported so that Angular Material schematic code can consume the
14+
// vendored "@schematics/angular" AST utils.
15+
export * from './utils/vendored-ast-utils';
16+
1317
// Re-export parse5 from the CDK. Material schematics code cannot simply import
1418
// "parse5" because it could result in a different version. As long as we import
1519
// it from within the CDK, it will always be the correct version that is specified

src/cdk/schematics/utils/ast.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,15 @@
99
import {WorkspaceProject} from '@angular-devkit/core/src/experimental/workspace';
1010
import {SchematicsException, Tree} from '@angular-devkit/schematics';
1111
import {Schema as ComponentOptions} from '@schematics/angular/component/schema';
12-
import {addImportToModule} from '@schematics/angular/utility/ast-utils';
1312
import {InsertChange} from '@schematics/angular/utility/change';
1413
import {getWorkspace} from '@schematics/angular/utility/config';
1514
import {findModuleFromOptions as internalFindModule} from '@schematics/angular/utility/find-module';
16-
import {getAppModulePath} from '@schematics/angular/utility/ng-ast-utils';
15+
import * as ts from 'typescript';
1716
import {getProjectMainFile} from './project-main-file';
18-
import {ts, typescript} from './version-agnostic-typescript';
19-
17+
import {addImportToModule, getAppModulePath} from './vendored-ast-utils';
2018

2119
/** Reads file given path and returns TypeScript source file. */
22-
export function getSourceFile(host: Tree, path: string): typescript.SourceFile {
20+
export function parseSourceFile(host: Tree, path: string): ts.SourceFile {
2321
const buffer = host.read(path);
2422
if (!buffer) {
2523
throw new SchematicsException(`Could not find file for path: ${path}`);
@@ -44,7 +42,7 @@ export function addModuleImportToRootModule(host: Tree, moduleName: string, src:
4442
export function addModuleImportToModule(host: Tree, modulePath: string, moduleName: string,
4543
src: string) {
4644

47-
const moduleSource = getSourceFile(host, modulePath);
45+
const moduleSource = parseSourceFile(host, modulePath);
4846

4947
if (!moduleSource) {
5048
throw new SchematicsException(`Module not found: ${modulePath}`);

src/cdk/schematics/utils/build-component.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@ import {
2323
} from '@angular-devkit/schematics';
2424
import {FileSystemSchematicContext} from '@angular-devkit/schematics/tools';
2525
import {Schema as ComponentOptions, Style} from '@schematics/angular/component/schema';
26-
import {
27-
addDeclarationToModule,
28-
addEntryComponentToModule,
29-
addExportToModule,
30-
} from '@schematics/angular/utility/ast-utils';
3126
import {InsertChange} from '@schematics/angular/utility/change';
3227
import {getWorkspace} from '@schematics/angular/utility/config';
3328
import {buildRelativePath, findModuleFromOptions} from '@schematics/angular/utility/find-module';
@@ -36,9 +31,14 @@ import {buildDefaultPath} from '@schematics/angular/utility/project';
3631
import {validateHtmlSelector, validateName} from '@schematics/angular/utility/validation';
3732
import {readFileSync, statSync} from 'fs';
3833
import {dirname, join, resolve} from 'path';
34+
import * as ts from 'typescript';
35+
import {
36+
addDeclarationToModule,
37+
addEntryComponentToModule,
38+
addExportToModule,
39+
} from '../utils/vendored-ast-utils';
3940
import {getProjectFromWorkspace} from './get-project';
4041
import {getDefaultComponentOptions} from './schematic-options';
41-
import {ts} from './version-agnostic-typescript';
4242

4343
/**
4444
* List of style extensions which are CSS compatible. All supported CLI style extensions can be

src/cdk/schematics/utils/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,3 @@ export * from './project-main-file';
1717
export * from './project-style-file';
1818
export * from './project-targets';
1919
export * from './schematic-options';
20-
export * from './version-agnostic-typescript';

0 commit comments

Comments
 (0)