Custom extension rules
Introduction
Considering that users may have their own specific rule definition requirements, Rsdoctor provides an external interface for users to customize their own rule checks in addition to the built-in rules.
The external extension interface needs to be configured on the Rsdoctor plugin through the extends field, and its configuration is also placed in the rules field. See the example below:
You can follow the detailed steps below to define and write custom rules.
Steps for custom rules
1. Installation
When writing custom rules, install @rsdoctor/core and use the defineRule function from @rsdoctor/core to define unified Rsdoctor rules.
2. Writing rules
To write rules, you need to use the defineRule function, which takes a function as input and returns an object in a fixed format. Refer to the following example:
The meta field contains the fixed configuration and content of this rule, and the check field contains the callback that includes the specific logic for rule checking. Their types are as follows.
meta object
For the metadata type accepted by external rules, see ExtendRuleMeta.
Property meanings
- meta
- category
- info: Defines the category of the rule.
- type: 'compile' | 'bundle'.
- title
- info: The title of the rule, used to display in the Rsdoctor report page.
- type: string | generics, can be passed down through generics.
- severity
- info: The severity level of the rule.
- type: 'Ignore' | 'Warn' | 'Error' for external rules.
- default: 'Warn'
- defaultConfig
- info: The default configuration of the rule. Custom rules may require specific configurations, and defaultConfig can be used to configure the default rule configuration.
- type: Generics, can be defined through generics. As shown in the example above.
- referenceUrl
- info: The documentation link for the rule.
- type: string.
- category
check function
The check function is mainly used for rule judgment. The parameter ruleContext is all the build information that Rsdoctor integrates during the build analysis process, and its type is defined as follows.
You can use the build information in the body of the check function to make custom rule judgments. After the judgment, if the rule check fails, you can report it using the report method in the parameter. See the next step for details.
CheckCallback type
RuleCheckerContext type definition, please refer to the details
Example
The CheckRule example above checks the number of assets and reports a finding when it exceeds ruleConfig.limit.
3. Reporting rule results
To report errors, you need to use the report method in the check callback function's parameter. The report method's parameters mainly include the following parts:
- message: The error message.
- document: File data used to describe the location of the error code and code position.
- suggestions: Rule suggestions.
- detail: Detailed information, mainly providing additional data to the frontend.
For detailed type definitions, refer to: ReportData
4. Displaying rule results
The report function will pass the error information of custom rules to the compilation's errors or warnings. It will display the rule results in the terminal during the build process, and even interrupt the build.
Rsdoctor displays custom rule results in a warning list. For more details, see Display Components.
- Basic Rule Warning Component
Display components
Basic rule warning component
-
Component Type
-
Component Input
-
type
- The type of the component.
- value: 'link'.
-
title
- The title of the rule.
- type: string.
-
description
- The description of the rule. The data comes from the
messageordetail.descriptionin thereportfunction: - type: string.
- The description of the rule. The data comes from the
-
level
- The level of the rule.
- type: warn | error.
-
link:
- The details of the rule. The data comes from
detail.link: - type:string。
- The details of the rule. The data comes from
-
-
Example
- Display Components
- Component Code Code
Type definitions
ExtendRuleMeta
This is the metadata accepted by external rules. Internal rules also specify a rule code and use numeric severity values.
RuleCheckerContext
ReportData
LinkRuleStoreData
CodeViewRule
Tools
AST processing
The rule context exposes moduleGraph. Use moduleGraph.getModules() to access modules. For AST-based checks, parse the available original code from module.getSource().source with a parser that supports your project's syntax. Skip modules without source code. The internal parser, asserts, and Document helpers are not exported by @rsdoctor/core.
Data reporting
Please go to Data Reporting for viewing.

