eslint-plugin-react
Advanced tools
Comparing version 7.20.2 to 7.20.3
@@ -15,2 +15,4 @@ /** | ||
const getKeyValue = require('./ast').getKeyValue; | ||
const findReturnStatement = require('./ast').findReturnStatement; | ||
const isJSX = require('./jsx').isJSX; | ||
@@ -71,3 +73,21 @@ /** | ||
} | ||
return false; | ||
} | ||
/** | ||
* Checks if a node is a Function and return JSXElement | ||
* | ||
* @param {ASTNode} node the AST node being checked. | ||
* @returns {Boolean} True if the node is a Function and return JSXElement. | ||
*/ | ||
function isJSXFunctionComponent(node) { | ||
if (node.type === 'ArrowFunctionExpression' || node.type === 'FunctionDeclaration' || node.type === 'FunctionExpression') { | ||
const res = findReturnStatement(node); | ||
// If function return JSXElement with return keyword. | ||
if (res) { | ||
return isJSX(res.argument); | ||
} | ||
// If function return JSXElement without return keyword; | ||
return isJSX(node.body); | ||
} | ||
return false; | ||
@@ -220,3 +240,2 @@ } | ||
} | ||
return annotation; | ||
@@ -319,9 +338,19 @@ } | ||
foundDeclaredPropertiesList.forEach((tsPropertySignature) => { | ||
declaredPropTypes[tsPropertySignature.key.name] = { | ||
fullName: tsPropertySignature.key.name, | ||
name: tsPropertySignature.key.name, | ||
node: tsPropertySignature, | ||
isRequired: !tsPropertySignature.optional | ||
}; | ||
foundDeclaredPropertiesList.forEach((tsInterfaceBody) => { | ||
if (tsInterfaceBody.type === 'TSPropertySignature') { | ||
let accessor = 'name'; | ||
if (tsInterfaceBody.key.type === 'Literal') { | ||
if (typeof tsInterfaceBody.key.value === 'number') { | ||
accessor = 'raw'; | ||
} else { | ||
accessor = 'value'; | ||
} | ||
} | ||
declaredPropTypes[tsInterfaceBody.key[accessor]] = { | ||
fullName: tsInterfaceBody.key[accessor], | ||
name: tsInterfaceBody.key[accessor], | ||
node: tsInterfaceBody, | ||
isRequired: !tsInterfaceBody.optional | ||
}; | ||
} | ||
}); | ||
@@ -626,2 +655,7 @@ return false; | ||
// Should ignore function that not return JSXElement | ||
if (!isJSXFunctionComponent(node)) { | ||
return; | ||
} | ||
const param = node.params[0]; | ||
@@ -628,0 +662,0 @@ if (param.typeAnnotation && param.typeAnnotation.typeAnnotation && param.typeAnnotation.typeAnnotation.type === 'UnionTypeAnnotation') { |
@@ -212,33 +212,2 @@ /** | ||
/** | ||
* Retrieve the name of a property node | ||
* @param {ASTNode} node The AST node with the property. | ||
* @return {string|undefined} the name of the property or undefined if not found | ||
*/ | ||
function getPropertyName(node) { | ||
const property = node.property; | ||
if (property) { | ||
switch (property.type) { | ||
case 'Identifier': | ||
if (node.computed) { | ||
return '__COMPUTED_PROP__'; | ||
} | ||
return property.name; | ||
case 'MemberExpression': | ||
return; | ||
case 'Literal': | ||
// Accept computed properties that are literal strings | ||
if (typeof property.value === 'string') { | ||
return property.value; | ||
} | ||
// falls through | ||
default: | ||
if (node.computed) { | ||
return '__COMPUTED_PROP__'; | ||
} | ||
break; | ||
} | ||
} | ||
} | ||
/** | ||
* Checks if the node is a propTypes usage of the form `this.props.*`, `props.*`, `prevProps.*`, or `nextProps.*`. | ||
@@ -276,2 +245,42 @@ * @param {ASTNode} node | ||
/** | ||
* Retrieve the name of a property node | ||
* @param {ASTNode} node The AST node with the property. | ||
* @param {Context} context | ||
* @param {Object} utils | ||
* @param {boolean} checkAsyncSafeLifeCycles | ||
* @return {string|undefined} the name of the property or undefined if not found | ||
*/ | ||
function getPropertyName(node, context, utils, checkAsyncSafeLifeCycles) { | ||
const property = node.property; | ||
if (property) { | ||
switch (property.type) { | ||
case 'Identifier': | ||
if (node.computed) { | ||
return '__COMPUTED_PROP__'; | ||
} | ||
return property.name; | ||
case 'MemberExpression': | ||
return; | ||
case 'Literal': | ||
// Accept computed properties that are literal strings | ||
if (typeof property.value === 'string') { | ||
return property.value; | ||
} | ||
// Accept number as well but only accept props[123] | ||
if (typeof property.value === 'number') { | ||
if (isPropTypesUsageByMemberExpression(node, context, utils, checkAsyncSafeLifeCycles)) { | ||
return property.raw; | ||
} | ||
} | ||
// falls through | ||
default: | ||
if (node.computed) { | ||
return '__COMPUTED_PROP__'; | ||
} | ||
break; | ||
} | ||
} | ||
} | ||
module.exports = function usedPropTypesInstructions(context, components, utils) { | ||
@@ -298,3 +307,3 @@ const checkAsyncSafeLifeCycles = versionUtil.testReactVersion(context, '16.3.0'); | ||
case 'MemberExpression': | ||
name = getPropertyName(node); | ||
name = getPropertyName(node, context, utils, checkAsyncSafeLifeCycles); | ||
if (name) { | ||
@@ -301,0 +310,0 @@ allNames = parentNames.concat(name); |
{ | ||
"name": "eslint-plugin-react", | ||
"version": "7.20.2", | ||
"version": "7.20.3", | ||
"author": "Yannick Croissant <yannick.croissant+npm@gmail.com>", | ||
@@ -5,0 +5,0 @@ "description": "React specific linting rules for ESLint", |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
708491
15245