Tutorials

Create AST Rules

BackNext

6. Write your rule

The rule code has a visit function that is the entry point of your rule.

1function visit(node, filename, code)

It has the following parameter:

  1. The node your rule matches on. The exact type of this argument depends on the element type being checked. All types inherit the AstElement type. As the element checked is set to FunctionCall, the node for our rule will be a type FunctionCall.
  2. The filename where the code is located (a string)
  3. The code being checked (a string)
  4. If the element type checked is a function call, the value of node is a FunctionCall object
  5. If the element type checked is an if condition, the value of node is a IfCondition
  6. If the element type checked is a for loop, the value of node is aForStatement object
  7. If the element type checked is a function definition, the value of node is a FunctionDefinition object
  8. If the element type checked is a try block, the value of node is a TryBlock object
  9. If the element type checked is an import, the value of node is an Import or an ImportFrom
  10. If the element type checked is an assignment, the value of node is a Assignment object

Let's go!

Start interacting with the tutorial!

my-new-rule.js

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.