Custom Rules Programming Interface
Debugging your rules
You can write console.log()
statement to debug and troubleshoot your rules.
The output is shown in the rule writer.
Reporting an error
Build an error
When building an error, use the start
and end
attribute of the AST of Pattern.
To report an error, use the reportError
function
reportError(startLine, startCol, endLine, endCol, message, severity, category);
With the following arguments:
startLine
: the line where the error startsstartCol
: the column where the error startsendLine
: the line where the error endsendCol
: the column where the error endsmessage
: the error message to showseverity
: one string with one of the following values:CRITICAL
,ERROR
,WARNING
,INFORMATIONAL
category
: one string with one of the following values:ERROR_PRONE
,CODE_STYLE
,BEST_PRACTICE
,SAFETY
,SECURITY
,DESIGN
,DEPLOYMENT
.
Report the error
To report the error, use the function addError
as follow:
addError(error);
The following code builds an error and reports it.
// Build the error
const error = buildError(
exception.start.line,
exception.start.col,
exception.end.line,
exception.end.col,
"generic exception",
"WARNING",
"BEST_PRACTICES"
);
// Add the error
addError(error);
Generating a fix
A fix is made of several edits. Edits are executed sequentially in the IDE plugins.
All edits in a fix are executed sequentially by the IDE plugins. If you edit the same location in the code, make sure that all fixes can be applied sequentially.
Generate an edit
Use the function buildEdit
. This function has the following signature:
buildEdit(startLine, startCol, endLine, endCol, editType, content);
This function takes the following parameter:
startLine
: starting line of the editstartCol
: starting column of the editendLine
: ending line of the edit (null
if this )endCol
: ending column of the editeditType
: can have the valueadd
,remove
,update
: the type of the actioncontent
: the new text added or updated. The value isnull
ifeditType
isremove
.
There is an example that builds an edit to update from 1:10 to 1:20 with the content "foobar".
const edit = buildEdit(1, 10, 1, 20, "update", "foobar");
Helper Functions
buildEditUpdate(startLine, startCol, endLine, endCol, content)
: create an edit that updates the code from starLine:startCol to endLine:endColbuildEditRemove(startLine, startCol, endLine, endCol)
: remove content from startLine:startCol to endLine:endColbuildEditAdd(startLine, startCol, content)
: add the stringcontent
at startLine:startCol
Build the fix
The function buildFix
builds a fix. It takes the description of the fix and a list of edits
that are applied for this fix.
const fix = buildFix("remove the write flag", [edit]);
Attach the fix to an error
Use the function .addFix
to the error.
const errorWithFix = error.addFix(fix);
You can report an error with a fix using the following function:
addError(error.addFix(fix));