source: imaps-frontend/node_modules/eslint-plugin-react/lib/util/version.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 6.8 KB
Line 
1/**
2 * @fileoverview Utility functions for React and Flow version configuration
3 * @author Yannick Croissant
4 */
5
6'use strict';
7
8const fs = require('fs');
9const path = require('path');
10
11const resolve = require('resolve');
12const semver = require('semver');
13const error = require('./error');
14
15const ULTIMATE_LATEST_SEMVER = '999.999.999';
16
17let warnedForMissingVersion = false;
18
19function resetWarningFlag() {
20 warnedForMissingVersion = false;
21}
22
23let cachedDetectedReactVersion;
24
25function resetDetectedVersion() {
26 cachedDetectedReactVersion = undefined;
27}
28
29function resolveBasedir(contextOrFilename) {
30 if (contextOrFilename) {
31 const filename = typeof contextOrFilename === 'string' ? contextOrFilename : contextOrFilename.getFilename();
32 const dirname = path.dirname(filename);
33 try {
34 if (fs.statSync(filename).isFile()) {
35 // dirname must be dir here
36 return dirname;
37 }
38 } catch (err) {
39 // https://github.com/eslint/eslint/issues/11989
40 if (err.code === 'ENOTDIR') {
41 // virtual filename could be recursive
42 return resolveBasedir(dirname);
43 }
44 }
45 }
46 return process.cwd();
47}
48
49function convertConfVerToSemver(confVer) {
50 const fullSemverString = /^[0-9]+\.[0-9]+$/.test(confVer) ? `${confVer}.0` : confVer;
51 return semver.coerce(fullSemverString.split('.').map((part) => Number(part)).join('.'));
52}
53
54let defaultVersion = ULTIMATE_LATEST_SEMVER;
55
56function resetDefaultVersion() {
57 defaultVersion = ULTIMATE_LATEST_SEMVER;
58}
59
60function readDefaultReactVersionFromContext(context) {
61 // .eslintrc shared settings (https://eslint.org/docs/user-guide/configuring#adding-shared-settings)
62 if (context.settings && context.settings.react && context.settings.react.defaultVersion) {
63 let settingsDefaultVersion = context.settings.react.defaultVersion;
64 if (typeof settingsDefaultVersion !== 'string') {
65 error('Warning: default React version specified in eslint-pluigin-react-settings must be a string; '
66 + `got "${typeof settingsDefaultVersion}"`);
67 }
68 settingsDefaultVersion = String(settingsDefaultVersion);
69 const result = convertConfVerToSemver(settingsDefaultVersion);
70 if (result) {
71 defaultVersion = result.version;
72 } else {
73 error(`Warning: React version specified in eslint-plugin-react-settings must be a valid semver version, or "detect"; got “${settingsDefaultVersion}”. Falling back to latest version as default.`);
74 }
75 } else {
76 defaultVersion = ULTIMATE_LATEST_SEMVER;
77 }
78}
79
80// TODO, semver-major: remove context fallback
81function detectReactVersion(context) {
82 if (cachedDetectedReactVersion) {
83 return cachedDetectedReactVersion;
84 }
85
86 const basedir = resolveBasedir(context);
87
88 try {
89 const reactPath = resolve.sync('react', { basedir });
90 const react = require(reactPath); // eslint-disable-line global-require, import/no-dynamic-require
91 cachedDetectedReactVersion = react.version;
92 return cachedDetectedReactVersion;
93 } catch (e) {
94 if (e.code === 'MODULE_NOT_FOUND') {
95 if (!warnedForMissingVersion) {
96 let sentence2 = 'Assuming latest React version for linting.';
97 if (defaultVersion !== ULTIMATE_LATEST_SEMVER) {
98 sentence2 = `Assuming default React version for linting: "${defaultVersion}".`;
99 }
100 error(`Warning: React version was set to "detect" in eslint-plugin-react settings, but the "react" package is not installed. ${sentence2}`);
101 warnedForMissingVersion = true;
102 }
103 cachedDetectedReactVersion = defaultVersion;
104 return cachedDetectedReactVersion;
105 }
106 throw e;
107 }
108}
109
110function getReactVersionFromContext(context) {
111 readDefaultReactVersionFromContext(context);
112 let confVer = defaultVersion;
113 // .eslintrc shared settings (https://eslint.org/docs/user-guide/configuring#adding-shared-settings)
114 if (context.settings && context.settings.react && context.settings.react.version) {
115 let settingsVersion = context.settings.react.version;
116 if (settingsVersion === 'detect') {
117 settingsVersion = detectReactVersion(context);
118 }
119 if (typeof settingsVersion !== 'string') {
120 error('Warning: React version specified in eslint-plugin-react-settings must be a string; '
121 + `got “${typeof settingsVersion}”`);
122 }
123 confVer = String(settingsVersion);
124 } else if (!warnedForMissingVersion) {
125 error('Warning: React version not specified in eslint-plugin-react settings. '
126 + 'See https://github.com/jsx-eslint/eslint-plugin-react#configuration .');
127 warnedForMissingVersion = true;
128 }
129
130 const result = convertConfVerToSemver(confVer);
131 if (!result) {
132 error(`Warning: React version specified in eslint-plugin-react-settings must be a valid semver version, or "detect"; got “${confVer}”`);
133 }
134 return result ? result.version : defaultVersion;
135}
136
137// TODO, semver-major: remove context fallback
138function detectFlowVersion(context) {
139 const basedir = resolveBasedir(context);
140
141 try {
142 const flowPackageJsonPath = resolve.sync('flow-bin/package.json', { basedir });
143 const flowPackageJson = require(flowPackageJsonPath); // eslint-disable-line global-require, import/no-dynamic-require
144 return flowPackageJson.version;
145 } catch (e) {
146 if (e.code === 'MODULE_NOT_FOUND') {
147 error('Warning: Flow version was set to "detect" in eslint-plugin-react settings, '
148 + 'but the "flow-bin" package is not installed. Assuming latest Flow version for linting.');
149 return ULTIMATE_LATEST_SEMVER;
150 }
151 throw e;
152 }
153}
154
155function getFlowVersionFromContext(context) {
156 let confVer = defaultVersion;
157 // .eslintrc shared settings (https://eslint.org/docs/user-guide/configuring#adding-shared-settings)
158 if (context.settings.react && context.settings.react.flowVersion) {
159 let flowVersion = context.settings.react.flowVersion;
160 if (flowVersion === 'detect') {
161 flowVersion = detectFlowVersion(context);
162 }
163 if (typeof flowVersion !== 'string') {
164 error('Warning: Flow version specified in eslint-plugin-react-settings must be a string; '
165 + `got “${typeof flowVersion}”`);
166 }
167 confVer = String(flowVersion);
168 } else {
169 throw 'Could not retrieve flowVersion from settings'; // eslint-disable-line no-throw-literal
170 }
171
172 const result = convertConfVerToSemver(confVer);
173 if (!result) {
174 error(`Warning: Flow version specified in eslint-plugin-react-settings must be a valid semver version, or "detect"; got “${confVer}”`);
175 }
176 return result ? result.version : defaultVersion;
177}
178
179function test(semverRange, confVer) {
180 return semver.satisfies(confVer, semverRange);
181}
182
183function testReactVersion(context, semverRange) {
184 return test(semverRange, getReactVersionFromContext(context));
185}
186
187function testFlowVersion(context, semverRange) {
188 return test(semverRange, getFlowVersionFromContext(context));
189}
190
191module.exports = {
192 testReactVersion,
193 testFlowVersion,
194 resetWarningFlag,
195 resetDetectedVersion,
196 resetDefaultVersion,
197};
Note: See TracBrowser for help on using the repository browser.