Skip to content

Commit

Permalink
Add SFC examples in the documentation and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fsmaia authored and b0gok committed Jan 11, 2018
1 parent 5770c72 commit 4f00455
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 27 deletions.
14 changes: 14 additions & 0 deletions docs/rules/jsx-sort-default-props.md
Expand Up @@ -40,6 +40,13 @@ class Component extends React.Component {
return <div />;
}
}

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

The following patterns are considered okay and do not cause warnings:
Expand Down Expand Up @@ -76,6 +83,13 @@ class Component extends React.Component {
return <div />;
}
}

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

## Rule Options
Expand Down
54 changes: 27 additions & 27 deletions lib/rules/jsx-sort-default-props.js
Expand Up @@ -4,7 +4,7 @@
*/
'use strict';

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

// ------------------------------------------------------------------------------
// Rule Definition
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
20 changes: 20 additions & 0 deletions tests/lib/rules/jsx-sort-default-props.js
Expand Up @@ -474,5 +474,25 @@ ruleTester.run('jsx-sort-default-props', rule, {
column: 3,
type: 'Property'
}]
}, {
code: [
'const First = (props) => <div />;',
'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'
}]
}]
});

0 comments on commit 4f00455

Please sign in to comment.