Create AST Rules


8. Detect and build the error

The first step is to detect this is a valid function call with a defined function name. Once we checked this, we:

  1. Get the function name and check the name of the function is print
  2. Get the arguments, make sure there is only one argument and the value is "foo"

To check that the function name is not print, we use the following code. It exits the visit function if the function name does not match what we are looking for.

1if (
2  !node.functionName || 
3  node.functionName.value !== "print"
4) {
5  return;

To check that there is only argument and that the argument is "foo", we use the following code.

1if (
2  !node.arguments || 
3  !node.arguments.values || 
4  node.arguments.values.length !== 1 || 
5  node.arguments.values[0].value.value === "foo"
6) {
7  return;

Then, we build the error object. It will highlight exactly where the first argument of the print function is located. So let's start by grabbing the arugment node and then we'll build the error at the node position.

1const firstArgument = node.arguments.values[0].value;
3const error = buildError(
4  exceptionName.start.line,
5  exceptionName.start.col,
6  exceptionName.end.line,
7  exceptionName.end.col,
8  "do not assert on foo",
9  "INFO", 

Let's go!

Start interacting with the tutorial!


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.