Skip to content

Commit f0a9883

Browse files
bpmuttersnitin315nzakasmdjermanovic
authored
docs: split rules documentation (#16797)
* docs: split rule docs Resolves #16648 * Add todo * add page to IA * Copy edits + remove TODO * Update docs/src/contribute/contribute-core-rule.md Co-authored-by: Nitin Kumar <[email protected]> * Apply suggestions from code review Co-authored-by: Nicholas C. Zakas <[email protected]> Co-authored-by: Milos Djermanovic <[email protected]> * Apply suggestions from code review Co-authored-by: Milos Djermanovic <[email protected]> * move rule performance testing to custom rules * Apply suggestions from code review Co-authored-by: Nicholas C. Zakas <[email protected]> Co-authored-by: Milos Djermanovic <[email protected]> * add basic custom rule overview --------- Co-authored-by: Nitin Kumar <[email protected]> Co-authored-by: Nicholas C. Zakas <[email protected]> Co-authored-by: Milos Djermanovic <[email protected]>
1 parent 923f61d commit f0a9883

File tree

5 files changed

+132
-77
lines changed

5 files changed

+132
-77
lines changed

docs/src/contribute/core-rules.md

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
---
2+
title: Contribute to Core Rules
3+
eleventyNavigation:
4+
key: contribute core rule
5+
parent: contribute to eslint
6+
title: Contribute to Core Rules
7+
order: 10
8+
---
9+
10+
The ESLint core rules are the rules included in the ESLint package.
11+
12+
## Rule Writing Documentation
13+
14+
For full reference information on writing rules, refer to [Custom Rules](../extend/custom-rules). Both custom rules and core rules have the same API. The primary difference between core and custom rules are:
15+
16+
1. Core rules are included in the `eslint` package.
17+
1. Core rules must adhere to the conventions documented on this page.
18+
19+
## File Structure
20+
21+
Each core rule in ESLint has three files named with its identifier (for example, `no-extra-semi`).
22+
23+
* in the `lib/rules` directory: a source file (for example, `no-extra-semi.js`)
24+
* in the `tests/lib/rules` directory: a test file (for example, `no-extra-semi.js`)
25+
* in the `docs/src/rules` directory: a Markdown documentation file (for example, `no-extra-semi.md`)
26+
27+
**Important:** If you submit a core rule to the ESLint repository, you **must** follow the conventions explained below.
28+
29+
Here is the basic format of the source file for a rule:
30+
31+
```js
32+
/**
33+
* @fileoverview Rule to disallow unnecessary semicolons
34+
* @author Nicholas C. Zakas
35+
*/
36+
37+
"use strict";
38+
39+
//------------------------------------------------------------------------------
40+
// Rule Definition
41+
//------------------------------------------------------------------------------
42+
43+
/** @type {import('../shared/types').Rule} */
44+
module.exports = {
45+
meta: {
46+
type: "suggestion",
47+
48+
docs: {
49+
description: "disallow unnecessary semicolons",
50+
recommended: true,
51+
url: "https://eslint.org/docs/rules/no-extra-semi"
52+
},
53+
fixable: "code",
54+
schema: [] // no options
55+
},
56+
create: function(context) {
57+
return {
58+
// callback functions
59+
};
60+
}
61+
};
62+
```
63+
64+
## Rule Unit Tests
65+
66+
Each bundled rule for ESLint core must have a set of unit tests submitted with it to be accepted. The test file is named the same as the source file but lives in `tests/lib/`. For example, if the rule source file is `lib/rules/foo.js` then the test file should be `tests/lib/rules/foo.js`.
67+
68+
ESLint provides the [`RuleTester`](../integrate/nodejs-api#ruletester) utility to make it easy to write tests for rules.
69+
70+
## Performance Testing
71+
72+
To keep the linting process efficient and unobtrusive, it is useful to verify the performance impact of new rules or modifications to existing rules.
73+
74+
To learn how to profile the performance of individual rules, refer to [Profile Rule Performance](../extend/custom-rules#profile-rule-performance) in the custom rules documentation.
75+
76+
When developing in the ESLint core repository, the `npm run perf` command gives a high-level overview of ESLint running time with all core rules enabled.
77+
78+
```bash
79+
$ git checkout main
80+
Switched to branch 'main'
81+
82+
$ npm run perf
83+
CPU Speed is 2200 with multiplier 7500000
84+
Performance Run #1: 1394.689313ms
85+
Performance Run #2: 1423.295351ms
86+
Performance Run #3: 1385.09515ms
87+
Performance Run #4: 1382.406982ms
88+
Performance Run #5: 1409.68566ms
89+
Performance budget ok: 1394.689313ms (limit: 3409.090909090909ms)
90+
91+
$ git checkout my-rule-branch
92+
Switched to branch 'my-rule-branch'
93+
94+
$ npm run perf
95+
CPU Speed is 2200 with multiplier 7500000
96+
Performance Run #1: 1443.736547ms
97+
Performance Run #2: 1419.193291ms
98+
Performance Run #3: 1436.018228ms
99+
Performance Run #4: 1473.605485ms
100+
Performance Run #5: 1457.455283ms
101+
Performance budget ok: 1443.736547ms (limit: 3409.090909090909ms)
102+
```
103+
104+
## Rule Naming Conventions
105+
106+
The rule naming conventions for ESLint are as follows:
107+
108+
* Use dashes between words.
109+
* If your rule only disallows something, prefix it with `no-` such as `no-eval` for disallowing `eval()` and `no-debugger` for disallowing `debugger`.
110+
* If your rule is enforcing the inclusion of something, use a short name without a special prefix.

docs/src/contribute/governance.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ eleventyNavigation:
44
key: governance
55
parent: contribute to eslint
66
title: Governance
7-
order: 10
7+
order: 11
88

99
---
1010

docs/src/contribute/index.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ Have some extra time and want to contribute? This section talks about the proces
5050

5151
We're always looking for contributions from the community. This section explains the requirements for pull requests and the process of contributing code.
5252

53+
## [Contribute to Core Rules](core-rules)
54+
55+
This section explains how to add to the core rules of ESLint.
56+
5357
## [Governance](governance)
5458

5559
Describes the governance policy for ESLint, including the rights and privileges of individuals inside the project.

docs/src/extend/custom-rules.md

Lines changed: 16 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,20 @@ eleventyNavigation:
88

99
---
1010

11-
**Note:** This page covers the most recent rule format for ESLint >= 3.0.0. There is also a [deprecated rule format](./custom-rules-deprecated).
12-
13-
Each rule in ESLint has three files named with its identifier (for example, `no-extra-semi`).
11+
You can create custom rules to use with ESLint. You might want to create a custom rule if the [core rules](../rules/) do not cover your use case.
1412

15-
* in the `lib/rules` directory: a source file (for example, `no-extra-semi.js`)
16-
* in the `tests/lib/rules` directory: a test file (for example, `no-extra-semi.js`)
17-
* in the `docs/src/rules` directory: a Markdown documentation file (for example, `no-extra-semi.md`)
18-
19-
**Important:** If you submit a **core** rule to the ESLint repository, you **must** follow some conventions explained below.
13+
**Note:** This page covers the most recent rule format for ESLint >= 3.0.0. There is also a [deprecated rule format](./custom-rules-deprecated).
2014

21-
Here is the basic format of the source file for a rule:
15+
Here's the basic format of a custom rule:
2216

2317
```js
24-
/**
25-
* @fileoverview Rule to disallow unnecessary semicolons
26-
* @author Nicholas C. Zakas
27-
*/
18+
// customRule.js
2819

29-
"use strict";
30-
31-
//------------------------------------------------------------------------------
32-
// Rule Definition
33-
//------------------------------------------------------------------------------
34-
35-
/** @type {import('eslint').Rule.RuleModule} */
3620
module.exports = {
3721
meta: {
3822
type: "suggestion",
39-
4023
docs: {
41-
description: "disallow unnecessary semicolons",
42-
recommended: true,
43-
url: "https://eslint.org/docs/rules/no-extra-semi"
24+
description: "Description of the rule",
4425
},
4526
fixable: "code",
4627
schema: [] // no options
@@ -712,47 +693,25 @@ You can access that code path objects with five events related to code paths.
712693

713694
## Rule Unit Tests
714695

715-
Each bundled rule for ESLint core must have a set of unit tests submitted with it to be accepted. The test file is named the same as the source file but lives in `tests/lib/`. For example, if the rule source file is `lib/rules/foo.js` then the test file should be `tests/lib/rules/foo.js`.
716-
717696
ESLint provides the [`RuleTester`](../integrate/nodejs-api#ruletester) utility to make it easy to write tests for rules.
718697

719-
## Performance Testing
698+
## Rule Naming Conventions
720699

721-
To keep the linting process efficient and unobtrusive, it is useful to verify the performance impact of new rules or modifications to existing rules.
700+
While you can give a custom rule any name you'd like, the core rules have naming conventions that it could be clearer to apply to your custom rule. To learn more, refer to the [Core Rule Naming Conventions](../contribute/core-rules#rule-naming-conventions) documentation.
722701

723-
### Overall Performance
702+
## Runtime Rules
724703

725-
When developing in the ESLint core repository, the `npm run perf` command gives a high-level overview of ESLint running time with all core rules enabled.
704+
The thing that makes ESLint different from other linters is the ability to define custom rules at runtime. This is perfect for rules that are specific to your project or company and wouldn't make sense for ESLint to ship with. With runtime rules, you don't have to wait for the next version of ESLint or be disappointed that your rule isn't general enough to apply to the larger JavaScript community, just write your rules and include them at runtime.
726705

727-
```bash
728-
$ git checkout main
729-
Switched to branch 'main'
730-
731-
$ npm run perf
732-
CPU Speed is 2200 with multiplier 7500000
733-
Performance Run #1: 1394.689313ms
734-
Performance Run #2: 1423.295351ms
735-
Performance Run #3: 1385.09515ms
736-
Performance Run #4: 1382.406982ms
737-
Performance Run #5: 1409.68566ms
738-
Performance budget ok: 1394.689313ms (limit: 3409.090909090909ms)
739-
740-
$ git checkout my-rule-branch
741-
Switched to branch 'my-rule-branch'
742-
743-
$ npm run perf
744-
CPU Speed is 2200 with multiplier 7500000
745-
Performance Run #1: 1443.736547ms
746-
Performance Run #2: 1419.193291ms
747-
Performance Run #3: 1436.018228ms
748-
Performance Run #4: 1473.605485ms
749-
Performance Run #5: 1457.455283ms
750-
Performance budget ok: 1443.736547ms (limit: 3409.090909090909ms)
751-
```
706+
Runtime rules are written in the same format as all other rules. Create your rule as you would any other and then follow these steps:
707+
708+
1. Place all of your runtime rules in the same directory (e.g., `eslint_rules`).
709+
2. Create a [configuration file](../use/configure/) and specify your rule ID error level under the `rules` key. Your rule will not run unless it has a value of `"warn"` or `"error"` in the configuration file.
710+
3. Run the [command line interface](../use/command-line-interface) using the `--rulesdir` option to specify the location of your runtime rules.
752711

753-
### Per-rule Performance
712+
## Profile Rule Performance
754713

755-
ESLint has a built-in method to track performance of individual rules. Setting the `TIMING` environment variable will trigger the display, upon linting completion, of the ten longest-running rules, along with their individual running time (rule creation + rule execution) and relative performance impact as a percentage of total rule processing time (rule creation + rule execution).
714+
ESLint has a built-in method to track the performance of individual rules. Setting the `TIMING` environment variable will trigger the display, upon linting completion, of the ten longest-running rules, along with their individual running time (rule creation + rule execution) and relative performance impact as a percentage of total rule processing time (rule creation + rule execution).
756715

757716
```bash
758717
$ TIMING=1 eslint lib
@@ -780,21 +739,3 @@ quotes | 18.066 | 100.0%
780739
```
781740

782741
To see a longer list of results (more than 10), set the environment variable to another value such as `TIMING=50` or `TIMING=all`.
783-
784-
## Rule Naming Conventions
785-
786-
The rule naming conventions for ESLint are fairly simple:
787-
788-
* If your rule is disallowing something, prefix it with `no-` such as `no-eval` for disallowing `eval()` and `no-debugger` for disallowing `debugger`.
789-
* If your rule is enforcing the inclusion of something, use a short name without a special prefix.
790-
* Use dashes between words.
791-
792-
## Runtime Rules
793-
794-
The thing that makes ESLint different from other linters is the ability to define custom rules at runtime. This is perfect for rules that are specific to your project or company and wouldn't make sense for ESLint to ship with. With runtime rules, you don't have to wait for the next version of ESLint or be disappointed that your rule isn't general enough to apply to the larger JavaScript community, just write your rules and include them at runtime.
795-
796-
Runtime rules are written in the same format as all other rules. Create your rule as you would any other and then follow these steps:
797-
798-
1. Place all of your runtime rules in the same directory (e.g., `eslint_rules`).
799-
2. Create a [configuration file](../use/configure/) and specify your rule ID error level under the `rules` key. Your rule will not run unless it has a value of `"warn"` or `"error"` in the configuration file.
800-
3. Run the [command line interface](../use/command-line-interface) using the `--rulesdir` option to specify the location of your runtime rules.

docs/src/extend/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ You've developed custom rules for ESLint and you want to share them with the com
2323

2424
## [Custom Rules](custom-rules)
2525

26-
This section explains how to create and modify rules to use with ESLint.
26+
This section explains how to create custom rules to use with ESLint.
2727

2828
## [Custom Formatters](custom-formatters)
2929

0 commit comments

Comments
 (0)