-
Notifications
You must be signed in to change notification settings - Fork 1.9k
-
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 javascript
from ClassDeclStmt c
where 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!
Beta Was this translation helpful? Give feedback.
All reactions
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 use getADecorator().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
-
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 use getADecorator().getExpression().(InvokeExpr).getCalleeName() or similar (depending on the exact nature of the decorator) to extract the decorator's full function name.
Beta Was this translation helpful? Give feedback.
All reactions
-
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".
Using getADecorator().getExpression().(InvokeExpr).getCalleeName() i was able to match the correct classes.
Beta Was this translation helpful? Give feedback.