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);
2
3buildEditRemove(startLine, startCol, endLine, endCol);
4
5buildEditUpdate(startLine, startCol, endLine, endCol, newValue);
- buildEditAdd adds the text valueToAdd in the IDE at line startLine and startCol
- buildEditRemove removes the text between startLine:startCol and endLine:endCol
- 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"
7);
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!
my-new-rule.js