Skip to content

Commit

Permalink
feat: add file path in error result (#885)
Browse files Browse the repository at this point in the history
  • Loading branch information
splincode authored Sep 13, 2024
1 parent 6edf419 commit 3963cec
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
17 changes: 10 additions & 7 deletions libs/ng-morph/src/utils/get-declaration-getter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ export function getDeclarationGetter<
>(getFn: (pattern: Pattern) => Declaration[]) {
return function getDeclaration(
pattern: Pattern,
query?: Query<Omit<Structure, 'kind'>>,
query?: Query<Structure>,
): Declaration[] {
return getFn(pattern).filter((declaration) =>
// TODO: refactor it to support new typings
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
matchQuery(declaration.getStructure(), query),
);
return getFn(pattern).filter((declaration) => {
try {
return matchQuery(declaration.getStructure(), query);
} catch (error: unknown) {
const filePath = declaration.getSourceFile().getFilePath();

throw new Error(`Error in ${filePath}\n${(error as Error).message}`);
}
});
};
}
6 changes: 3 additions & 3 deletions libs/ng-morph/src/utils/match-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ function coerceName<T extends string>(name: T | {name: T}): T {
}

export function matchQuery<T extends Structure>(
value: Record<any, any> & T,
value: Record<any, any> | T,
query?: Query<T> & Record<any, any>,
): boolean {
return (
!query ||
Object.keys(query).every((key) =>
coerceArray(value[key])
coerceArray((value as unknown as Record<any, any>)?.[key])
.map(coerceName)
.some((v) => {
.some((v: unknown) => {
const patterns = coerceArray(query[key]);

return typeof v === 'string'
Expand Down

0 comments on commit 3963cec

Please sign in to comment.