@atlaskit/eslint-plugin-platform 0.0.5 → 0.0.7

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.
package/report.api.md CHANGED
@@ -23,6 +23,8 @@ export const configs: {
23
23
  plugins: string[];
24
24
  rules: {
25
25
  '@atlaskit/platform/ensure-feature-flag-registration': string;
26
+ '@atlaskit/platform/ensure-test-runner-arguments': string;
27
+ '@atlaskit/platform/ensure-test-runner-nested-count': string;
26
28
  '@atlaskit/platform/no-invalid-feature-flag-usage': string;
27
29
  };
28
30
  };
@@ -31,6 +33,8 @@ export const configs: {
31
33
  // @public (undocumented)
32
34
  export const rules: {
33
35
  'ensure-feature-flag-registration': Rule.RuleModule;
36
+ 'ensure-test-runner-arguments': Rule.RuleModule;
37
+ 'ensure-test-runner-nested-count': Rule.RuleModule;
34
38
  'no-invalid-feature-flag-usage': Rule.RuleModule;
35
39
  };
36
40
 
package/src/index.tsx CHANGED
@@ -1,8 +1,12 @@
1
1
  import ensureFeatureFlagRegistration from './rules/ensure-feature-flag-registration';
2
+ import ensureTestRunnerArguments from './rules/ensure-test-runner-arguments';
3
+ import ensureTestRunnerNestedCount from './rules/ensure-test-runner-nested-count';
2
4
  import noInvalidFeatureFlagUsage from './rules/no-invalid-feature-flag-usage';
3
5
 
4
6
  export const rules = {
5
7
  'ensure-feature-flag-registration': ensureFeatureFlagRegistration,
8
+ 'ensure-test-runner-arguments': ensureTestRunnerArguments,
9
+ 'ensure-test-runner-nested-count': ensureTestRunnerNestedCount,
6
10
  'no-invalid-feature-flag-usage': noInvalidFeatureFlagUsage,
7
11
  };
8
12
 
@@ -11,6 +15,8 @@ export const configs = {
11
15
  plugins: ['@atlaskit/platform'],
12
16
  rules: {
13
17
  '@atlaskit/platform/ensure-feature-flag-registration': 'error',
18
+ '@atlaskit/platform/ensure-test-runner-arguments': 'error',
19
+ '@atlaskit/platform/ensure-test-runner-nested-count': 'warn',
14
20
  '@atlaskit/platform/no-invalid-feature-flag-usage': 'error',
15
21
  },
16
22
  },
@@ -0,0 +1,298 @@
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
+ output: `ffTest(
149
+ 'uip.sample.color',
150
+ ff =>
151
+ ffTest(
152
+ 'uip.sample.backgroundColor',
153
+ () => {
154
+ expect(getByText('SampleComponent')).toHaveStyle('color: red');
155
+ },
156
+ () => {
157
+ expect(getByText('SampleComponent')).toHaveStyle('color: red');
158
+ },
159
+ ff,
160
+ ),
161
+ ff =>
162
+ ffTest(
163
+ 'uip.sample.backgroundColor',
164
+ () => {
165
+ expect(getByText('SampleComponent')).toHaveStyle('color: blue');
166
+ },
167
+ () => {
168
+ expect(getByText('SampleComponent')).toHaveStyle('color: blue');
169
+ },
170
+ ff,
171
+ ),
172
+ );
173
+ `,
174
+ errors: [
175
+ {
176
+ messageId: 'passDownExistingFeatureFlagParam',
177
+ },
178
+ ],
179
+ },
180
+ {
181
+ code: `ffTest(
182
+ 'uip.sample.color',
183
+ ff =>
184
+ ffTest(
185
+ 'uip.sample.backgroundColor',
186
+ () => {
187
+ expect(getByText('SampleComponent')).toHaveStyle('color: red');
188
+ },
189
+ () => {
190
+ expect(getByText('SampleComponent')).toHaveStyle('color: red');
191
+ },
192
+ ),
193
+ ff =>
194
+ ffTest(
195
+ 'uip.sample.backgroundColor',
196
+ () => {
197
+ expect(getByText('SampleComponent')).toHaveStyle('color: blue');
198
+ },
199
+ () => {
200
+ expect(getByText('SampleComponent')).toHaveStyle('color: blue');
201
+ },
202
+ ff,
203
+ ),
204
+ );
205
+ `,
206
+ output: `ffTest(
207
+ 'uip.sample.color',
208
+ ff =>
209
+ ffTest(
210
+ 'uip.sample.backgroundColor',
211
+ () => {
212
+ expect(getByText('SampleComponent')).toHaveStyle('color: red');
213
+ },
214
+ () => {
215
+ expect(getByText('SampleComponent')).toHaveStyle('color: red');
216
+ }, ff,
217
+ ),
218
+ ff =>
219
+ ffTest(
220
+ 'uip.sample.backgroundColor',
221
+ () => {
222
+ expect(getByText('SampleComponent')).toHaveStyle('color: blue');
223
+ },
224
+ () => {
225
+ expect(getByText('SampleComponent')).toHaveStyle('color: blue');
226
+ },
227
+ ff,
228
+ ),
229
+ );
230
+ `,
231
+ errors: [
232
+ {
233
+ messageId: 'passDownExistingFeatureFlagArgument',
234
+ },
235
+ ],
236
+ },
237
+ {
238
+ code: `ffTest(
239
+ 'uip.sample.color',
240
+ ff =>
241
+ ffTest(
242
+ 'uip.sample.backgroundColor',
243
+ () => {
244
+ expect(getByText('SampleComponent')).toHaveStyle('color: red');
245
+ },
246
+ () => {
247
+ expect(getByText('SampleComponent')).toHaveStyle('color: red');
248
+ },
249
+ ff,
250
+ ),
251
+ ff =>
252
+ ffTest(
253
+ 'uip.sample.backgroundColor',
254
+ () => {
255
+ expect(getByText('SampleComponent')).toHaveStyle('color: blue');
256
+ },
257
+ () => {
258
+ expect(getByText('SampleComponent')).toHaveStyle('color: blue');
259
+ },
260
+ ffExisting,
261
+ ),
262
+ );
263
+ `,
264
+ output: `ffTest(
265
+ 'uip.sample.color',
266
+ ff =>
267
+ ffTest(
268
+ 'uip.sample.backgroundColor',
269
+ () => {
270
+ expect(getByText('SampleComponent')).toHaveStyle('color: red');
271
+ },
272
+ () => {
273
+ expect(getByText('SampleComponent')).toHaveStyle('color: red');
274
+ },
275
+ ff,
276
+ ),
277
+ ff =>
278
+ ffTest(
279
+ 'uip.sample.backgroundColor',
280
+ () => {
281
+ expect(getByText('SampleComponent')).toHaveStyle('color: blue');
282
+ },
283
+ () => {
284
+ expect(getByText('SampleComponent')).toHaveStyle('color: blue');
285
+ },
286
+ ff,
287
+ ),
288
+ );
289
+ `,
290
+ errors: [
291
+ {
292
+ messageId: 'passDownExistingFeatureFlagNamesMatch',
293
+ },
294
+ ],
295
+ },
296
+ ],
297
+ });
298
+ });
@@ -0,0 +1,134 @@
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
+ fixable: 'code',
12
+ messages: {
13
+ onlyInlineFeatureFlag:
14
+ 'Only pass in feature flag as string literal, please replace {{identifierName}} with its value.',
15
+ onlyInlineTestFunction:
16
+ '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.',
17
+ passDownExistingFeatureFlagParam:
18
+ 'An argument symbolising existing FFs needs to be passed down as param when calling nested test runner. See examples in the package which declares this function.',
19
+ passDownExistingFeatureFlagArgument:
20
+ 'An argument symbolising existing FFs needs to be passed in as argument when calling nested test runner. See examples in the package which declares this function.',
21
+ passDownExistingFeatureFlagNamesMatch:
22
+ 'Argument names not matching when passing down existing feature flags. See examples in the package which declares this function.',
23
+ },
24
+ },
25
+ create(context) {
26
+ return {
27
+ [`CallExpression[callee.name=/${TEST_RUNNER_IDENTIFIER}/]`]: (
28
+ node: Rule.Node,
29
+ ) => {
30
+ if (node.type === 'CallExpression') {
31
+ const args = node.arguments;
32
+
33
+ // Verify FF is passed inline
34
+ if (args[0] && args[0].type !== 'Literal') {
35
+ return context.report({
36
+ node,
37
+ messageId: 'onlyInlineFeatureFlag',
38
+ data: {
39
+ identifierName:
40
+ args[0].type === 'Identifier' ? args[0].name : '',
41
+ },
42
+ });
43
+ }
44
+
45
+ // Verify test functions/cases are passed inline
46
+ if (args[1] && args[1].type !== 'ArrowFunctionExpression') {
47
+ return context.report({
48
+ node,
49
+ messageId: 'onlyInlineTestFunction',
50
+ data: {
51
+ identifierName:
52
+ args[1].type === 'Identifier' ? args[1].name : '',
53
+ },
54
+ });
55
+ }
56
+
57
+ if (args[2] && args[2].type !== 'ArrowFunctionExpression') {
58
+ return context.report({
59
+ node,
60
+ messageId: 'onlyInlineTestFunction',
61
+ data: {
62
+ identifierName:
63
+ args[2].type === 'Identifier' ? args[2].name : '',
64
+ },
65
+ });
66
+ }
67
+
68
+ // Verify existing ff overrides are passed down if test runner is nested
69
+ let upperTestRunner = node.parent?.parent;
70
+ if (
71
+ upperTestRunner?.type === 'CallExpression' &&
72
+ upperTestRunner?.callee.type === 'Identifier' &&
73
+ upperTestRunner?.callee.name === TEST_RUNNER_IDENTIFIER &&
74
+ node.parent?.type === 'ArrowFunctionExpression'
75
+ ) {
76
+ // Not pass in ff to the function that calls test runner
77
+ if (
78
+ !node.parent.params[0] ||
79
+ node.parent.params[0].type !== 'Identifier'
80
+ ) {
81
+ return context.report({
82
+ node: node.parent,
83
+ messageId: 'passDownExistingFeatureFlagParam',
84
+ fix: function (fixer) {
85
+ const parentNodeRange = node.parent.range as [number, number];
86
+ return [
87
+ fixer.replaceTextRange(
88
+ [parentNodeRange[0], parentNodeRange[0] + 2],
89
+ 'ff',
90
+ ),
91
+ node.arguments[3]
92
+ ? fixer.replaceText(node.arguments[3], 'ff')
93
+ : fixer.insertTextAfter(node.arguments[2], ', ff'),
94
+ ];
95
+ },
96
+ });
97
+ }
98
+
99
+ // Not pass in ff to test runner as 4th argument
100
+ const paramName = node.parent.params[0].name;
101
+ if (!node.arguments[3] || node.arguments[3].type !== 'Identifier') {
102
+ return context.report({
103
+ node,
104
+ messageId: 'passDownExistingFeatureFlagArgument',
105
+ fix: function (fixer) {
106
+ return node.arguments[3]
107
+ ? fixer.replaceText(node.arguments[3], paramName)
108
+ : fixer.insertTextAfter(
109
+ node.arguments[2],
110
+ `, ${paramName}`,
111
+ );
112
+ },
113
+ });
114
+ }
115
+ // Pass in the above two, but names don't match
116
+ const arguName = node.arguments[3].name;
117
+ if (paramName !== arguName) {
118
+ return context.report({
119
+ node: node.parent,
120
+ messageId: 'passDownExistingFeatureFlagNamesMatch',
121
+ fix: function (fixer) {
122
+ return fixer.replaceText(node.arguments[3], paramName);
123
+ },
124
+ });
125
+ }
126
+ }
127
+ }
128
+ return {};
129
+ },
130
+ };
131
+ },
132
+ };
133
+
134
+ export default rule;