@atlaskit/eslint-plugin-platform 0.0.5 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,221 @@
1
+ import { tester } from '../../../../__tests__/utils/_tester';
2
+ import rule from '../../index';
3
+
4
+ describe('Feature Flag can only be passed into ffTest as Literal', () => {
5
+ tester.run('ensure-test-runner-arguments', rule, {
6
+ valid: [
7
+ {
8
+ code: `ffTest('sample.feature.flag', () => {
9
+ const { getByText } = render(<SampleComponent />);
10
+ expect(getByText('SampleComponent')).toBeDefined();
11
+ });`,
12
+ },
13
+ ],
14
+ invalid: [
15
+ {
16
+ code: `ffTest(myFeatureFlag, () => {
17
+ const { getByText } = render(<SampleComponent />);
18
+ expect(getByText('SampleComponent')).toBeDefined();
19
+ });`,
20
+ errors: [
21
+ {
22
+ messageId: 'onlyInlineFeatureFlag',
23
+ data: { identifierName: 'myFeatureFlag' },
24
+ },
25
+ ],
26
+ },
27
+ ],
28
+ });
29
+ });
30
+
31
+ describe('Test functions can only be passed in directly, instead of as variables', () => {
32
+ tester.run('ensure-test-runner-arguments', rule, {
33
+ valid: [
34
+ {
35
+ code: `ffTest('sample.feature.flag', () => {
36
+ const { getByText } = render(<SampleComponent />);
37
+ expect(getByText('SampleComponent')).toBeDefined();
38
+ });`,
39
+ },
40
+ {
41
+ code: `ffTest('sample.feature.flag', () => {
42
+ const { getByText } = render(<SampleComponent />);
43
+ expect(getByText('SampleComponent')).toBeDefined();
44
+ }, () => {
45
+ const { getByText } = render(<SampleComponent />);
46
+ expect(getByText('AnotherSampleComponent')).toBeDefined();
47
+ });`,
48
+ },
49
+ ],
50
+ invalid: [
51
+ {
52
+ code: `ffTest('sample.feature.flag', fnToPassIn);`,
53
+ errors: [
54
+ {
55
+ messageId: 'onlyInlineTestFunction',
56
+ data: { identifierName: 'fnToPassIn' },
57
+ },
58
+ ],
59
+ },
60
+ {
61
+ code: `ffTest('sample.feature.flag', () => {
62
+ const { getByText } = render(<SampleComponent />);
63
+ expect(getByText('SampleComponent')).toBeDefined();
64
+ }, fnToPassInWhenFlagIsDisabled);`,
65
+ errors: [
66
+ {
67
+ messageId: 'onlyInlineTestFunction',
68
+ data: { identifierName: 'fnToPassInWhenFlagIsDisabled' },
69
+ },
70
+ ],
71
+ },
72
+ {
73
+ code: `ffTest('sample.feature.flag', fnToPassInWhenFlagIsEnabled, () => {
74
+ const { getByText } = render(<SampleComponent />);
75
+ expect(getByText('SampleComponent')).toBeDefined();
76
+ },);`,
77
+ errors: [
78
+ {
79
+ messageId: 'onlyInlineTestFunction',
80
+ data: { identifierName: 'fnToPassInWhenFlagIsEnabled' },
81
+ },
82
+ ],
83
+ },
84
+ ],
85
+ });
86
+ });
87
+
88
+ describe('Verify existing ff overrides are passed down if test runner is nested', () => {
89
+ tester.run('ensure-test-runner-arguments', rule, {
90
+ valid: [
91
+ {
92
+ code: `ffTest(
93
+ 'uip.sample.color',
94
+ ff =>
95
+ ffTest(
96
+ 'uip.sample.backgroundColor',
97
+ () => {
98
+ expect(getByText('SampleComponent')).toHaveStyle('color: red');
99
+ },
100
+ () => {
101
+ expect(getByText('SampleComponent')).toHaveStyle('color: red');
102
+ },
103
+ ff,
104
+ ),
105
+ ff =>
106
+ ffTest(
107
+ 'uip.sample.backgroundColor',
108
+ () => {
109
+ expect(getByText('SampleComponent')).toHaveStyle('color: blue');
110
+ },
111
+ () => {
112
+ expect(getByText('SampleComponent')).toHaveStyle('color: blue');
113
+ },
114
+ ff,
115
+ ),
116
+ );
117
+ `,
118
+ },
119
+ ],
120
+ invalid: [
121
+ {
122
+ code: `ffTest(
123
+ 'uip.sample.color',
124
+ () =>
125
+ ffTest(
126
+ 'uip.sample.backgroundColor',
127
+ () => {
128
+ expect(getByText('SampleComponent')).toHaveStyle('color: red');
129
+ },
130
+ () => {
131
+ expect(getByText('SampleComponent')).toHaveStyle('color: red');
132
+ },
133
+ ff,
134
+ ),
135
+ ff =>
136
+ ffTest(
137
+ 'uip.sample.backgroundColor',
138
+ () => {
139
+ expect(getByText('SampleComponent')).toHaveStyle('color: blue');
140
+ },
141
+ () => {
142
+ expect(getByText('SampleComponent')).toHaveStyle('color: blue');
143
+ },
144
+ ff,
145
+ ),
146
+ );
147
+ `,
148
+ errors: [
149
+ {
150
+ messageId: 'passDownExistingFeatureFlagParam',
151
+ },
152
+ ],
153
+ },
154
+ {
155
+ code: `ffTest(
156
+ 'uip.sample.color',
157
+ ff =>
158
+ ffTest(
159
+ 'uip.sample.backgroundColor',
160
+ () => {
161
+ expect(getByText('SampleComponent')).toHaveStyle('color: red');
162
+ },
163
+ () => {
164
+ expect(getByText('SampleComponent')).toHaveStyle('color: red');
165
+ },
166
+ ),
167
+ ff =>
168
+ ffTest(
169
+ 'uip.sample.backgroundColor',
170
+ () => {
171
+ expect(getByText('SampleComponent')).toHaveStyle('color: blue');
172
+ },
173
+ () => {
174
+ expect(getByText('SampleComponent')).toHaveStyle('color: blue');
175
+ },
176
+ ff,
177
+ ),
178
+ );
179
+ `,
180
+ errors: [
181
+ {
182
+ messageId: 'passDownExistingFeatureFlagArgument',
183
+ },
184
+ ],
185
+ },
186
+ {
187
+ code: `ffTest(
188
+ 'uip.sample.color',
189
+ ff =>
190
+ ffTest(
191
+ 'uip.sample.backgroundColor',
192
+ () => {
193
+ expect(getByText('SampleComponent')).toHaveStyle('color: red');
194
+ },
195
+ () => {
196
+ expect(getByText('SampleComponent')).toHaveStyle('color: red');
197
+ },
198
+ ff,
199
+ ),
200
+ ff =>
201
+ ffTest(
202
+ 'uip.sample.backgroundColor',
203
+ () => {
204
+ expect(getByText('SampleComponent')).toHaveStyle('color: blue');
205
+ },
206
+ () => {
207
+ expect(getByText('SampleComponent')).toHaveStyle('color: blue');
208
+ },
209
+ ffExisting,
210
+ ),
211
+ );
212
+ `,
213
+ errors: [
214
+ {
215
+ messageId: 'passDownExistingFeatureFlagNamesMatch',
216
+ },
217
+ ],
218
+ },
219
+ ],
220
+ });
221
+ });
@@ -0,0 +1,110 @@
1
+ import type { Rule } from 'eslint';
2
+
3
+ const TEST_RUNNER_IDENTIFIER = 'ffTest' as const;
4
+
5
+ const rule: Rule.RuleModule = {
6
+ meta: {
7
+ docs: {
8
+ recommended: false,
9
+ },
10
+ type: 'problem',
11
+ messages: {
12
+ onlyInlineFeatureFlag:
13
+ 'Only pass in feature flag as string literal, please replace {{identifierName}} with its value.',
14
+ onlyInlineTestFunction:
15
+ 'Only pass in test functions/cases in an inline manner. Test functions/cases should be passed in directly, instead of as variables. Please replace {{identifierName}} with its own definition.',
16
+ passDownExistingFeatureFlagParam:
17
+ 'Existing feature flags need to be passed down as params when calling nested test runner. See examples in the package which declares this function.',
18
+ passDownExistingFeatureFlagArgument:
19
+ 'Existing feature flags need to be passed in as argument when calling nested test runner. See examples in the package which declares this function.',
20
+ passDownExistingFeatureFlagNamesMatch:
21
+ 'Argument names not matching when passing down existing feature flags. See examples in the package which declares this function.',
22
+ },
23
+ },
24
+ create(context) {
25
+ return {
26
+ [`CallExpression[callee.name=/${TEST_RUNNER_IDENTIFIER}/]`]: (
27
+ node: Rule.Node,
28
+ ) => {
29
+ if (node.type === 'CallExpression') {
30
+ const args = node.arguments;
31
+
32
+ // Verify FF is passed inline
33
+ if (args[0] && args[0].type !== 'Literal') {
34
+ return context.report({
35
+ node,
36
+ messageId: 'onlyInlineFeatureFlag',
37
+ data: {
38
+ identifierName:
39
+ args[0].type === 'Identifier' ? args[0].name : '',
40
+ },
41
+ });
42
+ }
43
+
44
+ // Verify test functions/cases are passed inline
45
+ if (args[1] && args[1].type !== 'ArrowFunctionExpression') {
46
+ return context.report({
47
+ node,
48
+ messageId: 'onlyInlineTestFunction',
49
+ data: {
50
+ identifierName:
51
+ args[1].type === 'Identifier' ? args[1].name : '',
52
+ },
53
+ });
54
+ }
55
+
56
+ if (args[2] && args[2].type !== 'ArrowFunctionExpression') {
57
+ return context.report({
58
+ node,
59
+ messageId: 'onlyInlineTestFunction',
60
+ data: {
61
+ identifierName:
62
+ args[2].type === 'Identifier' ? args[2].name : '',
63
+ },
64
+ });
65
+ }
66
+
67
+ // Verify existing ff overrides are passed down if test runner is nested
68
+ let upperTestRunner = node.parent?.parent;
69
+ if (
70
+ upperTestRunner?.type === 'CallExpression' &&
71
+ upperTestRunner?.callee.type === 'Identifier' &&
72
+ upperTestRunner?.callee.name === TEST_RUNNER_IDENTIFIER &&
73
+ node.parent?.type === 'ArrowFunctionExpression'
74
+ ) {
75
+ // Not pass in ff to the function that calls test runner
76
+ if (
77
+ !node.parent.params[0] ||
78
+ node.parent.params[0].type !== 'Identifier'
79
+ ) {
80
+ return context.report({
81
+ node,
82
+ messageId: 'passDownExistingFeatureFlagParam',
83
+ });
84
+ }
85
+
86
+ // Not pass in ff to test runner as 4th argument
87
+ if (!node.arguments[3] || node.arguments[3].type !== 'Identifier') {
88
+ return context.report({
89
+ node,
90
+ messageId: 'passDownExistingFeatureFlagArgument',
91
+ });
92
+ }
93
+ // Pass in the above two, but names don't match
94
+ const paramName = node.parent.params[0].name;
95
+ const arguName = node.arguments[3].name;
96
+ if (paramName !== arguName) {
97
+ return context.report({
98
+ node,
99
+ messageId: 'passDownExistingFeatureFlagNamesMatch',
100
+ });
101
+ }
102
+ }
103
+ }
104
+ return {};
105
+ },
106
+ };
107
+ },
108
+ };
109
+
110
+ export default rule;
@@ -0,0 +1,308 @@
1
+ import { tester } from '../../../../__tests__/utils/_tester';
2
+ import rule from '../../index';
3
+
4
+ describe('Warning about too many nested test runners', () => {
5
+ tester.run('ensure-test-runner-nested-count', rule, {
6
+ valid: [
7
+ {
8
+ code: `describe('1 FF', () => {
9
+ ffTest('uip.sample.color', () => {
10
+ const { getByText } = render(<SampleComponent />);
11
+ expect(getByText('SampleComponent')).toBeDefined();
12
+ });
13
+ });`,
14
+ },
15
+ {
16
+ code: `describe('2 FFs', () => {
17
+ ffTest(
18
+ 'uip.sample.color',
19
+ ff =>
20
+ ffTest(
21
+ 'uip.sample.backgroundColor',
22
+ () => {
23
+ expect(getByText('SampleComponent')).toHaveStyle('color: red');
24
+ },
25
+ () => {
26
+ expect(getByText('SampleComponent')).toHaveStyle('color: red');
27
+ },
28
+ ff,
29
+ ),
30
+ ff =>
31
+ ffTest(
32
+ 'uip.sample.backgroundColor',
33
+ () => {
34
+ expect(getByText('SampleComponent')).toHaveStyle('color: blue');
35
+ },
36
+ () => {
37
+ expect(getByText('SampleComponent')).toHaveStyle('color: blue');
38
+ },
39
+ ff,
40
+ ),
41
+ );
42
+ });`,
43
+ },
44
+ {
45
+ code: `ffTest(
46
+ 'uip.sample.color',
47
+ (ff) =>
48
+ ffTest(
49
+ 'uip.sample.backgroundColor',
50
+ () => {},
51
+ () => {},
52
+ ff,
53
+ ),
54
+ (ff) =>
55
+ ffTest(
56
+ 'uip.sample.backgroundColor',
57
+ () => {},
58
+ () => {},
59
+ ff,
60
+ ),
61
+ );`,
62
+ },
63
+ {
64
+ code: `ffTest(
65
+ 'uip.sample.color',
66
+ (ff) =>
67
+ ffTest(
68
+ 'uip.sample.backgroundColor',
69
+ (ff) =>
70
+ ffTest(
71
+ 'uip.sample.display',
72
+ () => {},
73
+ () => {},
74
+ ff,
75
+ ),
76
+ (ff) =>
77
+ ffTest(
78
+ 'uip.sample.display',
79
+ () => {},
80
+ () => {},
81
+ ff,
82
+ ),
83
+ ff,
84
+ ),
85
+ (ff) =>
86
+ ffTest(
87
+ 'uip.sample.backgroundColor',
88
+ () => {},
89
+ (ff) =>
90
+ ffTest(
91
+ 'uip.sample.display',
92
+ () => {},
93
+ () => {},
94
+ ff,
95
+ ),
96
+ ff,
97
+ ),
98
+ );`,
99
+ },
100
+ {
101
+ code: `
102
+ ffTest(
103
+ 'uip.sample.color',
104
+ (ff) =>
105
+ ffTest(
106
+ 'uip.sample.backgroundColor',
107
+ (ff) =>
108
+ ffTest(
109
+ 'uip.sample.display',
110
+ () => {},
111
+ () => {},
112
+ ff,
113
+ ),
114
+ (ff) =>
115
+ ffTest(
116
+ 'uip.sample.display',
117
+ () => {},
118
+ () => {},
119
+ ff,
120
+ ),
121
+ ff,
122
+ ),
123
+ (ff) =>
124
+ ffTest(
125
+ 'uip.sample.backgroundColor',
126
+ (ff) =>
127
+ ffTest(
128
+ 'uip.sample.display',
129
+ (ff) =>
130
+ ffTest(
131
+ 'uip.sample.opacity',
132
+ () => {},
133
+ () => {},
134
+ ff,
135
+ ),
136
+ () => {},
137
+ ff,
138
+ ),
139
+ (ff) =>
140
+ ffTest(
141
+ 'uip.sample.display',
142
+ () => {},
143
+ (ff) =>
144
+ ffTest(
145
+ 'uip.sample.opacity',
146
+ () => {},
147
+ () => {},
148
+ ff,
149
+ ),
150
+ ff,
151
+ ),
152
+ ff,
153
+ ),
154
+ );`,
155
+ },
156
+ ],
157
+ invalid: [
158
+ {
159
+ code: `
160
+ describe('5 FFs', () => {
161
+ ffTest(
162
+ 'uip.sample.color',
163
+ (ff) =>
164
+ ffTest(
165
+ 'uip.sample.backgroundColor',
166
+ (ff) =>
167
+ ffTest(
168
+ 'uip.sample.display',
169
+ () => {},
170
+ () => {},
171
+ ff,
172
+ ),
173
+ (ff) =>
174
+ ffTest(
175
+ 'uip.sample.display',
176
+ () => {},
177
+ () => {},
178
+ ff,
179
+ ),
180
+ ff,
181
+ ),
182
+ (ff) =>
183
+ ffTest(
184
+ 'uip.sample.backgroundColor',
185
+ (ff) =>
186
+ ffTest(
187
+ 'uip.sample.display',
188
+ (ff) =>
189
+ ffTest(
190
+ 'uip.sample.opacity',
191
+ (ff) =>
192
+ ffTest(
193
+ 'uip.sample.font',
194
+ () => {},
195
+ () => {},
196
+ ff,
197
+ ),
198
+ () => {},
199
+ ff,
200
+ ),
201
+ () => {},
202
+ ff,
203
+ ),
204
+ (ff) =>
205
+ ffTest(
206
+ 'uip.sample.display',
207
+ () => {},
208
+ (ff) =>
209
+ ffTest(
210
+ 'uip.sample.opacity',
211
+ () => {},
212
+ () => {},
213
+ ff,
214
+ ),
215
+ ff,
216
+ ),
217
+ ff,
218
+ ),
219
+ );
220
+ });`,
221
+ errors: [
222
+ {
223
+ messageId: 'tooManyNestedTestRunner',
224
+ data: {
225
+ nestedTestRunner: 5,
226
+ },
227
+ },
228
+ ],
229
+ },
230
+ {
231
+ code: `
232
+ ffTest(
233
+ 'uip.sample.color',
234
+ (ff) =>
235
+ ffTest(
236
+ 'uip.sample.backgroundColor',
237
+ (ff) =>
238
+ ffTest(
239
+ 'uip.sample.display',
240
+ () => {},
241
+ () => {},
242
+ ff,
243
+ ),
244
+ (ff) =>
245
+ ffTest(
246
+ 'uip.sample.display',
247
+ () => {},
248
+ () => {},
249
+ ff,
250
+ ),
251
+ ff,
252
+ ),
253
+ (ff) =>
254
+ ffTest(
255
+ 'uip.sample.backgroundColor',
256
+ (ff) =>
257
+ ffTest(
258
+ 'uip.sample.display',
259
+ (ff) =>
260
+ ffTest(
261
+ 'uip.sample.opacity',
262
+ (ff) =>
263
+ ffTest(
264
+ 'uip.sample.font',
265
+ (ff) =>
266
+ ffTest(
267
+ 'uip.sample.border',
268
+ () => {},
269
+ () => {},
270
+ ff,
271
+ ),
272
+ () => {},
273
+ ff,
274
+ ),
275
+ () => {},
276
+ ff,
277
+ ),
278
+ () => {},
279
+ ff,
280
+ ),
281
+ (ff) =>
282
+ ffTest(
283
+ 'uip.sample.display',
284
+ () => {},
285
+ (ff) =>
286
+ ffTest(
287
+ 'uip.sample.opacity',
288
+ () => {},
289
+ () => {},
290
+ ff,
291
+ ),
292
+ ff,
293
+ ),
294
+ ff,
295
+ ),
296
+ );`,
297
+ errors: [
298
+ {
299
+ messageId: 'tooManyNestedTestRunner',
300
+ data: {
301
+ nestedTestRunner: 6,
302
+ },
303
+ },
304
+ ],
305
+ },
306
+ ],
307
+ });
308
+ });