test(ivy): enhance the in-memory-typescript helper (#25445)
The `getDeclaration()` function now searches down into the AST for matching nodes, which is needed for UMD testing. PR Close #25445
This commit is contained in:
parent
e68490c5e4
commit
aeec66b657
|
@ -117,30 +117,7 @@ export function getDeclaration<T extends ts.Declaration>(
|
||||||
if (!sf) {
|
if (!sf) {
|
||||||
throw new Error(`No such file: ${fileName}`);
|
throw new Error(`No such file: ${fileName}`);
|
||||||
}
|
}
|
||||||
|
const chosenDecl = walkForDeclaration(sf);
|
||||||
let chosenDecl: ts.Declaration|null = null;
|
|
||||||
|
|
||||||
sf.statements.forEach(stmt => {
|
|
||||||
if (chosenDecl !== null) {
|
|
||||||
return;
|
|
||||||
} else if (ts.isVariableStatement(stmt)) {
|
|
||||||
stmt.declarationList.declarations.forEach(decl => {
|
|
||||||
if (bindingNameEquals(decl.name, name)) {
|
|
||||||
chosenDecl = decl;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} else if (ts.isClassDeclaration(stmt) || ts.isFunctionDeclaration(stmt)) {
|
|
||||||
if (stmt.name !== undefined && stmt.name.text === name) {
|
|
||||||
chosenDecl = stmt;
|
|
||||||
}
|
|
||||||
} else if (
|
|
||||||
ts.isImportDeclaration(stmt) && stmt.importClause !== undefined &&
|
|
||||||
stmt.importClause.name !== undefined && stmt.importClause.name.text === name) {
|
|
||||||
chosenDecl = stmt.importClause;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
chosenDecl = chosenDecl as ts.Declaration | null;
|
|
||||||
|
|
||||||
if (chosenDecl === null) {
|
if (chosenDecl === null) {
|
||||||
throw new Error(`No such symbol: ${name} in ${fileName}`);
|
throw new Error(`No such symbol: ${name} in ${fileName}`);
|
||||||
|
@ -148,6 +125,33 @@ export function getDeclaration<T extends ts.Declaration>(
|
||||||
if (!assert(chosenDecl)) {
|
if (!assert(chosenDecl)) {
|
||||||
throw new Error(`Symbol ${name} from ${fileName} is a ${ts.SyntaxKind[chosenDecl.kind]}`);
|
throw new Error(`Symbol ${name} from ${fileName} is a ${ts.SyntaxKind[chosenDecl.kind]}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
return chosenDecl;
|
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)) {
|
||||||
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue