test(ngcc): use `isNamedDeclaration()` helper to simplify tests (#38959)

Previously these tests were checking multiple specific expression
types. The new helper function is more general and will also support
`PropertyAccessExpression` nodes for `InlineDeclaration` types.

PR Close #38959
This commit is contained in:
Pete Bacon Darwin 2020-09-29 20:52:40 +01:00 committed by atscott
parent 0accd1e68d
commit 47eab61cad
3 changed files with 10 additions and 9 deletions

View File

@ -10,8 +10,8 @@ import * as ts from 'typescript';
import {absoluteFrom, AbsoluteFsPath, getSourceFileOrError} from '../../../src/ngtsc/file_system';
import {runInEachFileSystem, TestFile} from '../../../src/ngtsc/file_system/testing';
import {MockLogger} from '../../../src/ngtsc/logging/testing';
import {DeclarationNode, isNamedClassDeclaration, isNamedVariableDeclaration} from '../../../src/ngtsc/reflection';
import {getDeclaration} from '../../../src/ngtsc/testing';
import {DeclarationNode} from '../../../src/ngtsc/reflection';
import {getDeclaration, isNamedDeclaration} from '../../../src/ngtsc/testing';
import {loadTestFiles} from '../../../test/helpers';
import {ModuleWithProvidersAnalyses, ModuleWithProvidersAnalyzer} from '../../src/analysis/module_with_providers_analyzer';
import {NgccReferencesRegistry} from '../../src/analysis/ngcc_references_registry';
@ -661,9 +661,7 @@ runInEachFileSystem(() => {
}
function getName(node: DeclarationNode|null): string {
return node && (isNamedVariableDeclaration(node) || isNamedClassDeclaration(node)) ?
`${node.name.text}.` :
'';
return node && isNamedDeclaration(node) ? `${node.name.text}.` : '';
}
});
});

View File

@ -5,4 +5,4 @@
* 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
*/
export {expectCompleteReuse, getDeclaration, makeProgram} from './src/utils';
export {expectCompleteReuse, getDeclaration, isNamedDeclaration, makeProgram} from './src/utils';

View File

@ -93,9 +93,7 @@ export function walkForDeclarations(name: string, rootNode: ts.Node): Declaratio
chosenDecls.push(...walkForDeclarations(name, node));
}
});
} else if (
ts.isClassDeclaration(node) || ts.isFunctionDeclaration(node) ||
ts.isInterfaceDeclaration(node) || ts.isClassExpression(node)) {
} else if (isNamedDeclaration(node)) {
if (node.name !== undefined && node.name.text === name) {
chosenDecls.push(node);
}
@ -111,6 +109,11 @@ export function walkForDeclarations(name: string, rootNode: ts.Node): Declaratio
return chosenDecls;
}
export function isNamedDeclaration(node: ts.Node): node is ts.Declaration&{name: ts.Identifier} {
const namedNode = node as {name?: ts.Identifier};
return namedNode.name !== undefined && ts.isIdentifier(namedNode.name);
}
const COMPLETE_REUSE_FAILURE_MESSAGE =
'The original program was not reused completely, even though no changes should have been made to its structure';