Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some note to new-rule.md #2287

Merged
merged 3 commits into from
Feb 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Loading