Create Pattern Rules


8. Build a fix

It's not enough to detect an error, we want to automatically fix it!

We will build a fix that updates the value of the argument and replaces foo with bar.

A fix is composed of at least one edit. You can use any of the following functions to build edits:

1buildEditAdd(startLine, startCol, valueToAdd);
3buildEditRemove(startLine, startCol, endLine, endCol);
5buildEditUpdate(startLine, startCol, endLine, endCol, newValue);
  1. buildEditAdd adds the text valueToAdd in the IDE at line startLine and startCol
  2. buildEditRemove removes the text between startLine:startCol and endLine:endCol
  3. buildEditUpdate updates the text between startLine:startCol and endLine:endCol with newValue.

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

1const edit = buildEditUpdate(
2  exceptionName.start.line,
3  exceptionName.start.col,
4  exceptionName.end.line,
5  exceptionName.end.col,
6  "bar"

Then, build a fix with the edit you previously defined. The first argument is the message of the fix that will be shown in the IDE.

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

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.