version.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /**
  2. * @fileoverview Utility functions for React and Flow version configuration
  3. * @author Yannick Croissant
  4. */
  5. 'use strict';
  6. function getReactVersionFromContext(context) {
  7. let confVer = '999.999.999';
  8. // .eslintrc shared settings (http://eslint.org/docs/user-guide/configuring#adding-shared-settings)
  9. if (context.settings.react && context.settings.react.version) {
  10. confVer = context.settings.react.version;
  11. }
  12. confVer = /^[0-9]+\.[0-9]+$/.test(confVer) ? `${confVer}.0` : confVer;
  13. return confVer.split('.').map(part => Number(part));
  14. }
  15. function getFlowVersionFromContext(context) {
  16. let confVer = '999.999.999';
  17. // .eslintrc shared settings (http://eslint.org/docs/user-guide/configuring#adding-shared-settings)
  18. if (context.settings.react && context.settings.react.flowVersion) {
  19. confVer = context.settings.react.flowVersion;
  20. } else {
  21. throw 'Could not retrieve flowVersion from settings';
  22. }
  23. confVer = /^[0-9]+\.[0-9]+$/.test(confVer) ? `${confVer}.0` : confVer;
  24. return confVer.split('.').map(part => Number(part));
  25. }
  26. function test(context, methodVer, confVer) {
  27. methodVer = String(methodVer || '').split('.').map(part => Number(part));
  28. const higherMajor = methodVer[0] < confVer[0];
  29. const higherMinor = methodVer[0] === confVer[0] && methodVer[1] < confVer[1];
  30. const higherOrEqualPatch = methodVer[0] === confVer[0] && methodVer[1] === confVer[1] && methodVer[2] <= confVer[2];
  31. return higherMajor || higherMinor || higherOrEqualPatch;
  32. }
  33. function testReactVersion(context, methodVer) {
  34. return test(context, methodVer, getReactVersionFromContext(context));
  35. }
  36. function testFlowVersion(context, methodVer) {
  37. return test(context, methodVer, getFlowVersionFromContext(context));
  38. }
  39. module.exports = {
  40. testReactVersion,
  41. testFlowVersion
  42. };