Tutorials

Create AST Rules

BackNext

9. Build a fix

We build a fix that will replace the text from the first argument by "bar".

A fix is made of at least one edit. We have different functions to build an edit (buildEditAdd, buildEditRemove and buildEditUpdate). We choose buildEditUpdate to replace text in the code.

The function buildEditUpdate takes as arguments:

  1. the starting line of the edit
  2. the starting column of the edit
  3. the ending line of the edit
  4. the ending column of the edit
  5. the text to update

To make an edit that replaces the value of the argument, such as, updating from foo to var, build an edit like this:

1const edit = buildEditUpdate(
2  firstArgument.start.line,
3  firstArgument.start.col,
4  firstArgument.end.line,
5  firstArgument.end.col,
6  "bar"
7);

We create a fix with the previously created edit:

1const fix = buildFix("print bar", [edit]);

Finally, once the report and the associated edit is complete, we can report it to the IDE using the function addError().

1addError(error.addFix(fix));

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.