forked from jsx-eslint/eslint-plugin-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversion.js
190 lines (148 loc) · 8.66 KB
/
version.js
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
'use strict';
const path = require('path');
const assert = require('assert');
const sinon = require('sinon');
const versionUtil = require('../../lib/util/version');
describe('Version', () => {
const base = path.resolve(__dirname, '..', 'fixtures', 'version');
let expectedErrorArgs = [];
beforeEach(() => {
sinon.stub(console, 'error');
expectedErrorArgs = [];
versionUtil.resetWarningFlag();
versionUtil.resetDetectedVersion();
versionUtil.resetDefaultVersion();
});
afterEach(() => {
const actualArgs = console.error.args; // eslint-disable-line no-console
console.error.restore(); // eslint-disable-line no-console
assert.deepEqual(actualArgs, expectedErrorArgs);
});
describe('Detect version', () => {
const context = { settings: { react: { version: 'detect', flowVersion: 'detect' } }, getFilename: () => path.resolve(base, 'test.js') };
afterEach(() => {
if (context.getFilename.restore) {
context.getFilename.restore();
}
});
it('matches detected version', () => {
sinon.stub(context, 'getFilename').callsFake(() => path.resolve(base, 'detect-version', 'test.js'));
assert.equal(versionUtil.testReactVersion(context, '>= 1.2.3'), true);
assert.equal(versionUtil.testReactVersion(context, '>= 1.2.4'), false);
assert.equal(versionUtil.testFlowVersion(context, '>= 0.92.0'), true);
});
it('matches detected version in sibling project', () => {
sinon.stub(context, 'getFilename').callsFake(() => path.resolve(base, 'detect-version-sibling', 'test.js'));
assert.equal(versionUtil.testReactVersion(context, '>= 2.3.4'), true);
assert.equal(versionUtil.testReactVersion(context, '>= 2.3.5'), false);
assert.equal(versionUtil.testFlowVersion(context, '>= 2.92.0'), true);
});
it('matches detected version in child project', () => {
sinon.stub(context, 'getFilename').callsFake(() => path.resolve(base, 'detect-version', 'detect-version-child', 'test.js'));
assert.equal(versionUtil.testReactVersion(context, '>= 3.4.5'), true);
assert.equal(versionUtil.testReactVersion(context, '>= 3.4.6'), false);
assert.equal(versionUtil.testFlowVersion(context, '>= 3.92.0'), true);
});
it('assumes latest version if react is not installed', () => {
sinon.stub(context, 'getFilename').callsFake(() => path.resolve(base, 'detect-version-missing', 'test.js'));
assert.equal(versionUtil.testReactVersion(context, '999.999.999'), true);
expectedErrorArgs = [
['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.'],
];
});
it('uses default version from settings if provided and react is not installed', () => {
context.settings.react.defaultVersion = '16.14.0';
sinon.stub(context, 'getFilename').callsFake(() => path.resolve(base, 'detect-version-missing', 'test.js'));
assert.equal(versionUtil.testReactVersion(context, '16.14.0'), true);
expectedErrorArgs = [
['Warning: React version was set to "detect" in eslint-plugin-react settings, but the "react" package is not installed. Assuming default React version for linting: "16.14.0".'],
];
delete context.settings.react.defaultVersion;
});
it('fails nicely with an invalid default version of react', () => {
context.settings.react.defaultVersion = 'not semver';
sinon.stub(context, 'getFilename').callsFake(() => path.resolve(base, 'detect-version-missing', 'test.js'));
assert.equal(versionUtil.testReactVersion(context, '999.999.999'), true);
expectedErrorArgs = [
['Warning: React version specified in eslint-plugin-react-settings must be a valid semver version, or "detect"; got “not semver”. Falling back to latest version as default.'],
['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.'],
];
delete context.settings.react.defaultVersion;
});
it('warns only once for failure to detect react ', () => {
sinon.stub(context, 'getFilename').callsFake(() => path.resolve(base, 'detect-version-missing', 'test.js'));
assert.equal(versionUtil.testReactVersion(context, '999.999.999'), true);
assert.equal(versionUtil.testReactVersion(context, '999.999.999'), true);
expectedErrorArgs = [
['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.'],
];
});
it('assumes latest version if flow-bin is not installed', () => {
assert.equal(versionUtil.testFlowVersion(context, '999.999.999'), true);
expectedErrorArgs = [
['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.'],
];
});
it('works with virtual filename', () => {
sinon.stub(context, 'getFilename').callsFake(() => path.resolve(base, 'detect-version-sibling', 'test.js/0_fake.js'));
assert.equal(versionUtil.testReactVersion(context, '>= 2.3.4'), true);
assert.equal(versionUtil.testReactVersion(context, '>= 2.3.5'), false);
assert.equal(versionUtil.testFlowVersion(context, '>= 2.92.0'), true);
});
it('works with recursive virtual filename', () => {
sinon.stub(context, 'getFilename').callsFake(() => path.resolve(base, 'detect-version-sibling', 'test.js/0_fake.md/1_fake.js'));
assert.equal(versionUtil.testReactVersion(context, '>= 2.3.4'), true);
assert.equal(versionUtil.testReactVersion(context, '>= 2.3.5'), false);
assert.equal(versionUtil.testFlowVersion(context, '>= 2.92.0'), true);
});
});
describe('string version', () => {
const context = { settings: { react: { version: '15.0', flowVersion: '1.2' } } };
const invalidContext = { settings: { react: { version: 'latest', flowVersion: 'not semver' } } };
it('works with react', () => {
assert.equal(versionUtil.testReactVersion(context, '>= 0.14.0'), true);
assert.equal(versionUtil.testReactVersion(context, '>= 15.0.0'), true);
assert.equal(versionUtil.testReactVersion(context, '>= 16.0.0'), false);
});
it('works with flow', () => {
assert.equal(versionUtil.testFlowVersion(context, '>= 1.1.0'), true);
assert.equal(versionUtil.testFlowVersion(context, '>= 1.2.0'), true);
assert.equal(versionUtil.testFlowVersion(context, '>= 1.3.0'), false);
});
it('fails nicely with an invalid react version', () => {
assert.equal(versionUtil.testReactVersion(invalidContext, '>= 15.0'), true);
expectedErrorArgs = [
['Warning: React version specified in eslint-plugin-react-settings must be a valid semver version, or "detect"; got “latest”'],
];
});
it('fails nicely with an invalid flow version', () => {
assert.equal(versionUtil.testFlowVersion(invalidContext, '>= 1.0'), true);
expectedErrorArgs = [
['Warning: Flow version specified in eslint-plugin-react-settings must be a valid semver version, or "detect"; got “not semver”'],
];
});
});
describe('non-string version', () => {
const context = { settings: { react: { version: 15.0, flowVersion: 1.2 } } };
it('works with react', () => {
assert.equal(versionUtil.testReactVersion(context, '>= 0.14.0'), true, '>= 0.14.0');
assert.equal(versionUtil.testReactVersion(context, '>= 15.0.0'), true, '>= 15.0.0');
assert.equal(versionUtil.testReactVersion(context, '>= 16.0.0'), false, '>= 16.0.0');
expectedErrorArgs = [
['Warning: React version specified in eslint-plugin-react-settings must be a string; got “number”'],
['Warning: React version specified in eslint-plugin-react-settings must be a string; got “number”'],
['Warning: React version specified in eslint-plugin-react-settings must be a string; got “number”'],
];
});
it('works with flow', () => {
assert.equal(versionUtil.testFlowVersion(context, '>= 1.1.0'), true);
assert.equal(versionUtil.testFlowVersion(context, '>= 1.2.0'), true);
assert.equal(versionUtil.testFlowVersion(context, '>= 1.3.0'), false);
expectedErrorArgs = [
['Warning: Flow version specified in eslint-plugin-react-settings must be a string; got “number”'],
['Warning: Flow version specified in eslint-plugin-react-settings must be a string; got “number”'],
['Warning: Flow version specified in eslint-plugin-react-settings must be a string; got “number”'],
];
});
});
});