Skip to content

Commit 994aab7

Browse files
committed
refactor(@schematics/angular): use type guard based narrowing with TS AST
By leveraging TypeScript's AST type guards, function parameter assumptions and casting can be removed. Many of these cases caused errors when enabling TypeScript's strict option. This is preliminary work to support enabling full TypeScript strict mode within the project.
1 parent bcf6ead commit 994aab7

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

packages/schematics/angular/app-shell/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,11 @@ function getBootstrapComponentPath(
110110
const componentSymbol = arrLiteral.elements[0].getText();
111111

112112
const relativePath = getSourceNodes(moduleSource)
113-
.filter(node => node.kind === ts.SyntaxKind.ImportDeclaration)
113+
.filter(ts.isImportDeclaration)
114114
.filter(imp => {
115115
return findNode(imp, ts.SyntaxKind.Identifier, componentSymbol);
116116
})
117-
.map((imp: ts.ImportDeclaration) => {
117+
.map(imp => {
118118
const pathStringLiteral = imp.moduleSpecifier as ts.StringLiteral;
119119

120120
return pathStringLiteral.text;
@@ -205,8 +205,8 @@ function addRouterModule(mainPath: string): Rule {
205205
function getMetadataProperty(metadata: ts.Node, propertyName: string): ts.PropertyAssignment {
206206
const properties = (metadata as ts.ObjectLiteralExpression).properties;
207207
const property = properties
208-
.filter(prop => prop.kind === ts.SyntaxKind.PropertyAssignment)
209-
.filter((prop: ts.PropertyAssignment) => {
208+
.filter(ts.isPropertyAssignment)
209+
.filter((prop) => {
210210
const name = prop.name;
211211
switch (name.kind) {
212212
case ts.SyntaxKind.Identifier:

packages/schematics/angular/utility/ng-ast-utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ export function findBootstrapModulePath(host: Tree, mainPath: string): string {
6262
const source = ts.createSourceFile(mainPath, mainText, ts.ScriptTarget.Latest, true);
6363
const allNodes = getSourceNodes(source);
6464
const bootstrapModuleRelativePath = allNodes
65-
.filter(node => node.kind === ts.SyntaxKind.ImportDeclaration)
65+
.filter(ts.isImportDeclaration)
6666
.filter(imp => {
6767
return findNode(imp, ts.SyntaxKind.Identifier, bootstrapModule.getText());
6868
})
69-
.map((imp: ts.ImportDeclaration) => {
69+
.map(imp => {
7070
const modulePathStringLiteral = imp.moduleSpecifier as ts.StringLiteral;
7171

7272
return modulePathStringLiteral.text;

0 commit comments

Comments
 (0)