Skip to content

Commit 78810a5

Browse files
fiskersindresorhus
andauthored
Add some notes to new-rule.md (#2287)
Co-authored-by: Sindre Sorhus <[email protected]>
1 parent 8f0ee89 commit 78810a5

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

docs/new-rule.md

+69
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,72 @@ Use the [`astexplorer` site](https://astexplorer.net) with the `espree` parser a
2323
- Open a pull request with a title in exactly the format `` Add `rule-name` rule ``, for example, `` Add `no-unused-properties` rule ``.
2424
- The pull request description should include the issue it fixes, for example, `Fixes #123`.
2525
- Run `npm run run-rules-on-codebase` to run the rules against codebase to ensure code in the repository are following your rule, _you can ignore this step until your PR is reviewed_.
26+
27+
## Implementation note
28+
29+
1. Try your best to provide an autofix if possible.
30+
1. Try to provide a suggestion if an autofix is not possible.
31+
1. Make sure the autofix doesn't change the runtime result.
32+
1. Make sure the suggestion doesn't cause a syntax error.
33+
1. Make sure that edge cases needing parentheses are considered in the fix function.
34+
35+
```js
36+
const foo = 1;
37+
foo.toString()
38+
```
39+
40+
When changing `foo` to something else, make sure it works without `()`.
41+
42+
```js
43+
// Good
44+
(1).toString()
45+
46+
// Bad, will cause syntax error
47+
1.toString()
48+
```
49+
50+
1. Make sure that edge cases needing leading semicolons are considered in the fix function.
51+
52+
```js
53+
foo
54+
const bar = [1]
55+
bar.forEach(number => {
56+
console.log(number)
57+
})
58+
```
59+
60+
When changing `bar` to something that starts with `[` or `(`.
61+
62+
```js
63+
// Good
64+
foo
65+
;[1].forEach(number => {
66+
console.log(number)
67+
})
68+
69+
// Bad
70+
foo
71+
[1].forEach(number => {
72+
console.log(number)
73+
})
74+
```
75+
76+
1. If replacing a node that starts or ends with a symbol like `{`, make sure to add space before if the replacement starts with a letter.
77+
78+
The following is valid JavaScript code:
79+
80+
```js
81+
for(const{foo}of[]);
82+
```
83+
84+
When replacing `{foo}` with something that starts with a letter, space around is needed.
85+
86+
```js
87+
// Good
88+
for(const foo of[]);
89+
90+
// Bad
91+
for(constfooof[]);
92+
```
93+
94+
1. Try not to remove comments in the fix function.

0 commit comments

Comments
 (0)