| 1 | /**
|
|---|
| 2 | * @fileoverview Utility functions for React and Flow version configuration
|
|---|
| 3 | * @author Yannick Croissant
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | 'use strict';
|
|---|
| 7 |
|
|---|
| 8 | const fs = require('fs');
|
|---|
| 9 | const path = require('path');
|
|---|
| 10 |
|
|---|
| 11 | const resolve = require('resolve');
|
|---|
| 12 | const semver = require('semver');
|
|---|
| 13 | const error = require('./error');
|
|---|
| 14 |
|
|---|
| 15 | const ULTIMATE_LATEST_SEMVER = '999.999.999';
|
|---|
| 16 |
|
|---|
| 17 | let warnedForMissingVersion = false;
|
|---|
| 18 |
|
|---|
| 19 | function resetWarningFlag() {
|
|---|
| 20 | warnedForMissingVersion = false;
|
|---|
| 21 | }
|
|---|
| 22 |
|
|---|
| 23 | let cachedDetectedReactVersion;
|
|---|
| 24 |
|
|---|
| 25 | function resetDetectedVersion() {
|
|---|
| 26 | cachedDetectedReactVersion = undefined;
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | function 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 |
|
|---|
| 49 | function 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 |
|
|---|
| 54 | let defaultVersion = ULTIMATE_LATEST_SEMVER;
|
|---|
| 55 |
|
|---|
| 56 | function resetDefaultVersion() {
|
|---|
| 57 | defaultVersion = ULTIMATE_LATEST_SEMVER;
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | function 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; got "${typeof settingsDefaultVersion}"`);
|
|---|
| 66 | }
|
|---|
| 67 | settingsDefaultVersion = String(settingsDefaultVersion);
|
|---|
| 68 | const result = convertConfVerToSemver(settingsDefaultVersion);
|
|---|
| 69 | if (result) {
|
|---|
| 70 | defaultVersion = result.version;
|
|---|
| 71 | } else {
|
|---|
| 72 | 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.`);
|
|---|
| 73 | }
|
|---|
| 74 | } else {
|
|---|
| 75 | defaultVersion = ULTIMATE_LATEST_SEMVER;
|
|---|
| 76 | }
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | // TODO, semver-major: remove context fallback
|
|---|
| 80 | function detectReactVersion(context) {
|
|---|
| 81 | if (cachedDetectedReactVersion) {
|
|---|
| 82 | return cachedDetectedReactVersion;
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | const basedir = resolveBasedir(context);
|
|---|
| 86 |
|
|---|
| 87 | try {
|
|---|
| 88 | const reactPath = resolve.sync('react', { basedir });
|
|---|
| 89 | const react = require(reactPath); // eslint-disable-line global-require, import/no-dynamic-require
|
|---|
| 90 | cachedDetectedReactVersion = react.version;
|
|---|
| 91 | return cachedDetectedReactVersion;
|
|---|
| 92 | } catch (e) {
|
|---|
| 93 | if (e.code === 'MODULE_NOT_FOUND') {
|
|---|
| 94 | if (!warnedForMissingVersion) {
|
|---|
| 95 | let sentence2 = 'Assuming latest React version for linting.';
|
|---|
| 96 | if (defaultVersion !== ULTIMATE_LATEST_SEMVER) {
|
|---|
| 97 | sentence2 = `Assuming default React version for linting: "${defaultVersion}".`;
|
|---|
| 98 | }
|
|---|
| 99 | error(`Warning: React version was set to "detect" in eslint-plugin-react settings, but the "react" package is not installed. ${sentence2}`);
|
|---|
| 100 | warnedForMissingVersion = true;
|
|---|
| 101 | }
|
|---|
| 102 | cachedDetectedReactVersion = defaultVersion;
|
|---|
| 103 | return cachedDetectedReactVersion;
|
|---|
| 104 | }
|
|---|
| 105 | throw e;
|
|---|
| 106 | }
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | function getReactVersionFromContext(context) {
|
|---|
| 110 | readDefaultReactVersionFromContext(context);
|
|---|
| 111 | let confVer = defaultVersion;
|
|---|
| 112 | // .eslintrc shared settings (https://eslint.org/docs/user-guide/configuring#adding-shared-settings)
|
|---|
| 113 | if (context.settings && context.settings.react && context.settings.react.version) {
|
|---|
| 114 | let settingsVersion = context.settings.react.version;
|
|---|
| 115 | if (settingsVersion === 'detect') {
|
|---|
| 116 | settingsVersion = detectReactVersion(context);
|
|---|
| 117 | }
|
|---|
| 118 | if (typeof settingsVersion !== 'string') {
|
|---|
| 119 | error(`Warning: React version specified in eslint-plugin-react-settings must be a string; got “${typeof settingsVersion}”`);
|
|---|
| 120 | }
|
|---|
| 121 | confVer = String(settingsVersion);
|
|---|
| 122 | } else if (!warnedForMissingVersion) {
|
|---|
| 123 | error('Warning: React version not specified in eslint-plugin-react settings. See https://github.com/jsx-eslint/eslint-plugin-react#configuration .');
|
|---|
| 124 | warnedForMissingVersion = true;
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | const result = convertConfVerToSemver(confVer);
|
|---|
| 128 | if (!result) {
|
|---|
| 129 | error(`Warning: React version specified in eslint-plugin-react-settings must be a valid semver version, or "detect"; got “${confVer}”`);
|
|---|
| 130 | }
|
|---|
| 131 | return result ? result.version : defaultVersion;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | // TODO, semver-major: remove context fallback
|
|---|
| 135 | function detectFlowVersion(context) {
|
|---|
| 136 | const basedir = resolveBasedir(context);
|
|---|
| 137 |
|
|---|
| 138 | try {
|
|---|
| 139 | const flowPackageJsonPath = resolve.sync('flow-bin/package.json', { basedir });
|
|---|
| 140 | const flowPackageJson = require(flowPackageJsonPath); // eslint-disable-line global-require, import/no-dynamic-require
|
|---|
| 141 | return flowPackageJson.version;
|
|---|
| 142 | } catch (e) {
|
|---|
| 143 | if (e.code === 'MODULE_NOT_FOUND') {
|
|---|
| 144 | error('Warning: Flow version was set to "detect" in eslint-plugin-react settings, '
|
|---|
| 145 | + 'but the "flow-bin" package is not installed. Assuming latest Flow version for linting.');
|
|---|
| 146 | return ULTIMATE_LATEST_SEMVER;
|
|---|
| 147 | }
|
|---|
| 148 | throw e;
|
|---|
| 149 | }
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | function getFlowVersionFromContext(context) {
|
|---|
| 153 | let confVer = defaultVersion;
|
|---|
| 154 | // .eslintrc shared settings (https://eslint.org/docs/user-guide/configuring#adding-shared-settings)
|
|---|
| 155 | if (context.settings.react && context.settings.react.flowVersion) {
|
|---|
| 156 | let flowVersion = context.settings.react.flowVersion;
|
|---|
| 157 | if (flowVersion === 'detect') {
|
|---|
| 158 | flowVersion = detectFlowVersion(context);
|
|---|
| 159 | }
|
|---|
| 160 | if (typeof flowVersion !== 'string') {
|
|---|
| 161 | error('Warning: Flow version specified in eslint-plugin-react-settings must be a string; '
|
|---|
| 162 | + `got “${typeof flowVersion}”`);
|
|---|
| 163 | }
|
|---|
| 164 | confVer = String(flowVersion);
|
|---|
| 165 | } else {
|
|---|
| 166 | throw 'Could not retrieve flowVersion from settings'; // eslint-disable-line no-throw-literal
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | const result = convertConfVerToSemver(confVer);
|
|---|
| 170 | if (!result) {
|
|---|
| 171 | error(`Warning: Flow version specified in eslint-plugin-react-settings must be a valid semver version, or "detect"; got “${confVer}”`);
|
|---|
| 172 | }
|
|---|
| 173 | return result ? result.version : defaultVersion;
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | function test(semverRange, confVer) {
|
|---|
| 177 | return semver.satisfies(confVer, semverRange);
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | function testReactVersion(context, semverRange) {
|
|---|
| 181 | return test(semverRange, getReactVersionFromContext(context));
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | function testFlowVersion(context, semverRange) {
|
|---|
| 185 | return test(semverRange, getFlowVersionFromContext(context));
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | module.exports = {
|
|---|
| 189 | testReactVersion,
|
|---|
| 190 | testFlowVersion,
|
|---|
| 191 | resetWarningFlag,
|
|---|
| 192 | resetDetectedVersion,
|
|---|
| 193 | resetDefaultVersion,
|
|---|
| 194 | };
|
|---|