Tutorials

Create AST Rules

BackNext

5. Decide what AST element to match

Specify to write an AST rule for different element type. The element type specifies that particular node of the AST you focus on. This node will be the entry point of your analysis rules.

  1. try block: check a try/except block
  2. function call: check a function call in Python (e.g. function(val1, val2))
  3. function definition: check the definition of a function (e.g. def myfunction(val1, val2):)
  4. for loop: check a for loop (e.g. for i in ...)
  5. if condition: check a if condition (e.g. if foo == bar:)
  6. import: check an import statement (e.g. import mypackage or from mypackage import foo)
  7. assignment: check an assignment (e.g. foo = "bar" or foo = bar(baz))

Before you start writing your rule, think about what element of a program you want to check, which will indicate the correct value for the element checked.

Let's go!

Start interacting with the tutorial!

Rule type

Codiga Logo
Codiga Hub
  • Rulesets
  • Playground
  • Snippets
  • Cookbooks
soc-2 icon

We are SOC-2 Compliance Certified

G2 high performer medal

Codiga – All rights reserved 2022.