Skip to content

Commit

Permalink
Add docs for Directive class
Browse files Browse the repository at this point in the history
  • Loading branch information
nzakas committed Aug 29, 2024
1 parent 61d64fe commit a71b9b6
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions packages/plugin-kit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ This package exports the following utilities:

- `ConfigCommentParser` - used to parse ESLint configuration comments (i.e., `/* eslint-disable rule */`)
- `VisitNodeStep` and `CallMethodStep` - used to help implement `SourceCode#traverse()`
- `Directive` - used to help implement `SourceCode#getDisableDirectives()`
- `TextSourceCodeBase` - base class to help implement the `SourceCode` interface

### `ConfigCommentParser`
Expand Down Expand Up @@ -151,6 +152,59 @@ class MySourceCode {
}
```

### `Directive`

The `Directive` class represents a disable directive in the source code and implements the `Directive` interface from `@eslint/core`. You can tell ESLint about disable directives using the `SourceCode#getDisableDirectives()` method, where part of the return value is an array of `Directive` objects. Here's an example:

```js
import {
VisitNodeStep,
CallMethodStep,
ConfigCommentParser,
} from "@eslint/plugin-kit";

class MySourceCode {
getDisableDirectives() {
const directives = [];
const problems = [];
const commentParser = new ConfigCommentParser();

// read in the inline config nodes to check each one
this.getInlineConfigNodes().forEach(comment => {
// Step 1: Parse the directive
const { label, value, justification } =
commentParser.parseDirective(comment.value);

// Step 2: Extract the directive value and create the `Directive` object
switch (label) {
case "eslint-disable":
case "eslint-enable":
case "eslint-disable-next-line":
case "eslint-disable-line": {
const directiveType = label.slice("eslint-".length);

directives.push(
new Directive({
type: directiveType,
node: comment,
value,
justification,
}),
);
}

// ignore any comments that don't begin with known labels
}
});

return {
directives,
problems,
};
}
}
```

### `TextSourceCodeBase`

The `TextSourceCodeBase` class is intended to be a base class that has several of the common members found in `SourceCode` objects already implemented. Those members are:
Expand Down

0 comments on commit a71b9b6

Please sign in to comment.