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

Fix handling of identifiers #1356

Merged
merged 5 commits into from
Aug 15, 2017
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion lib/rules/default-props-match-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ module.exports = {
propWrapperFunctions.has(node.callee.name) &&
node.arguments && node.arguments[0]
) {
return node.arguments[0];
return resolveNodeValue(node.arguments[0]);
}
return node;
}
Expand Down
82 changes: 48 additions & 34 deletions lib/rules/forbid-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
*/
'use strict';

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

// ------------------------------------------------------------------------------
// Constants
// ------------------------------------------------------------------------------
Expand Down Expand Up @@ -67,14 +69,33 @@ module.exports = {
);
}

/**
* Find a variable by name in the current scope.
* @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;
}


/**
* Checks if propTypes declarations are forbidden
* @param {Array} declarations The array of AST nodes being checked.
* @returns {void}
*/
function checkForbidden(declarations) {
(declarations || []).forEach(declaration => {
function checkProperties(declarations) {
declarations.forEach(declaration => {
if (declaration.type !== 'Property') {
return;
}
Expand Down Expand Up @@ -108,49 +129,42 @@ module.exports = {
});
}

function checkNode(node) {
switch (node && node.type) {
case 'ObjectExpression':
checkProperties(node.properties);
break;
case 'Identifier':
const propTypesObject = findVariableByName(node.name);
if (propTypesObject && propTypesObject.properties) {
checkProperties(propTypesObject.properties);
}
break;
case 'CallExpression':
const innerNode = node.arguments && node.arguments[0];
if (propWrapperFunctions.has(node.callee.name) && innerNode) {
checkNode(innerNode);
}
break;
default:
break;
}
}

return {
ClassProperty: function(node) {
if (!isPropTypesDeclaration(node)) {
return;
}
switch (node.value && node.value.type) {
case 'ObjectExpression':
checkForbidden(node.value.properties);
break;
case 'CallExpression':
if (
propWrapperFunctions.has(node.value.callee.name) &&
node.value.arguments && node.value.arguments[0]
) {
checkForbidden(node.value.arguments[0].properties);
}
break;
default:
break;
}
checkNode(node.value);
},

MemberExpression: function(node) {
if (!isPropTypesDeclaration(node.property)) {
return;
}

const right = node.parent.right;
switch (right && right.type) {
case 'ObjectExpression':
checkForbidden(right.properties);
break;
case 'CallExpression':
if (
propWrapperFunctions.has(right.callee.name) &&
right.arguments && right.arguments[0]
) {
checkForbidden(right.arguments[0].properties);
}
break;
default:
break;
}
checkNode(node.parent.right);
},

ObjectExpression: function(node) {
Expand All @@ -163,7 +177,7 @@ module.exports = {
return;
}
if (property.value.type === 'ObjectExpression') {
checkForbidden(property.value.properties);
checkProperties(property.value.properties);
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/require-default-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ module.exports = {
propWrapperFunctions.has(node.callee.name) &&
node.arguments && node.arguments[0]
) {
return node.arguments[0];
return resolveNodeValue(node.arguments[0]);
}

return node;
Expand Down
89 changes: 44 additions & 45 deletions lib/rules/sort-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,25 @@ module.exports = {
return getValueName(node) === 'isRequired';
}

/**
* Find a variable by name in the current scope.
* @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;
}

/**
* Checks if propTypes declarations are sorted
* @param {Array} declarations The array of AST nodes being checked.
Expand Down Expand Up @@ -143,62 +162,42 @@ module.exports = {
}, declarations[0]);
}

function checkNode(node) {
switch (node && node.type) {
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;
default:
break;
}
}

return {
ClassProperty: function(node) {
if (!isPropTypesDeclaration(node)) {
return;
}
switch (node.value && node.value.type) {
case 'ObjectExpression':
checkSorted(node.value.properties);
break;
case 'CallExpression':
if (
propWrapperFunctions.has(node.value.callee.name) &&
node.value.arguments && node.value.arguments[0]
) {
checkSorted(node.value.arguments[0].properties);
}
break;
default:
break;
}
checkNode(node.value);
},

MemberExpression: function(node) {
if (!isPropTypesDeclaration(node.property)) {
return;
}
const right = node.parent.right;
let declarations;
switch (right && right.type) {
case 'CallExpression':
if (
propWrapperFunctions.has(right.callee.name) &&
right.arguments && right.arguments[0]
) {
declarations = right.arguments[0].properties;
}
break;
case 'ObjectExpression':
declarations = right.properties;
break;
case 'Identifier':
const variable = variableUtil.variablesInScope(context).find(item => item.name === right.name);
if (
!variable || !variable.defs[0] ||
!variable.defs[0].node.init || !variable.defs[0].node.init.properties
) {
break;
}
declarations = variable.defs[0].node.init.properties;
break;
default:
break;
}
if (declarations) {
checkSorted(declarations);
}

checkNode(node.parent.right);
},

ObjectExpression: function(node) {
Expand Down
23 changes: 23 additions & 0 deletions tests/lib/rules/default-props-match-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,29 @@ ruleTester.run('default-props-match-prop-types', rule, {
column: 3
}]
},
{
code: [
'function MyStatelessComponent({ foo, bar }) {',
' return <div>{foo}{bar}</div>;',
'}',
'const propTypes = {',
' foo: React.PropTypes.string,',
' bar: React.PropTypes.string.isRequired',
'};',
'MyStatelessComponent.propTypes = forbidExtraProps(propTypes);',
'MyStatelessComponent.defaultProps = {',
' baz: "baz"',
'};'
].join('\n'),
settings: {
propWrapperFunctions: ['forbidExtraProps']
},
errors: [{
message: 'defaultProp "baz" has no corresponding propTypes declaration.',
line: 10,
column: 3
}]
},
{
code: [
'function MyStatelessComponent({ foo, bar }) {',
Expand Down
11 changes: 9 additions & 2 deletions tests/lib/rules/forbid-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,15 +398,22 @@ ruleTester.run('forbid-prop-types', rule, {
settings: {
propWrapperFunctions: ['forbidExtraProps']
}
}, {
code: [
'import { forbidExtraProps } from "airbnb-prop-types";',
'export const propTypes = {dpm: PropTypes.any};',
'export default function Component() {}',
'Component.propTypes = propTypes;'
].join('\n'),
errors: [{message: ANY_ERROR_MESSAGE}]
}, {
code: [
'import { forbidExtraProps } from "airbnb-prop-types";',
'export const propTypes = {a: PropTypes.any};',
'export default function Component() {}',
'Component.propTypes = forbidExtraProps(propTypes);'
].join('\n'),
// errors: [{message: ANY_ERROR_MESSAGE}], // TODO: make this pass
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<3

errors: [],
errors: [{message: ANY_ERROR_MESSAGE}],
settings: {
propWrapperFunctions: ['forbidExtraProps']
}
Expand Down
20 changes: 20 additions & 0 deletions tests/lib/rules/require-default-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,26 @@ ruleTester.run('require-default-props', rule, {
propWrapperFunctions: ['forbidExtraProps']
}
},
{
code: [
'function MyStatelessComponent({ foo, bar }) {',
' return <div>{foo}{bar}</div>;',
'}',
'const propTypes = {',
' foo: PropTypes.string,',
' bar: PropTypes.string.isRequired',
'};',
'MyStatelessComponent.propTypes = forbidExtraProps(propTypes);'
].join('\n'),
errors: [{
message: 'propType "foo" is not required, but has no corresponding defaultProp declaration.',
line: 5,
column: 3
}],
settings: {
propWrapperFunctions: ['forbidExtraProps']
}
},
{
code: [
'function MyStatelessComponent({ foo, bar }) {',
Expand Down
36 changes: 36 additions & 0 deletions tests/lib/rules/sort-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,42 @@ ruleTester.run('sort-prop-types', rule, {
column: 5,
type: 'Property'
}]
}, {
code: [
'const First = (props) => <div />;',
'const propTypes = {',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you also (just in case) add a test with export const propTypes = … to this rule too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, just so I can understand, what behavior will that change be verifying?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hopefully nothing; I'm just making sure that an export declaration works just as well as a variable declaration :-)

' z: PropTypes.string,',
' a: PropTypes.any,',
'};',
'First.propTypes = forbidExtraProps(propTypes);'
].join('\n'),
settings: {
propWrapperFunctions: ['forbidExtraProps']
},
errors: [{
message: ERROR_MESSAGE,
line: 4,
column: 5,
type: 'Property'
}]
}, {
code: [
'const First = (props) => <div />;',
'const propTypes = {',
' z: PropTypes.string,',
' a: PropTypes.any,',
'};',
'First.propTypes = propTypes;'
].join('\n'),
settings: {
propWrapperFunctions: ['forbidExtraProps']
},
errors: [{
message: ERROR_MESSAGE,
line: 4,
column: 5,
type: 'Property'
}]
}, {
code: [
'var First = createReactClass({',
Expand Down