Skip to content

Commit 1ebfcee

Browse files
committed
Add flowVersion back but without a default. When there is no flow version, we detect the props argument position by the arguments length'
1 parent d232811 commit 1ebfcee

File tree

5 files changed

+40
-18
lines changed

5 files changed

+40
-18
lines changed

lib/rules/no-deprecated.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ module.exports = {
7878
return (
7979
deprecated &&
8080
deprecated[method] &&
81-
versionUtil.test(context, deprecated[method][0])
81+
versionUtil.testReactVersion(context, deprecated[method][0])
8282
);
8383
}
8484

lib/rules/no-render-return-value.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ module.exports = {
3535
}
3636

3737
let calleeObjectName = /^ReactDOM$/;
38-
if (versionUtil.test(context, '15.0.0')) {
38+
if (versionUtil.testReactVersion(context, '15.0.0')) {
3939
calleeObjectName = /^ReactDOM$/;
40-
} else if (versionUtil.test(context, '0.14.0')) {
40+
} else if (versionUtil.testReactVersion(context, '0.14.0')) {
4141
calleeObjectName = /^React(DOM)?$/;
42-
} else if (versionUtil.test(context, '0.13.0')) {
42+
} else if (versionUtil.testReactVersion(context, '0.13.0')) {
4343
calleeObjectName = /^React$/;
4444
}
4545

lib/rules/prefer-stateless-function.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ module.exports = {
371371
scope = scope.upper;
372372
}
373373
const isRender = blockNode && blockNode.key && blockNode.key.name === 'render';
374-
const allowNull = versionUtil.test(context, '15.0.0'); // Stateless components can return null since React 15
374+
const allowNull = versionUtil.testReactVersion(context, '15.0.0'); // Stateless components can return null since React 15
375375
const isReturningJSX = utils.isReturningJSX(node, !allowNull);
376376
const isReturningNull = node.argument && (node.argument.value === null || node.argument.value === false);
377377
if (

lib/rules/prop-types.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const has = require('has');
1111
const Components = require('../util/Components');
1212
const variable = require('../util/variable');
1313
const annotations = require('../util/annotations');
14+
const versionUtil = require('../util/version');
1415

1516
// ------------------------------------------------------------------------------
1617
// Constants
@@ -857,16 +858,17 @@ module.exports = {
857858
* @returns {ASTNode} The resolved type annotation for the node.
858859
*/
859860
function resolveSuperParameterPropsType(node) {
860-
let annotation;
861-
862-
// Flow <=0.52 had 3 required TypedParameters of which the second one is the Props.
863-
// Flow >=0.53 has 2 optional TypedParameters of which the first one is the Props.
864-
if (node.superTypeParameters.params.length <= 2) {
865-
annotation = node.superTypeParameters.params[0];
866-
} else {
867-
annotation = node.superTypeParameters.params[1];
861+
let propsParameterPosition;
862+
try {
863+
// Flow <=0.52 had 3 required TypedParameters of which the second one is the Props.
864+
// Flow >=0.53 has 2 optional TypedParameters of which the first one is the Props.
865+
propsParameterPosition = versionUtil.testFlowVersion(context, 0.53) ? 1 : 0;
866+
} catch (e) {
867+
// In case there is no flow version defined, we can safely assume that when there are 3 Props we are dealing with version <= 0.52
868+
propsParameterPosition = node.superTypeParameters.params.length <= 2 ? 0 : 1;
868869
}
869870

871+
let annotation = node.superTypeParameters.params[propsParameterPosition];
870872
while (annotation && (annotation.type === 'TypeAnnotation' || annotation.type === 'NullableTypeAnnotation')) {
871873
annotation = annotation.typeAnnotation;
872874
}

lib/util/version.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/**
2-
* @fileoverview Utility functions for React version configuration
2+
* @fileoverview Utility functions for React and Flow version configuration
33
* @author Yannick Croissant
44
*/
55
'use strict';
66

7-
function getFromContext(context) {
7+
function getReactVersionFromContext(context) {
88
let confVer = '999.999.999';
99
// .eslintrc shared settings (http://eslint.org/docs/user-guide/configuring#adding-shared-settings)
1010
if (context.settings.react && context.settings.react.version) {
@@ -14,8 +14,19 @@ function getFromContext(context) {
1414
return confVer.split('.').map(part => Number(part));
1515
}
1616

17-
function test(context, methodVer) {
18-
const confVer = getFromContext(context);
17+
function getFlowVersionFromContext(context) {
18+
let confVer = '999.999.999';
19+
// .eslintrc shared settings (http://eslint.org/docs/user-guide/configuring#adding-shared-settings)
20+
if (context.settings.react && context.settings.react.flowVersion) {
21+
confVer = context.settings.react.flowVersion;
22+
} else {
23+
throw 'Could not retrieve flowVersion from settings';
24+
}
25+
confVer = /^[0-9]+\.[0-9]+$/.test(confVer) ? `${confVer}.0` : confVer;
26+
return confVer.split('.').map(part => Number(part));
27+
}
28+
29+
function test(context, methodVer, confVer) {
1930
methodVer = methodVer.split('.').map(part => Number(part));
2031
const higherMajor = methodVer[0] < confVer[0];
2132
const higherMinor = methodVer[0] === confVer[0] && methodVer[1] < confVer[1];
@@ -24,6 +35,15 @@ function test(context, methodVer) {
2435
return higherMajor || higherMinor || higherOrEqualPatch;
2536
}
2637

38+
function testReactVersion(context, methodVer) {
39+
return test(context, methodVer, getReactVersionFromContext(context));
40+
}
41+
42+
function testFlowVersion(context, methodVer) {
43+
return test(context, methodVer, getFlowVersionFromContext(context));
44+
}
45+
2746
module.exports = {
28-
test: test
47+
testReactVersion,
48+
testFlowVersion
2949
};

0 commit comments

Comments
 (0)