From 4f004557ab980ae54b03e9dcdc3742835c48ecae Mon Sep 17 00:00:00 2001 From: Fernando Maia Date: Tue, 21 Nov 2017 12:01:32 -0200 Subject: [PATCH] Add SFC examples in the documentation and tests --- docs/rules/jsx-sort-default-props.md | 14 ++++++ lib/rules/jsx-sort-default-props.js | 54 +++++++++++------------ tests/lib/rules/jsx-sort-default-props.js | 20 +++++++++ 3 files changed, 61 insertions(+), 27 deletions(-) diff --git a/docs/rules/jsx-sort-default-props.md b/docs/rules/jsx-sort-default-props.md index b211eaf019..f7a3c498b7 100644 --- a/docs/rules/jsx-sort-default-props.md +++ b/docs/rules/jsx-sort-default-props.md @@ -40,6 +40,13 @@ class Component extends React.Component { return
; } } + +const Component = (props) => (...); +Component.defaultProps = { + z: "z", + y: "y", + a: "a" +}; ``` The following patterns are considered okay and do not cause warnings: @@ -76,6 +83,13 @@ class Component extends React.Component { return
; } } + +const Component = (props) => (...); +Component.defaultProps = { + a: "a", + y: "y", + z: "z" +}; ``` ## Rule Options diff --git a/lib/rules/jsx-sort-default-props.js b/lib/rules/jsx-sort-default-props.js index 935b0e0694..967e0e419f 100644 --- a/lib/rules/jsx-sort-default-props.js +++ b/lib/rules/jsx-sort-default-props.js @@ -4,7 +4,7 @@ */ 'use strict'; -// const variableUtil = require('../util/variable'); +const variableUtil = require('../util/variable'); // ------------------------------------------------------------------------------ // Rule Definition @@ -33,7 +33,7 @@ module.exports = { const sourceCode = context.getSourceCode(); const configuration = context.options[0] || {}; const ignoreCase = configuration.ignoreCase || false; - // const propWrapperFunctions = new Set(context.settings.propWrapperFunctions || []); + const propWrapperFunctions = new Set(context.settings.propWrapperFunctions || []); /** * Get properties name @@ -73,19 +73,19 @@ module.exports = { * @param {string} name Name of the variable to look for. * @returns {ASTNode|null} Return null if the variable could not be found, ASTNode otherwise. */ - // function findVariableByName(name) { - // const variable = variableUtil.variablesInScope(context).find(item => item.name === name); - // - // if (!variable || !variable.defs[0] || !variable.defs[0].node) { - // return null; - // } - // - // if (variable.defs[0].node.type === 'TypeAlias') { - // return variable.defs[0].node.right; - // } - // - // return variable.defs[0].node.init; - // } + function findVariableByName(name) { + const variable = variableUtil.variablesInScope(context).find(item => item.name === name); + + if (!variable || !variable.defs[0] || !variable.defs[0].node) { + return null; + } + + if (variable.defs[0].node.type === 'TypeAlias') { + return variable.defs[0].node.right; + } + + return variable.defs[0].node.init; + } /** * Checks if defaultProps declarations are sorted @@ -124,18 +124,18 @@ module.exports = { case 'ObjectExpression': checkSorted(node.properties); break; - // case 'Identifier': - // const propTypesObject = findVariableByName(node.name); - // if (propTypesObject && propTypesObject.properties) { - // checkSorted(propTypesObject.properties); - // } - // break; - // case 'CallExpression': - // const innerNode = node.arguments && node.arguments[0]; - // if (propWrapperFunctions.has(node.callee.name) && innerNode) { - // checkNode(innerNode); - // } - // break; + case 'Identifier': + const propTypesObject = findVariableByName(node.name); + if (propTypesObject && propTypesObject.properties) { + checkSorted(propTypesObject.properties); + } + break; + case 'CallExpression': + const innerNode = node.arguments && node.arguments[0]; + if (propWrapperFunctions.has(node.callee.name) && innerNode) { + checkNode(innerNode); + } + break; default: break; } diff --git a/tests/lib/rules/jsx-sort-default-props.js b/tests/lib/rules/jsx-sort-default-props.js index 5e81aebca6..93098bc6f4 100644 --- a/tests/lib/rules/jsx-sort-default-props.js +++ b/tests/lib/rules/jsx-sort-default-props.js @@ -474,5 +474,25 @@ ruleTester.run('jsx-sort-default-props', rule, { column: 3, type: 'Property' }] + }, { + code: [ + 'const First = (props) =>
;', + 'const propTypes = {', + ' z: PropTypes.string,', + ' a: PropTypes.any,', + '};', + 'const defaultProps = {', + ' z: "z",', + ' a: "a",', + '};', + 'First.propTypes = propTypes;', + 'First.defaultProps = defaultProps;' + ].join('\n'), + errors: [{ + message: ERROR_MESSAGE, + line: 8, + column: 3, + type: 'Property' + }] }] });