Skip to content

Commit

Permalink
feat!: update migration rules list
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Oct 17, 2023
1 parent 32f1e27 commit e3de97f
Show file tree
Hide file tree
Showing 49 changed files with 1,346 additions and 4,189 deletions.
15 changes: 3 additions & 12 deletions packages/eslint-plugin-js/dts/rule-options.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { RuleOptions as ArrayBracketNewlineRuleOptions } from '../rules/array-bracket-newline/types'
import type { RuleOptions as ArrayBracketSpacingRuleOptions } from '../rules/array-bracket-spacing/types'
import type { RuleOptions as ArrayElementNewlineRuleOptions } from '../rules/array-element-newline/types'
import type { RuleOptions as ArrowParensRuleOptions } from '../rules/arrow-parens/types'
import type { RuleOptions as ArrowSpacingRuleOptions } from '../rules/arrow-spacing/types'
import type { RuleOptions as BlockSpacingRuleOptions } from '../rules/block-spacing/types'
import type { RuleOptions as BraceStyleRuleOptions } from '../rules/brace-style/types'
Expand All @@ -21,14 +22,11 @@ import type { RuleOptions as KeySpacingRuleOptions } from '../rules/key-spacing/
import type { RuleOptions as KeywordSpacingRuleOptions } from '../rules/keyword-spacing/types'
import type { RuleOptions as LinebreakStyleRuleOptions } from '../rules/linebreak-style/types'
import type { RuleOptions as LinesAroundCommentRuleOptions } from '../rules/lines-around-comment/types'
import type { RuleOptions as LinesAroundDirectiveRuleOptions } from '../rules/lines-around-directive/types'
import type { RuleOptions as LinesBetweenClassMembersRuleOptions } from '../rules/lines-between-class-members/types'
import type { RuleOptions as MaxLenRuleOptions } from '../rules/max-len/types'
import type { RuleOptions as MaxStatementsPerLineRuleOptions } from '../rules/max-statements-per-line/types'
import type { RuleOptions as MultilineTernaryRuleOptions } from '../rules/multiline-ternary/types'
import type { RuleOptions as NewParensRuleOptions } from '../rules/new-parens/types'
import type { RuleOptions as NewlineAfterVarRuleOptions } from '../rules/newline-after-var/types'
import type { RuleOptions as NewlineBeforeReturnRuleOptions } from '../rules/newline-before-return/types'
import type { RuleOptions as NewlinePerChainedCallRuleOptions } from '../rules/newline-per-chained-call/types'
import type { RuleOptions as NoConfusingArrowRuleOptions } from '../rules/no-confusing-arrow/types'
import type { RuleOptions as NoExtraParensRuleOptions } from '../rules/no-extra-parens/types'
Expand All @@ -38,7 +36,6 @@ import type { RuleOptions as NoMixedOperatorsRuleOptions } from '../rules/no-mix
import type { RuleOptions as NoMixedSpacesAndTabsRuleOptions } from '../rules/no-mixed-spaces-and-tabs/types'
import type { RuleOptions as NoMultiSpacesRuleOptions } from '../rules/no-multi-spaces/types'
import type { RuleOptions as NoMultipleEmptyLinesRuleOptions } from '../rules/no-multiple-empty-lines/types'
import type { RuleOptions as NoSpacedFuncRuleOptions } from '../rules/no-spaced-func/types'
import type { RuleOptions as NoTabsRuleOptions } from '../rules/no-tabs/types'
import type { RuleOptions as NoTrailingSpacesRuleOptions } from '../rules/no-trailing-spaces/types'
import type { RuleOptions as NoWhitespaceBeforePropertyRuleOptions } from '../rules/no-whitespace-before-property/types'
Expand Down Expand Up @@ -73,6 +70,7 @@ export interface RuleOptions {
'@stylistic/js/array-bracket-newline': ArrayBracketNewlineRuleOptions
'@stylistic/js/array-bracket-spacing': ArrayBracketSpacingRuleOptions
'@stylistic/js/array-element-newline': ArrayElementNewlineRuleOptions
'@stylistic/js/arrow-parens': ArrowParensRuleOptions
'@stylistic/js/arrow-spacing': ArrowSpacingRuleOptions
'@stylistic/js/block-spacing': BlockSpacingRuleOptions
'@stylistic/js/brace-style': BraceStyleRuleOptions
Expand All @@ -93,14 +91,11 @@ export interface RuleOptions {
'@stylistic/js/keyword-spacing': KeywordSpacingRuleOptions
'@stylistic/js/linebreak-style': LinebreakStyleRuleOptions
'@stylistic/js/lines-around-comment': LinesAroundCommentRuleOptions
'@stylistic/js/lines-around-directive': LinesAroundDirectiveRuleOptions
'@stylistic/js/lines-between-class-members': LinesBetweenClassMembersRuleOptions
'@stylistic/js/max-len': MaxLenRuleOptions
'@stylistic/js/max-statements-per-line': MaxStatementsPerLineRuleOptions
'@stylistic/js/multiline-ternary': MultilineTernaryRuleOptions
'@stylistic/js/new-parens': NewParensRuleOptions
'@stylistic/js/newline-after-var': NewlineAfterVarRuleOptions
'@stylistic/js/newline-before-return': NewlineBeforeReturnRuleOptions
'@stylistic/js/newline-per-chained-call': NewlinePerChainedCallRuleOptions
'@stylistic/js/no-confusing-arrow': NoConfusingArrowRuleOptions
'@stylistic/js/no-extra-parens': NoExtraParensRuleOptions
Expand All @@ -110,7 +105,6 @@ export interface RuleOptions {
'@stylistic/js/no-mixed-spaces-and-tabs': NoMixedSpacesAndTabsRuleOptions
'@stylistic/js/no-multi-spaces': NoMultiSpacesRuleOptions
'@stylistic/js/no-multiple-empty-lines': NoMultipleEmptyLinesRuleOptions
'@stylistic/js/no-spaced-func': NoSpacedFuncRuleOptions
'@stylistic/js/no-tabs': NoTabsRuleOptions
'@stylistic/js/no-trailing-spaces': NoTrailingSpacesRuleOptions
'@stylistic/js/no-whitespace-before-property': NoWhitespaceBeforePropertyRuleOptions
Expand Down Expand Up @@ -146,6 +140,7 @@ export interface UnprefixedRuleOptions {
'array-bracket-newline': ArrayBracketNewlineRuleOptions
'array-bracket-spacing': ArrayBracketSpacingRuleOptions
'array-element-newline': ArrayElementNewlineRuleOptions
'arrow-parens': ArrowParensRuleOptions
'arrow-spacing': ArrowSpacingRuleOptions
'block-spacing': BlockSpacingRuleOptions
'brace-style': BraceStyleRuleOptions
Expand All @@ -166,14 +161,11 @@ export interface UnprefixedRuleOptions {
'keyword-spacing': KeywordSpacingRuleOptions
'linebreak-style': LinebreakStyleRuleOptions
'lines-around-comment': LinesAroundCommentRuleOptions
'lines-around-directive': LinesAroundDirectiveRuleOptions
'lines-between-class-members': LinesBetweenClassMembersRuleOptions
'max-len': MaxLenRuleOptions
'max-statements-per-line': MaxStatementsPerLineRuleOptions
'multiline-ternary': MultilineTernaryRuleOptions
'new-parens': NewParensRuleOptions
'newline-after-var': NewlineAfterVarRuleOptions
'newline-before-return': NewlineBeforeReturnRuleOptions
'newline-per-chained-call': NewlinePerChainedCallRuleOptions
'no-confusing-arrow': NoConfusingArrowRuleOptions
'no-extra-parens': NoExtraParensRuleOptions
Expand All @@ -183,7 +175,6 @@ export interface UnprefixedRuleOptions {
'no-mixed-spaces-and-tabs': NoMixedSpacesAndTabsRuleOptions
'no-multi-spaces': NoMultiSpacesRuleOptions
'no-multiple-empty-lines': NoMultipleEmptyLinesRuleOptions
'no-spaced-func': NoSpacedFuncRuleOptions
'no-tabs': NoTabsRuleOptions
'no-trailing-spaces': NoTrailingSpacesRuleOptions
'no-whitespace-before-property': NoWhitespaceBeforePropertyRuleOptions
Expand Down
5 changes: 1 addition & 4 deletions packages/eslint-plugin-js/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
| [`@stylistic/js/array-bracket-newline`](./rules/array-bracket-newline) | Enforce linebreaks after opening and before closing array brackets || |
| [`@stylistic/js/array-bracket-spacing`](./rules/array-bracket-spacing) | Enforce consistent spacing inside array brackets || |
| [`@stylistic/js/array-element-newline`](./rules/array-element-newline) | Enforce line breaks after each array element || |
| [`@stylistic/js/arrow-parens`](./rules/arrow-parens) | Require parentheses around arrow function arguments || |
| [`@stylistic/js/arrow-spacing`](./rules/arrow-spacing) | Enforce consistent spacing before and after the arrow in arrow functions || |
| [`@stylistic/js/block-spacing`](./rules/block-spacing) | Disallow or enforce spaces inside of blocks after opening block and before closing block || |
| [`@stylistic/js/brace-style`](./rules/brace-style) | Enforce consistent brace style for blocks || |
Expand All @@ -25,14 +26,11 @@
| [`@stylistic/js/keyword-spacing`](./rules/keyword-spacing) | Enforce consistent spacing before and after keywords || |
| [`@stylistic/js/linebreak-style`](./rules/linebreak-style) | Enforce consistent linebreak style || |
| [`@stylistic/js/lines-around-comment`](./rules/lines-around-comment) | Require empty lines around comments || |
| [`@stylistic/js/lines-around-directive`](./rules/lines-around-directive) | Require or disallow newlines around directives || |
| [`@stylistic/js/lines-between-class-members`](./rules/lines-between-class-members) | Require or disallow an empty line between class members || |
| [`@stylistic/js/max-len`](./rules/max-len) | Enforce a maximum line length | | |
| [`@stylistic/js/max-statements-per-line`](./rules/max-statements-per-line) | Enforce a maximum number of statements allowed per line | | |
| [`@stylistic/js/multiline-ternary`](./rules/multiline-ternary) | Enforce newlines between operands of ternary expressions || |
| [`@stylistic/js/new-parens`](./rules/new-parens) | Enforce or disallow parentheses when invoking a constructor with no arguments || |
| [`@stylistic/js/newline-after-var`](./rules/newline-after-var) | Require or disallow an empty line after variable declarations || |
| [`@stylistic/js/newline-before-return`](./rules/newline-before-return) | Require an empty line before `return` statements || |
| [`@stylistic/js/newline-per-chained-call`](./rules/newline-per-chained-call) | Require a newline after each call in a method chain || |
| [`@stylistic/js/no-confusing-arrow`](./rules/no-confusing-arrow) | Disallow arrow functions where they could be confused with comparisons || |
| [`@stylistic/js/no-extra-parens`](./rules/no-extra-parens) | Disallow unnecessary parentheses || |
Expand All @@ -42,7 +40,6 @@
| [`@stylistic/js/no-mixed-spaces-and-tabs`](./rules/no-mixed-spaces-and-tabs) | Disallow mixed spaces and tabs for indentation | ||
| [`@stylistic/js/no-multi-spaces`](./rules/no-multi-spaces) | Disallow multiple spaces || |
| [`@stylistic/js/no-multiple-empty-lines`](./rules/no-multiple-empty-lines) | Disallow multiple empty lines || |
| [`@stylistic/js/no-spaced-func`](./rules/no-spaced-func) | Disallow spacing between function identifiers and their applications (deprecated) || |
| [`@stylistic/js/no-tabs`](./rules/no-tabs) | Disallow all tabs | | |
| [`@stylistic/js/no-trailing-spaces`](./rules/no-trailing-spaces) | Disallow trailing whitespace at the end of lines || |
| [`@stylistic/js/no-whitespace-before-property`](./rules/no-whitespace-before-property) | Disallow whitespace before properties || |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,15 +201,13 @@ module.exports = {
= options.objectsInArraysException && isObjectType(firstElement)
|| options.arraysInArraysException && isArrayType(firstElement)
|| options.singleElementException && node.elements.length === 1
? !options.spaced
: options.spaced
? !options.spaced : options.spaced

const closingBracketMustBeSpaced
= options.objectsInArraysException && isObjectType(lastElement)
|| options.arraysInArraysException && isArrayType(lastElement)
|| options.singleElementException && node.elements.length === 1
? !options.spaced
: options.spaced
? !options.spaced : options.spaced

if (astUtils.isTokenOnSameLine(first, second)) {
if (openingBracketMustBeSpaced && !sourceCode.isSpaceBetweenTokens(first, second))
Expand Down
254 changes: 254 additions & 0 deletions packages/eslint-plugin-js/rules/arrow-parens/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
---
title: arrow-parens
rule_type: layout
further_reading:
- https://github.com/airbnb/javascript#arrows--one-arg-parens
---



Arrow functions can omit parentheses when they have exactly one parameter. In all other cases the parameter(s) must
be wrapped in parentheses. This rule enforces the consistent use of parentheses in arrow functions.

## Rule Details

This rule enforces parentheses around arrow function parameters regardless of arity. For example:

```js
/*eslint-env es6*/

// Bad
a => {}

// Good
(a) => {}
```

Following this style will help you find arrow functions (`=>`) which may be mistakenly included in a condition
when a comparison such as `>=` was the intent.

```js
/*eslint-env es6*/

// Bad
if (a => 2) {
}

// Good
if (a >= 2) {
}
```

The rule can also be configured to discourage the use of parens when they are not required:

```js
/*eslint-env es6*/

// Bad
(a) => {}

// Good
a => {}
```

## Options

This rule has a string option and an object one.

String options are:

* `"always"` (default) requires parens around arguments in all cases.
* `"as-needed"` enforces no parens where they can be omitted.

Object properties for variants of the `"as-needed"` option:

* `"requireForBlockBody": true` modifies the as-needed rule in order to require parens if the function body is in an instructions block (surrounded by braces).

### always

Examples of **incorrect** code for this rule with the default `"always"` option:

:::incorrect

```js
/*eslint arrow-parens: ["error", "always"]*/
/*eslint-env es6*/

a => {};
a => a;
a => {'\n'};
a.then(foo => {});
a.then(foo => a);
a(foo => { if (true) {} });
```

:::

Examples of **correct** code for this rule with the default `"always"` option:

:::correct

```js
/*eslint arrow-parens: ["error", "always"]*/
/*eslint-env es6*/

() => {};
(a) => {};
(a) => a;
(a) => {'\n'}
a.then((foo) => {});
a.then((foo) => { if (true) {} });
```

:::

#### If Statements

One of the benefits of this option is that it prevents the incorrect use of arrow functions in conditionals:

```js
/*eslint-env es6*/

var a = 1;
var b = 2;
// ...
if (a => b) {
console.log('bigger');
} else {
console.log('smaller');
}
// outputs 'bigger', not smaller as expected
```

The contents of the `if` statement is an arrow function, not a comparison.

If the arrow function is intentional, it should be wrapped in parens to remove ambiguity.

```js
/*eslint-env es6*/

var a = 1;
var b = 0;
// ...
if ((a) => b) {
console.log('truthy value returned');
} else {
console.log('falsy value returned');
}
// outputs 'truthy value returned'
```

The following is another example of this behavior:

```js
/*eslint-env es6*/

var a = 1, b = 2, c = 3, d = 4;
var f = a => b ? c: d;
// f = ?
```

`f` is an arrow function which takes `a` as an argument and returns the result of `b ? c: d`.

This should be rewritten like so:

```js
/*eslint-env es6*/

var a = 1, b = 2, c = 3, d = 4;
var f = (a) => b ? c: d;
```

### as-needed

Examples of **incorrect** code for this rule with the `"as-needed"` option:

:::incorrect

```js
/*eslint arrow-parens: ["error", "as-needed"]*/
/*eslint-env es6*/

(a) => {};
(a) => a;
(a) => {'\n'};
a.then((foo) => {});
a.then((foo) => a);
a((foo) => { if (true) {} });
const f = /** @type {number} */(a) => a + a;
const g = /* comment */ (a) => a + a;
const h = (a) /* comment */ => a + a;
```

:::

Examples of **correct** code for this rule with the `"as-needed"` option:

:::correct

```js
/*eslint arrow-parens: ["error", "as-needed"]*/
/*eslint-env es6*/

() => {};
a => {};
a => a;
a => {'\n'};
a.then(foo => {});
a.then(foo => { if (true) {} });
(a, b, c) => a;
(a = 10) => a;
([a, b]) => a;
({a, b}) => a;
const f = (/** @type {number} */a) => a + a;
const g = (/* comment */ a) => a + a;
const h = (a /* comment */) => a + a;
```

:::

### requireForBlockBody

Examples of **incorrect** code for the `{ "requireForBlockBody": true }` option:

:::incorrect

```js
/*eslint arrow-parens: [2, "as-needed", { "requireForBlockBody": true }]*/
/*eslint-env es6*/

(a) => a;
a => {};
a => {'\n'};
a.map((x) => x * x);
a.map(x => {
return x * x;
});
a.then(foo => {});
```

:::

Examples of **correct** code for the `{ "requireForBlockBody": true }` option:

:::correct

```js
/*eslint arrow-parens: [2, "as-needed", { "requireForBlockBody": true }]*/
/*eslint-env es6*/

(a) => {};
(a) => {'\n'};
a => ({});
() => {};
a => a;
a.then((foo) => {});
a.then((foo) => { if (true) {} });
a((foo) => { if (true) {} });
(a, b, c) => a;
(a = 10) => a;
([a, b]) => a;
({a, b}) => a;
```

:::

0 comments on commit e3de97f

Please sign in to comment.