New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can ConstantIfExpressionRule
detect more complex cases? If so how?
#760
Comments
Hmmm, that is an interesting question/request. I think that would involve creating an AST Visitor that processed each variable definition (see I think that would work. I think extending |
You would need to build up some kind of table of declared variables in a scope (lets call it a context) and then when you get to The full context would actually require the full AST to be built and some processing of it to happen. I don't know Groovy's parser well but presumably this actually happens at some point when compiling/ running a groovy script. Perhaps there is something already there that can be reused? |
One possible way of doing this might be to collect the relevant statements/ code blocks and then use the |
I'm pretty sure you don't want to use GroovyShell to re-parse/evaluate the code .. that would be unpleasant and complicated and is unnecessary since the Groovy AST should already provide the relevant information. Most of the CodeNarc rules use the Groovy AST as-is. The approach uses the Visitor pattern. Many of those rules are implemented by a "main" XxxRule class and then an associated XxxAstVisitor class. We already have several rules where the AST Vistor builds up "context" information as it visits components in the AST, such as classes, methods, variables, expressions, etc.. The typical pattern instantiates the AST Visitor class for each file it is processing, so that "context" in specific to a single source file. See CodeNarc/src/main/groovy/org/codenarc/rule/design/AssignmentToStaticFieldFromInstanceMethodRule.groovy at master · CodeNarc/CodeNarc as an example. That holds on to the static field names and local variable names. The AST elements are visited in the order of how the source is parsed/compiled/processed, so you will visit field definitions before the fields are referenced, for instance. So I think in the scenario you describe above, you could process/visit variable definitions, save the names if they are initialized to "constant" values. Then also process/visit if expressions and check if the conditional is (only?) referencing one of those variable names .. something like that. |
I want to create/ extend a rule that is similar to the
ConstantIfExpressionRule
but covers cases where the constant may be inferred.For example
Arguably, this might not be static analysis but it's not far off as it only involves a lookup of a defined variable. Being new to CodeNarc and the Groovy AST I don't know if there is any existing support in either to determine the value of
b
while checking theif
statement. Is this possible?The text was updated successfully, but these errors were encountered: