From d42a91234370c084becbe14b9e29e1a01ec0e896 Mon Sep 17 00:00:00 2001 From: Pete Bacon Darwin Date: Tue, 19 May 2020 18:13:33 +0100 Subject: [PATCH] refactor(compiler-cli): expose the `walkForDeclaration()` function (#37206) This test helper can be useful when searching for nodes within an IIFE. Exporting it here is helpful in ngcc reflection tests. PR Close #37206 --- .../src/ngtsc/testing/src/utils.ts | 65 ++++++++++--------- 1 file changed, 34 insertions(+), 31 deletions(-) diff --git a/packages/compiler-cli/src/ngtsc/testing/src/utils.ts b/packages/compiler-cli/src/ngtsc/testing/src/utils.ts index b50751f088..135c3214b2 100644 --- a/packages/compiler-cli/src/ngtsc/testing/src/utils.ts +++ b/packages/compiler-cli/src/ngtsc/testing/src/utils.ts @@ -54,7 +54,7 @@ export function getDeclaration( program: ts.Program, fileName: AbsoluteFsPath, name: string, assert: (value: any) => value is T): T { const sf = getSourceFileOrError(program, fileName); - const chosenDecl = walkForDeclaration(sf); + const chosenDecl = walkForDeclaration(name, sf); if (chosenDecl === null) { throw new Error(`No such symbol: ${name} in ${fileName}`); @@ -63,38 +63,41 @@ export function getDeclaration( throw new Error(`Symbol ${name} from ${fileName} is a ${ts.SyntaxKind[chosenDecl.kind]}`); } return chosenDecl; - - // We walk the AST tree looking for a declaration that matches - function walkForDeclaration(rootNode: ts.Node): ts.Declaration|null { - let chosenDecl: ts.Declaration|null = null; - rootNode.forEachChild(node => { - if (chosenDecl !== null) { - return; - } - if (ts.isVariableStatement(node)) { - node.declarationList.declarations.forEach(decl => { - if (bindingNameEquals(decl.name, name)) { - chosenDecl = decl; - } - }); - } else if ( - ts.isClassDeclaration(node) || ts.isFunctionDeclaration(node) || - ts.isInterfaceDeclaration(node)) { - if (node.name !== undefined && node.name.text === name) { - chosenDecl = node; - } - } else if ( - ts.isImportDeclaration(node) && node.importClause !== undefined && - node.importClause.name !== undefined && node.importClause.name.text === name) { - chosenDecl = node.importClause; - } else { - chosenDecl = walkForDeclaration(node); - } - }); - return chosenDecl; - } } +// We walk the AST tree looking for a declaration that matches +export function walkForDeclaration(name: string, rootNode: ts.Node): ts.Declaration|null { + let chosenDecl: ts.Declaration|null = null; + rootNode.forEachChild(node => { + if (chosenDecl !== null) { + return; + } + if (ts.isVariableStatement(node)) { + node.declarationList.declarations.forEach(decl => { + if (bindingNameEquals(decl.name, name)) { + chosenDecl = decl; + } else { + chosenDecl = walkForDeclaration(name, node); + } + }); + } else if ( + ts.isClassDeclaration(node) || ts.isFunctionDeclaration(node) || + ts.isInterfaceDeclaration(node) || ts.isClassExpression(node)) { + if (node.name !== undefined && node.name.text === name) { + chosenDecl = node; + } + } else if ( + ts.isImportDeclaration(node) && node.importClause !== undefined && + node.importClause.name !== undefined && node.importClause.name.text === name) { + chosenDecl = node.importClause; + } else { + chosenDecl = walkForDeclaration(name, node); + } + }); + return chosenDecl; +} + + function bindingNameEquals(node: ts.BindingName, name: string): boolean { if (ts.isIdentifier(node)) { return node.text === name;