forbidden-code
Conformance is available on Enterprise plans
The rule type enables you to disallow code and code patterns through string and regular expression matches.
- Disallowing comments
- You want to disallow comments
- You want to disallow usage of
- Disallowing specific strings
- You want to enforce a certain casing for one or more strings
- You want to disallow specific strings from being used within code
If you want to disallow specific operations on a property, you should instead use the rule type.
To create a custom rule, you'll need to configure the below required properties:
| Property | Type | Description |
|---|---|---|
| The custom rule's type. | ||
| The custom rule's name. | ||
| (optional) | The custom rule's categories. Default is . | |
| The error message, which is shown to users when they encounter this rule. | ||
| (optional) | An optional link to show alongside the error message. | |
| (optional) | The rule description, which is shown in the Vercel Compass dashboard and included in allowlist files. | |
| (optional) | The rule severity added to the allowlists and used to calculate a project's conformance score. | |
| An array of regular expression patterns to match against. | ||
| An array of exact string to match against (case sensitive). |
Multi-line strings and patterns are currently unsupported by this custom rule type.
The example below configures a rule named that disallows:
- Any usage of at the start of a line (case-sensitive).
- Any usage of in any case.
- Any usage of (case-sensitive).
This custom rule type always sets the (or global) flag for regular expressions. This ensures that all regular expression matches are reported, opposed to only reporting on the first match.
When providing flags through an object in , you can omit the as this will automatically be set.
To learn more about regular expression flags, see the MDN guide on advanced searching with flags.
If you're not familiar with regular expressions, you can use tools like regex101 and/or RegExr to help you understand and write regular expressions.
Regular expressions can vary in complexity, depending on what you're trying to achieve. We've added some examples below to help you get started.
| Pattern | Description |
|---|---|
| Matches , but only if it occurs at the start of a line (). | |
| Matches and , but only if it occurs at the end of a line (). | |
| Matches and , with or without the (). | |
| Matches , but only when not following a hyphen (). |
To enable this rule type, you can set the rule to , or provide the following configuration.
| Property | Type | Description |
|---|---|---|
| (optional) | An optional array of exact paths or glob expressions*. *Note that paths containing square brackets need to be escaped, i.e. would become . |
The example below enables the custom rule for all files in the directory, excluding files in . In this example, the custom rule is also restricted to the and workspaces, which is optional.
This next example enables the custom rule for all files, and without workspace restrictions.
;
Was this helpful?