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

[New] add sort-default-props and deprecate jsx-sort-default-props #1861

Merged
merged 1 commit into from Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -7,8 +7,10 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange

### Added
* [`hook-use-state`]: add `allowDestructuredState` option ([#3449][] @ljharb)
* add [`sort-default-props`] and deprecate [`jsx-sort-default-props`] ([#1861][] @alexzherdev)

[#3449]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3449
[#1861]: https://github.com/jsx-eslint/eslint-plugin-react/pull/1861

## [7.31.9] - 2022.10.09

Expand Down Expand Up @@ -4023,6 +4025,7 @@ If you're still not using React 15 you can keep the old behavior by setting the
[`require-render-return`]: docs/rules/require-render-return.md
[`self-closing-comp`]: docs/rules/self-closing-comp.md
[`sort-comp`]: docs/rules/sort-comp.md
[`sort-default-props`]: docs/rules/sort-default-props.md
[`sort-prop-types`]: docs/rules/sort-prop-types.md
[`state-in-constructor`]: docs/rules/state-in-constructor.md
[`static-property-placement`]: docs/rules/static-property-placement.md
Expand Down
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -172,6 +172,7 @@ Enable the rules that you would like to use.
|| | | [react/require-render-return](docs/rules/require-render-return.md) | Enforce ES5 or ES6 class for returning value in render function |
| | 🔧 | | [react/self-closing-comp](docs/rules/self-closing-comp.md) | Disallow extra closing tags for components without children |
| | | | [react/sort-comp](docs/rules/sort-comp.md) | Enforce component methods order |
| | | | [react/sort-default-props](docs/rules/sort-default-props.md) | Enforce defaultProps declarations alphabetical sorting |
| | | | [react/sort-prop-types](docs/rules/sort-prop-types.md) | Enforce propTypes declarations alphabetical sorting |
| | | | [react/state-in-constructor](docs/rules/state-in-constructor.md) | Enforce class component state initialization style |
| | | | [react/static-property-placement](docs/rules/static-property-placement.md) | Enforces where React component static properties should be positioned. |
Expand Down Expand Up @@ -216,7 +217,7 @@ Enable the rules that you would like to use.
| | | | [react/jsx-pascal-case](docs/rules/jsx-pascal-case.md) | Enforce PascalCase for user-defined JSX components |
| | 🔧 | | [react/jsx-props-no-multi-spaces](docs/rules/jsx-props-no-multi-spaces.md) | Disallow multiple spaces between inline JSX props |
| | | | [react/jsx-props-no-spreading](docs/rules/jsx-props-no-spreading.md) | Disallow JSX prop spreading |
| | | | [react/jsx-sort-default-props](docs/rules/jsx-sort-default-props.md) | Enforce defaultProps declarations alphabetical sorting |
| | | | [react/jsx-sort-default-props](docs/rules/jsx-sort-default-props.md) | Enforce defaultProps declarations alphabetical sorting. ❌ This rule is deprecated. |
| | 🔧 | | [react/jsx-sort-props](docs/rules/jsx-sort-props.md) | Enforce props alphabetical sorting |
| | 🔧 | | [react/jsx-space-before-closing](docs/rules/jsx-space-before-closing.md) | Enforce spacing before closing bracket in JSX. ❌ This rule is deprecated. |
| | 🔧 | | [react/jsx-tag-spacing](docs/rules/jsx-tag-spacing.md) | Enforce whitespace in and around the JSX opening and closing brackets |
Expand Down
2 changes: 1 addition & 1 deletion docs/rules/jsx-sort-default-props.md
@@ -1,6 +1,6 @@
# Enforce defaultProps declarations alphabetical sorting (react/jsx-sort-default-props)

💼 This rule is enabled in the following [configs](https://github.com/jsx-eslint/eslint-plugin-react#shareable-configurations): `all`.
This rule is deprecated. Please use the [`sort-default-props`](./sort-default-props.md) rule instead.

Some developers prefer to sort `defaultProps` declarations alphabetically to be able to find necessary declarations easier at a later time. Others feel that it adds complexity and becomes a burden to maintain.

Expand Down
187 changes: 187 additions & 0 deletions docs/rules/sort-default-props.md
@@ -0,0 +1,187 @@
# Enforce defaultProps declarations alphabetical sorting (react/sort-default-props)

💼 This rule is enabled in the following [configs](https://github.com/jsx-eslint/eslint-plugin-react#shareable-configurations): `all`.

Some developers prefer to sort `defaultProps` declarations alphabetically to be able to find necessary declarations easier at a later time. Others feel that it adds complexity and becomes a burden to maintain.

## Rule Details

This rule checks all components and verifies that all `defaultProps` declarations are sorted alphabetically. A spread attribute resets the verification. The default configuration of the rule is case-sensitive.

The following patterns are considered warnings:

```jsx
var Component = createReactClass({
...
getDefaultProps: function() {
return {
z: "z",
a: "a",
b: "b"
};
},
...
});

class Component extends React.Component {
...
}
Component.defaultProps = {
z: "z",
a: "a",
b: "b"
};

class Component extends React.Component {
static defaultProps = {
z: "z",
y: "y",
a: "a"
}
render() {
return <div />;
}
}

const Component = (props) => (...);
Component.defaultProps = {
z: "z",
y: "y",
a: "a"
};

const defaults = {
b: "b"
};
const types = {
a: PropTypes.string,
b: PropTypes.string,
c: PropTypes.string'
};
function StatelessComponentWithSpreadInPropTypes({ a, b, c }) {
return <div>{a}{b}{c}</div>;
}
StatelessComponentWithSpreadInPropTypes.propTypes = types;
StatelessComponentWithSpreadInPropTypes.defaultProps = {
c: "c",
a: "a",
...defaults,
};

export default class ClassWithSpreadInPropTypes extends BaseClass {
static propTypes = {
a: PropTypes.string,
b: PropTypes.string,
c: PropTypes.string,
d: PropTypes.string,
e: PropTypes.string,
f: PropTypes.string
}
static defaultProps = {
b: "b",
a: "a",
...c.defaultProps,
f: "f",
e: "e",
...d.defaultProps
}
}
```

The following patterns are considered okay and do **not** cause warnings:

```jsx
var Component = createReactClass({
...
getDefaultProps: function() {
return {
a: "a",
b: "b",
c: "c"
};
},
...
});

class Component extends React.Component {
...
}
Component.defaultProps = {
a: "a",
b: "b",
c: "c"
};

class Component extends React.Component {
static defaultProps = {
a: PropTypes.any,
b: PropTypes.any,
c: PropTypes.any
}
render() {
return <div />;
}
}

const Component = (props) => (...);
Component.defaultProps = {
a: "a",
y: "y",
z: "z"
};

const defaults = {
b: "b"
};
const types = {
a: PropTypes.string,
b: PropTypes.string,
c: PropTypes.string'
};
function StatelessComponentWithSpreadInPropTypes({ a, b, c }) {
return <div>{a}{b}{c}</div>;
}
StatelessComponentWithSpreadInPropTypes.propTypes = types;
StatelessComponentWithSpreadInPropTypes.defaultProps = {
a: "a",
c: "c",
...defaults,
};

export default class ClassWithSpreadInPropTypes extends BaseClass {
static propTypes = {
a: PropTypes.string,
b: PropTypes.string,
c: PropTypes.string,
d: PropTypes.string,
e: PropTypes.string,
f: PropTypes.string
}
static defaultProps = {
a: "a",
b: "b",
...c.defaultProps,
e: "e",
f: "f",
...d.defaultProps
}
}
```

## Rule Options

```js
...
"react/sort-default-props": [<enabled>, {
"ignoreCase": <boolean>,
}]
...
```

### `ignoreCase`

When `true` the rule ignores the case-sensitivity of the declarations order.

## When not to use

This rule is a formatting preference and not following it won't negatively affect the quality of your code. If alphabetizing `defaultProps` declarations isn't a part of your coding standards, then you can leave this rule off.
1 change: 1 addition & 0 deletions index.js
Expand Up @@ -98,6 +98,7 @@ const allRules = {
'require-render-return': require('./lib/rules/require-render-return'),
'self-closing-comp': require('./lib/rules/self-closing-comp'),
'sort-comp': require('./lib/rules/sort-comp'),
'sort-default-props': require('./lib/rules/sort-default-props'),
'sort-prop-types': require('./lib/rules/sort-prop-types'),
'state-in-constructor': require('./lib/rules/state-in-constructor'),
'static-property-placement': require('./lib/rules/static-property-placement'),
Expand Down
14 changes: 14 additions & 0 deletions lib/rules/jsx-sort-default-props.js
@@ -1,13 +1,17 @@
/**
* @fileoverview Enforce default props alphabetical sorting
* @author Vladimir Kattsov
* @deprecated
*/

'use strict';

const variableUtil = require('../util/variable');
const docsUrl = require('../util/docsUrl');
const report = require('../util/report');
const log = require('../util/log');

let isWarnedForDeprecation = false;

// ------------------------------------------------------------------------------
// Rule Definition
Expand All @@ -19,6 +23,7 @@ const messages = {

module.exports = {
meta: {
deprecated: true,
docs: {
description: 'Enforce defaultProps declarations alphabetical sorting',
category: 'Stylistic Issues',
Expand Down Expand Up @@ -168,6 +173,15 @@ module.exports = {

checkNode(node.parent.right);
},

Program() {
if (isWarnedForDeprecation) {
return;
}

log('The react/jsx-sort-default-props rule is deprecated. It has been renamed to `react/sort-default-props`.');
isWarnedForDeprecation = true;
},
};
},
};