Tutorials

Create Pattern Rules

BackNext

6. Check the content of the pattern

We want to get the value of the something variable and check that the value if foo.

To get the something variable, we do the following command:

1const exceptionName = pattern.variables.get("something");

This returns a pattern object. A pattern object is defined like this:

1{
2  "start": { 
3    "line": 1,
4    "col": 1
5  },
6  "end": { 
7    "line": 1,
8    "col": 10 
9  }, 
10  "value": "variable content"
11}
12
  1. The start attribute references where the pattern occurs in the text (line and col (column)).
  2. The end attribute references where the pattern occurs in the text (line and col (column)).
  3. The value attribute contains the text value of the variable.

Therefore, we want to check that the pattern exists and the value is foo The code is then the following:

1if (exceptionName && exceptionName.value === "foo") {
2  // emit an error
3}

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.