Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Querying for classes based off Decorators#20942

Answeredbysmowton
Giardi77 asked this question inQ&A
Discussion options

Hi everyone, I was testing a NestJS codebase which uses a lot of different decorators to implement some logic.

In particular, I wanted to build a query to get all classes with a@Controller decorator but without a@RequireRole decorator.

My attempt was to do something like this:

import javascriptfrom ClassDeclStmt cwhere c.getADecorator().toString().matches("%Controller%") and      not c.getADecorator().toString().matches("%RequireRole%")select c

But this is matching all classes with a@Controller decorator, even those that also have a@RequireRole decorator.

Any help would be really appreciated!

You must be logged in to vote

Your code works for me for simple examples. Can you provide a code example that exhibits the problem?

One thing to watch out for:toString is abbreviated for AST nodes that would naturally consume more than 20 characters, so e.g.@RequireRole("Looooong") might turn into"@Requir ... ooong)". If that's the problem, you may need to usegetADecorator().getExpression().(InvokeExpr).getCalleeName() or similar (depending on the exact nature of the decorator) to extract the decorator's full function name.

Replies: 1 comment 1 reply

Comment options

Your code works for me for simple examples. Can you provide a code example that exhibits the problem?

One thing to watch out for:toString is abbreviated for AST nodes that would naturally consume more than 20 characters, so e.g.@RequireRole("Looooong") might turn into"@Requir ... ooong)". If that's the problem, you may need to usegetADecorator().getExpression().(InvokeExpr).getCalleeName() or similar (depending on the exact nature of the decorator) to extract the decorator's full function name.

You must be logged in to vote
1 reply
@Giardi77
Comment options

Hi@smowton, and thanks for the quick response!
Yes you are right and the problem was due to the fact that the decorator name got "truncated".
UsinggetADecorator().getExpression().(InvokeExpr).getCalleeName() i was able to match the correct classes.

Answer selected byGiardi77
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
2 participants
@Giardi77@smowton

[8]ページ先頭

©2009-2025 Movatter.jp