Skip to content

Commit 69b1317

Browse files
committed
Fix default-props-match-prop-types type annotations detection with ESLint 3
1 parent b267d19 commit 69b1317

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

lib/rules/default-props-match-prop-types.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ module.exports = {
4141
const configuration = context.options[0] || {};
4242
const allowRequiredDefaults = configuration.allowRequiredDefaults || false;
4343
const propWrapperFunctions = new Set(context.settings.propWrapperFunctions || []);
44+
// Used to track the type annotations in scope.
45+
// Necessary because babel's scopes do not track type annotations.
46+
let stack = null;
4447

4548
/**
4649
* Try to resolve the node passed in to a variable in the current scope. If the node passed in is not
@@ -62,6 +65,22 @@ module.exports = {
6265
return node;
6366
}
6467

68+
/**
69+
* Helper for accessing the current scope in the stack.
70+
* @param {string} key The name of the identifier to access. If omitted, returns the full scope.
71+
* @param {ASTNode} value If provided sets the new value for the identifier.
72+
* @returns {Object|ASTNode} Either the whole scope or the ASTNode associated with the given identifier.
73+
*/
74+
function typeScope(key, value) {
75+
if (arguments.length === 0) {
76+
return stack[stack.length - 1];
77+
} else if (arguments.length === 1) {
78+
return stack[stack.length - 1][key];
79+
}
80+
stack[stack.length - 1][key] = value;
81+
return value;
82+
}
83+
6584
/**
6685
* Tries to find the definition of a GenericTypeAnnotation in the current scope.
6786
* @param {ASTNode} node The node GenericTypeAnnotation node to resolve.
@@ -72,7 +91,7 @@ module.exports = {
7291
return null;
7392
}
7493

75-
return variableUtil.findVariableByName(context, node.id.name);
94+
return variableUtil.findVariableByName(context, node.id.name) || typeScope(node.id.name);
7695
}
7796

7897
function resolveUnionTypeAnnotation(node) {
@@ -551,12 +570,29 @@ module.exports = {
551570
});
552571
},
553572

573+
TypeAlias: function(node) {
574+
typeScope(node.id.name, node.right);
575+
},
576+
577+
Program: function() {
578+
stack = [{}];
579+
},
580+
581+
BlockStatement: function () {
582+
stack.push(Object.create(typeScope()));
583+
},
584+
585+
'BlockStatement:exit': function () {
586+
stack.pop();
587+
},
588+
554589
// Check for type annotations in stateless components
555590
FunctionDeclaration: handleStatelessComponent,
556591
ArrowFunctionExpression: handleStatelessComponent,
557592
FunctionExpression: handleStatelessComponent,
558593

559594
'Program:exit': function() {
595+
stack = null;
560596
const list = components.list();
561597

562598
for (const component in list) {

0 commit comments

Comments
 (0)