Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: recommend messageId over message for reporting rule violations #18050

Merged
merged 3 commits into from Jan 30, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions docs/src/extend/custom-rules.md
Expand Up @@ -145,7 +145,8 @@ Additionally, the `context` object has the following methods:

The main method you'll use when writing custom rules is `context.report()`, which publishes a warning or error (depending on the configuration being used). This method accepts a single argument, which is an object containing the following properties:

* `message`: (`string`) The problem message.
* `message`: (`string`) The problem message (alternative to `messageId`).
* `messageId`: (`string`) The ID of the message (see [messageIds](#messageids)) (recommended alternative to `message`).
bmish marked this conversation as resolved.
Show resolved Hide resolved
* `node`: (optional `object`) The AST node related to the problem. If present and `loc` is not specified, then the starting location of the node is used as the location of the problem.
* `loc`: (optional `object`) Specifies the location of the problem. If both `loc` and `node` are specified, then the location is used from `loc` instead of `node`.
* `start`: An object of the start location.
Expand Down Expand Up @@ -192,9 +193,11 @@ The node contains all the information necessary to figure out the line and colum

#### `messageId`s

Instead of typing out messages in both the `context.report()` call and your tests, you can use `messageId`s instead.
`messageId`s are the recommended alternative to typing out messages in `context.report()` calls because of the following benefits:
bmish marked this conversation as resolved.
Show resolved Hide resolved

This allows you to avoid retyping error messages. It also prevents errors reported in different sections of your rule from having out-of-date messages.
* Rule violation messages can be stored in a central `meta.messages` object for convenient management
* Rule violation messages do not need to be repeated in both the rule file and rule test file
* As a result, the barrier for changing rule violation messages is lower, encouraging more frequent contributions to improve and optimize them for the greatest clarity and usefulness

Rule file:

Expand Down