version.js
4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
* @fileoverview Utility functions for React and Flow version configuration
* @author Yannick Croissant
*/
'use strict';
const resolve = require('resolve');
const error = require('./error');
let warnedForMissingVersion = false;
function resetWarningFlag() {
warnedForMissingVersion = false;
}
let cachedDetectedReactVersion;
function resetDetectedVersion() {
cachedDetectedReactVersion = undefined;
}
function detectReactVersion() {
if (cachedDetectedReactVersion) {
return cachedDetectedReactVersion;
}
try {
const reactPath = resolve.sync('react', {basedir: process.cwd()});
const react = require(reactPath); // eslint-disable-line global-require, import/no-dynamic-require
cachedDetectedReactVersion = react.version;
return cachedDetectedReactVersion;
} catch (e) {
if (e.code === 'MODULE_NOT_FOUND') {
if (!warnedForMissingVersion) {
error('Warning: React version was set to "detect" in eslint-plugin-react settings, '
+ 'but the "react" package is not installed. Assuming latest React version for linting.');
warnedForMissingVersion = true;
}
cachedDetectedReactVersion = '999.999.999';
return cachedDetectedReactVersion;
}
throw e;
}
}
function getReactVersionFromContext(context) {
let confVer = '999.999.999';
// .eslintrc shared settings (http://eslint.org/docs/user-guide/configuring#adding-shared-settings)
if (context.settings && context.settings.react && context.settings.react.version) {
let settingsVersion = context.settings.react.version;
if (settingsVersion === 'detect') {
settingsVersion = detectReactVersion();
}
if (typeof settingsVersion !== 'string') {
error('Warning: React version specified in eslint-plugin-react-settings must be a string; '
+ `got “${typeof settingsVersion}”`);
}
confVer = String(settingsVersion);
} else if (!warnedForMissingVersion) {
error('Warning: React version not specified in eslint-plugin-react settings. '
+ 'See https://github.com/yannickcr/eslint-plugin-react#configuration .');
warnedForMissingVersion = true;
}
confVer = /^[0-9]+\.[0-9]+$/.test(confVer) ? `${confVer}.0` : confVer;
return confVer.split('.').map((part) => Number(part));
}
function detectFlowVersion() {
try {
const flowPackageJsonPath = resolve.sync('flow-bin/package.json', {basedir: process.cwd()});
const flowPackageJson = require(flowPackageJsonPath); // eslint-disable-line global-require, import/no-dynamic-require
return flowPackageJson.version;
} catch (e) {
if (e.code === 'MODULE_NOT_FOUND') {
error('Warning: Flow version was set to "detect" in eslint-plugin-react settings, '
+ 'but the "flow-bin" package is not installed. Assuming latest Flow version for linting.');
return '999.999.999';
}
throw e;
}
}
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) {
let flowVersion = context.settings.react.flowVersion;
if (flowVersion === 'detect') {
flowVersion = detectFlowVersion();
}
if (typeof flowVersion !== 'string') {
error('Warning: Flow version specified in eslint-plugin-react-settings must be a string; '
+ `got “${typeof flowVersion}”`);
}
confVer = String(flowVersion);
} else {
throw 'Could not retrieve flowVersion from settings'; // eslint-disable-line no-throw-literal
}
confVer = /^[0-9]+\.[0-9]+$/.test(confVer) ? `${confVer}.0` : confVer;
return confVer.split('.').map((part) => Number(part));
}
function normalizeParts(parts) {
return Array.from({length: 3}, (_, i) => (parts[i] || 0));
}
function test(context, methodVer, confVer) {
const methodVers = normalizeParts(String(methodVer || '').split('.').map((part) => Number(part)));
const confVers = normalizeParts(confVer);
const higherMajor = methodVers[0] < confVers[0];
const higherMinor = methodVers[0] === confVers[0] && methodVers[1] < confVers[1];
const higherOrEqualPatch = methodVers[0] === confVers[0]
&& methodVers[1] === confVers[1]
&& methodVers[2] <= confVers[2];
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 = {
testReactVersion,
testFlowVersion,
resetWarningFlag,
resetDetectedVersion
};