Skip to content

Commit

Permalink
Add flowVersion back but without a default. When there is no flow ver…
Browse files Browse the repository at this point in the history
…sion, we detect the props argument position by the arguments length'
  • Loading branch information
jseminck committed Aug 21, 2017
1 parent d232811 commit 1ebfcee
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 18 deletions.
2 changes: 1 addition & 1 deletion lib/rules/no-deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ module.exports = {
return (
deprecated &&
deprecated[method] &&
versionUtil.test(context, deprecated[method][0])
versionUtil.testReactVersion(context, deprecated[method][0])
);
}

Expand Down
6 changes: 3 additions & 3 deletions lib/rules/no-render-return-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ module.exports = {
}

let calleeObjectName = /^ReactDOM$/;
if (versionUtil.test(context, '15.0.0')) {
if (versionUtil.testReactVersion(context, '15.0.0')) {
calleeObjectName = /^ReactDOM$/;
} else if (versionUtil.test(context, '0.14.0')) {
} else if (versionUtil.testReactVersion(context, '0.14.0')) {
calleeObjectName = /^React(DOM)?$/;
} else if (versionUtil.test(context, '0.13.0')) {
} else if (versionUtil.testReactVersion(context, '0.13.0')) {
calleeObjectName = /^React$/;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/rules/prefer-stateless-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ module.exports = {
scope = scope.upper;
}
const isRender = blockNode && blockNode.key && blockNode.key.name === 'render';
const allowNull = versionUtil.test(context, '15.0.0'); // Stateless components can return null since React 15
const allowNull = versionUtil.testReactVersion(context, '15.0.0'); // Stateless components can return null since React 15
const isReturningJSX = utils.isReturningJSX(node, !allowNull);
const isReturningNull = node.argument && (node.argument.value === null || node.argument.value === false);
if (
Expand Down
18 changes: 10 additions & 8 deletions lib/rules/prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const has = require('has');
const Components = require('../util/Components');
const variable = require('../util/variable');
const annotations = require('../util/annotations');
const versionUtil = require('../util/version');

// ------------------------------------------------------------------------------
// Constants
Expand Down Expand Up @@ -857,16 +858,17 @@ module.exports = {
* @returns {ASTNode} The resolved type annotation for the node.
*/
function resolveSuperParameterPropsType(node) {
let annotation;

// Flow <=0.52 had 3 required TypedParameters of which the second one is the Props.
// Flow >=0.53 has 2 optional TypedParameters of which the first one is the Props.
if (node.superTypeParameters.params.length <= 2) {
annotation = node.superTypeParameters.params[0];
} else {
annotation = node.superTypeParameters.params[1];
let propsParameterPosition;
try {
// Flow <=0.52 had 3 required TypedParameters of which the second one is the Props.
// Flow >=0.53 has 2 optional TypedParameters of which the first one is the Props.
propsParameterPosition = versionUtil.testFlowVersion(context, 0.53) ? 1 : 0;
} catch (e) {
// 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
propsParameterPosition = node.superTypeParameters.params.length <= 2 ? 0 : 1;
}

let annotation = node.superTypeParameters.params[propsParameterPosition];
while (annotation && (annotation.type === 'TypeAnnotation' || annotation.type === 'NullableTypeAnnotation')) {
annotation = annotation.typeAnnotation;
}
Expand Down
30 changes: 25 additions & 5 deletions lib/util/version.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/**
* @fileoverview Utility functions for React version configuration
* @fileoverview Utility functions for React and Flow version configuration
* @author Yannick Croissant
*/
'use strict';

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

function test(context, methodVer) {
const confVer = getFromContext(context);
function getFlowVersionFromContext(context) {
let confVer = '999.999.999';
// .eslintrc shared settings (http://eslint.org/docs/user-guide/configuring#adding-shared-settings)
if (context.settings.react && context.settings.react.flowVersion) {
confVer = context.settings.react.flowVersion;
} else {
throw 'Could not retrieve flowVersion from settings';
}
confVer = /^[0-9]+\.[0-9]+$/.test(confVer) ? `${confVer}.0` : confVer;
return confVer.split('.').map(part => Number(part));
}

function test(context, methodVer, confVer) {
methodVer = methodVer.split('.').map(part => Number(part));
const higherMajor = methodVer[0] < confVer[0];
const higherMinor = methodVer[0] === confVer[0] && methodVer[1] < confVer[1];
Expand All @@ -24,6 +35,15 @@ function test(context, methodVer) {
return higherMajor || higherMinor || higherOrEqualPatch;
}

function testReactVersion(context, methodVer) {
return test(context, methodVer, getReactVersionFromContext(context));
}

function testFlowVersion(context, methodVer) {
return test(context, methodVer, getFlowVersionFromContext(context));
}

module.exports = {
test: test
testReactVersion,
testFlowVersion
};

0 comments on commit 1ebfcee

Please sign in to comment.