The LinterCheckAstNode API provides methods for getting abstract syntax tree (AST) node details in linter checks.

This API is included with the Instance Scan (com.glide.instance_scan) plugin and runs in the sn_instance_scan namespace. For more information, see Instance Scan.

Use the methods in this class to run linter checks on AST node types by adding code to the Script field in the Linter Check form. For more information, see Advanced linter check scripts.

Create a linter check to identify any issues in a script. When a linter check is run on a record, an abstract syntax tree for its code is generated. You can use the abstract syntax tree to analyze issues with the code.

Access methods in this API using the Instance Scan engine.rootNode object.

LinterCheckAstNode - getNameIdentifier()

Retrieves the string value of a name node type. A name node represents a simple identifier that is not a keyword, such as a function or variable name.

Table 1. Parameters
Name Type Description
None
Table 2. Returns
Type Description
String If the linter object is a name node type, return the name as a string. Null otherwise.

Example

The following example uses the Findings API incrementWithNode() method in a linter check. Use this method in the Script field of the Linter Check form.

(function(engine) {
  engine.rootNode.visit(function(node) {
    if (node.getTypeName() === "NAME" &&
        node.getNameIdentifier() === "soughtFunction" &&
        node.getParent().getTypeName() === "CALL") {
      engine.finding.incrementWithNode(node);
    } 
  });
})(engine);

LinterCheckAstNode - getParent()

Gets the parent node object of the accessed node.

Table 3. Parameters
Name Type Description
None
Table 4. Returns
Type Description
Object Parent node object of the accessed node.

Example

The following example uses the Findings API incrementWithNode() method in a linter check. Use this method in the Script field of the Linter Check form.

(function(engine) {
  engine.rootNode.visit(function(node) {
    if (node.getTypeName() === "NAME" &&
        node.getNameIdentifier() === "soughtFunction" &&
        node.getParent().getTypeName() === "CALL") {
      engine.finding.incrementWithNode(node);
    } 
  });
})(engine);

LinterCheckAstNode - getTypeName()

Gets the type of the accessed node.

Table 5. Parameters
Name Type Description
None
Table 6. Returns
Type Description
String Type name of the accessed node. For example, a function call in the source is tokenized as a node with the type name of CALL.

Example

The following example uses the Findings API incrementWithNode() method in a linter check. Use this method in the Script field of the Linter Check form.

(function(engine) {
  engine.rootNode.visit(function(node) {
    if (node.getTypeName() === "NAME" &&
        node.getNameIdentifier() === "soughtFunction" &&
        node.getParent().getTypeName() === "CALL") {
      engine.finding.incrementWithNode(node);
    } 
  });
})(engine);

LinterCheckAstNode - visit(Function callbackFunction)

Accesses each node in the subtree starting from this node and executes a given callback function on each node.

Table 7. Parameters
Name Type Description
callbackFunction Function Callback function to be executed on each node in the subtree of this node. This callback function takes a node as a parameter which is the node to be visited.
Table 8. Returns
Type Description
None

Example

The following example uses the Findings API incrementWithNode() method in a linter check. Use this method in the Script field of the Linter Check form.

(function(engine) {
  engine.rootNode.visit(function(node) {
    if (node.getTypeName() === "NAME" &&
        node.getNameIdentifier() === "soughtFunction" &&
        node.getParent().getTypeName() === "CALL") {
      engine.finding.incrementWithNode(node);
    } 
  });
})(engine);