Skip to content

Commit 52de665

Browse files
committed
Extract isAnnotatedFunctionPropsDeclaration for reuse
1 parent 51ad714 commit 52de665

File tree

3 files changed

+32
-38
lines changed

3 files changed

+32
-38
lines changed

lib/rules/no-unused-prop-types.js

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
var Components = require('../util/Components');
1111
var variable = require('../util/variable');
12+
var annotations = require('../util/annotations');
1213

1314
// ------------------------------------------------------------------------------
1415
// Constants
@@ -108,24 +109,6 @@ module.exports = {
108109
return false;
109110
}
110111

111-
/**
112-
* Checks if we are declaring a `props` argument with a flow type annotation.
113-
* @param {ASTNode} node The AST node being checked.
114-
* @returns {Boolean} True if the node is a type annotated props declaration, false if not.
115-
*/
116-
function isAnnotatedFunctionPropsDeclaration(node) {
117-
if (node && node.params && node.params.length) {
118-
var tokens = context.getFirstTokens(node.params[0], 2);
119-
var isAnnotated = node.params[0].typeAnnotation;
120-
var isDestructuredProps = node.params[0].type === 'ObjectPattern';
121-
var isProps = tokens[0].value === 'props' || (tokens[1] && tokens[1].value === 'props');
122-
if (isAnnotated && (isDestructuredProps || isProps)) {
123-
return true;
124-
}
125-
}
126-
return false;
127-
}
128-
129112
/**
130113
* Checks if we are declaring a prop
131114
* @param {ASTNode} node The AST node being checked.
@@ -799,7 +782,7 @@ module.exports = {
799782
* FunctionDeclaration, or FunctionExpression
800783
*/
801784
function markAnnotatedFunctionArgumentsAsDeclared(node) {
802-
if (!node.params || !node.params.length || !isAnnotatedFunctionPropsDeclaration(node)) {
785+
if (!node.params || !node.params.length || !annotations.isAnnotatedFunctionPropsDeclaration(node, context)) {
803786
return;
804787
}
805788
markPropTypesAsDeclared(node, resolveTypeAnnotation(node.params[0]));

lib/rules/prop-types.js

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
var Components = require('../util/Components');
1111
var variable = require('../util/variable');
12+
var annotations = require('../util/annotations');
1213

1314
// ------------------------------------------------------------------------------
1415
// Constants
@@ -114,24 +115,6 @@ module.exports = {
114115
return false;
115116
}
116117

117-
/**
118-
* Checks if we are declaring a `props` argument with a flow type annotation.
119-
* @param {ASTNode} node The AST node being checked.
120-
* @returns {Boolean} True if the node is a type annotated props declaration, false if not.
121-
*/
122-
function isAnnotatedFunctionPropsDeclaration(node) {
123-
if (node && node.params && node.params.length) {
124-
var tokens = context.getFirstTokens(node.params[0], 2);
125-
var isAnnotated = node.params[0].typeAnnotation;
126-
var isDestructuredProps = node.params[0].type === 'ObjectPattern';
127-
var isProps = tokens[0].value === 'props' || (tokens[1] && tokens[1].value === 'props');
128-
if (isAnnotated && (isDestructuredProps || isProps)) {
129-
return true;
130-
}
131-
}
132-
return false;
133-
}
134-
135118
/**
136119
* Checks if we are declaring a prop
137120
* @param {ASTNode} node The AST node being checked.
@@ -792,7 +775,7 @@ module.exports = {
792775
* FunctionDeclaration, or FunctionExpression
793776
*/
794777
function markAnnotatedFunctionArgumentsAsDeclared(node) {
795-
if (!node.params || !node.params.length || !isAnnotatedFunctionPropsDeclaration(node)) {
778+
if (!node.params || !node.params.length || !annotations.isAnnotatedFunctionPropsDeclaration(node, context)) {
796779
return;
797780
}
798781
markPropTypesAsDeclared(node, resolveTypeAnnotation(node.params[0]));

lib/util/annotations.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @fileoverview Utility functions for type annotation detection.
3+
* @author Yannick Croissant
4+
* @author Vitor Balocco
5+
*/
6+
'use strict';
7+
8+
/**
9+
* Checks if we are declaring a `props` argument with a flow type annotation.
10+
* @param {ASTNode} node The AST node being checked.
11+
* @returns {Boolean} True if the node is a type annotated props declaration, false if not.
12+
*/
13+
function isAnnotatedFunctionPropsDeclaration(node, context) {
14+
if (!node || !node.params || !node.params.length) {
15+
return false;
16+
}
17+
18+
var tokens = context.getFirstTokens(node.params[0], 2);
19+
var isAnnotated = node.params[0].typeAnnotation;
20+
var isDestructuredProps = node.params[0].type === 'ObjectPattern';
21+
var isProps = tokens[0].value === 'props' || (tokens[1] && tokens[1].value === 'props');
22+
23+
return (isAnnotated && (isDestructuredProps || isProps));
24+
}
25+
26+
module.exports = {
27+
isAnnotatedFunctionPropsDeclaration: isAnnotatedFunctionPropsDeclaration
28+
};

0 commit comments

Comments
 (0)