From 558576c7920c62df030c7d0819f544b4cabf9cdd Mon Sep 17 00:00:00 2001 From: Vladimir Kattsov Date: Mon, 15 Jan 2018 22:17:55 +0300 Subject: [PATCH] Add more tests with spread, add examples --- docs/rules/jsx-sort-default-props.md | 36 ++++++++++++ tests/lib/rules/jsx-sort-default-props.js | 70 +++++++++++++++++++++++ 2 files changed, 106 insertions(+) diff --git a/docs/rules/jsx-sort-default-props.md b/docs/rules/jsx-sort-default-props.md index 6c0e1725cc..f5a242eac2 100644 --- a/docs/rules/jsx-sort-default-props.md +++ b/docs/rules/jsx-sort-default-props.md @@ -47,6 +47,24 @@ Component.defaultProps = { 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
{a}{b}{c}
; +} +StatelessComponentWithSpreadInPropTypes.propTypes = types; +StatelessComponentWithSpreadInPropTypes.defaultProps = { + c: "c", + a: "a", + ...defaults, +}; ``` The following patterns are considered okay and do **not** cause warnings: @@ -90,6 +108,24 @@ Component.defaultProps = { 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
{a}{b}{c}
; +} +StatelessComponentWithSpreadInPropTypes.propTypes = types; +StatelessComponentWithSpreadInPropTypes.defaultProps = { + a: "a", + c: "c", + ...defaults, +}; ``` ## Rule Options diff --git a/tests/lib/rules/jsx-sort-default-props.js b/tests/lib/rules/jsx-sort-default-props.js index 93098bc6f4..18d544c023 100644 --- a/tests/lib/rules/jsx-sort-default-props.js +++ b/tests/lib/rules/jsx-sort-default-props.js @@ -262,6 +262,27 @@ ruleTester.run('jsx-sort-default-props', rule, { '}' ].join('\n'), parser: 'babel-eslint' + }, { + code: [ + 'const defaults = {', + ' b: "b"', + '};', + 'const types = {', + ' a: PropTypes.string,', + ' b: PropTypes.string,', + ' c: PropTypes.string', + '};', + 'function StatelessComponentWithSpreadInPropTypes({ a, b, c }) {', + ' return
{a}{b}{c}
;', + '}', + 'StatelessComponentWithSpreadInPropTypes.propTypes = types;', + 'StatelessComponentWithSpreadInPropTypes.defaultProps = {', + ' c: "c",', + ' ...defaults,', + ' a: "a"', + '};' + ].join('\n'), + parser: 'babel-eslint' }, { code: [ 'const propTypes = require(\'./externalPropTypes\')', @@ -494,5 +515,54 @@ ruleTester.run('jsx-sort-default-props', rule, { column: 3, type: 'Property' }] + }, { + code: [ + 'export default class ClassWithSpreadInPropTypes extends BaseClass {', + ' static propTypes = {', + ' b: PropTypes.string,', + ' ...c.propTypes,', + ' a: PropTypes.string', + ' }', + ' static defaultProps = {', + ' b: "b",', + ' a: "a",', + ' ...c.defaultProps', + ' }', + '}' + ].join('\n'), + parser: 'babel-eslint', + errors: [{ + message: ERROR_MESSAGE, + line: 9, + column: 5, + type: 'Property' + }] + }, { + code: [ + 'const defaults = {', + ' b: "b"', + '};', + 'const types = {', + ' a: PropTypes.string,', + ' b: PropTypes.string,', + ' c: PropTypes.string', + '};', + 'function StatelessComponentWithSpreadInPropTypes({ a, b, c }) {', + ' return
{a}{b}{c}
;', + '}', + 'StatelessComponentWithSpreadInPropTypes.propTypes = types;', + 'StatelessComponentWithSpreadInPropTypes.defaultProps = {', + ' c: "c",', + ' a: "a",', + ' ...defaults,', + '};' + ].join('\n'), + parser: 'babel-eslint', + errors: [{ + message: ERROR_MESSAGE, + line: 15, + column: 3, + type: 'Property' + }] }] });