Skip to content

Commit

Permalink
Add some notes to new-rule.md (#2287)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
fisker and sindresorhus authored Feb 23, 2024
1 parent 8f0ee89 commit 78810a5
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions docs/new-rule.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,72 @@ Use the [`astexplorer` site](https://astexplorer.net) with the `espree` parser a
- Open a pull request with a title in exactly the format `` Add `rule-name` rule ``, for example, `` Add `no-unused-properties` rule ``.
- The pull request description should include the issue it fixes, for example, `Fixes #123`.
- 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_.

## Implementation note

1. Try your best to provide an autofix if possible.
1. Try to provide a suggestion if an autofix is not possible.
1. Make sure the autofix doesn't change the runtime result.
1. Make sure the suggestion doesn't cause a syntax error.
1. Make sure that edge cases needing parentheses are considered in the fix function.

```js
const foo = 1;
foo.toString()
```

When changing `foo` to something else, make sure it works without `()`.

```js
// Good
(1).toString()

// Bad, will cause syntax error
1.toString()
```

1. Make sure that edge cases needing leading semicolons are considered in the fix function.

```js
foo
const bar = [1]
bar.forEach(number => {
console.log(number)
})
```

When changing `bar` to something that starts with `[` or `(`.

```js
// Good
foo
;[1].forEach(number => {
console.log(number)
})

// Bad
foo
[1].forEach(number => {
console.log(number)
})
```

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.

The following is valid JavaScript code:

```js
for(const{foo}of[]);
```

When replacing `{foo}` with something that starts with a letter, space around is needed.

```js
// Good
for(const foo of[]);

// Bad
for(constfooof[]);
```

1. Try not to remove comments in the fix function.

0 comments on commit 78810a5

Please sign in to comment.