@checkdigit/eslint-plugin 6.6.0-PR.75-9891 → 6.6.0-PR.75-0dbb

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.
@@ -6,12 +6,13 @@
6
6
  * This code is licensed under the MIT license (see LICENSE.txt for details).
7
7
  */
8
8
 
9
- import type { Identifier, MemberExpression, VariableDeclarator } from 'estree';
9
+ import type { Identifier, VariableDeclarator } from 'estree';
10
10
  import type { Rule } from 'eslint';
11
11
  import { analyzeResponseReferences } from './response-reference';
12
12
  import { strict as assert } from 'node:assert';
13
13
  import getDocumentationUrl from '../get-documentation-url';
14
14
  import { getParent } from '../ast/tree';
15
+ import { isInvalidResponseHeadersAccess } from './fetch';
15
16
 
16
17
  export const ruleId = 'fetch-header-getter';
17
18
 
@@ -42,12 +43,13 @@ const rule: Rule.RuleModule = {
42
43
  );
43
44
  assert.ok(responseVariable);
44
45
 
45
- const directHeaderReferences = responseHeadersReferences
46
- .map(getParent)
47
- .filter((parent): parent is MemberExpression => parent?.type === 'MemberExpression');
46
+ const directHeadersReferences = responseHeadersReferences.filter((headersReference) => {
47
+ const headersAccess = getParent(headersReference);
48
+ return headersAccess?.type !== 'VariableDeclarator';
49
+ });
48
50
 
49
- const indirectHeaderReferences = responseHeadersReferences
50
- .map((reference) => getParent(reference))
51
+ const indirectHeadersReferences = responseHeadersReferences
52
+ .map(getParent)
51
53
  .filter((parent): parent is VariableDeclarator => parent?.type === 'VariableDeclarator')
52
54
  .map((declarator) => (declarator.id as Identifier).name)
53
55
  .map((redefinedHeadersVariableName) => {
@@ -55,32 +57,31 @@ const rule: Rule.RuleModule = {
55
57
  const identifier = variable.identifiers[0];
56
58
  return identifier?.type === 'Identifier' && identifier.name === redefinedHeadersVariableName;
57
59
  });
58
- return (
59
- headersVariable?.references
60
- .map((reference) => getParent(reference.identifier))
61
- .filter((parent): parent is MemberExpression => parent?.type === 'MemberExpression') ?? []
62
- );
60
+ return headersVariable?.references.map((reference) => reference.identifier) ?? [];
63
61
  })
64
62
  .flat();
65
63
 
66
- const invalidHeaderReferences = [...directHeaderReferences, ...indirectHeaderReferences].filter(
67
- (reference) => !(reference.property.type === 'Identifier' && reference.property.name === 'get'),
64
+ const invalidHeadersReferences = [...directHeadersReferences, ...indirectHeadersReferences].filter(
65
+ isInvalidResponseHeadersAccess,
68
66
  );
69
67
 
70
- invalidHeaderReferences.forEach((reference) => {
71
- const headerNameNode = reference.property;
72
- const headerName = reference.computed
73
- ? sourceCode.getText(headerNameNode)
74
- : `'${sourceCode.getText(headerNameNode)}'`;
75
- const replacementText = `${sourceCode.getText(reference.object)}.get(${headerName})`;
68
+ invalidHeadersReferences.forEach((headersReference) => {
69
+ const headerAccess = getParent(headersReference);
70
+ if (headerAccess?.type === 'MemberExpression') {
71
+ const headerNameNode = headerAccess.property;
72
+ const headerName = headerAccess.computed
73
+ ? sourceCode.getText(headerNameNode)
74
+ : `'${sourceCode.getText(headerNameNode)}'`;
75
+ const replacementText = `${sourceCode.getText(headerAccess.object)}.get(${headerName})`;
76
76
 
77
- context.report({
78
- node: reference,
79
- messageId: 'shouldUseHeaderGetter',
80
- fix(fixer) {
81
- return fixer.replaceText(reference, replacementText);
82
- },
83
- });
77
+ context.report({
78
+ node: headerAccess,
79
+ messageId: 'shouldUseHeaderGetter',
80
+ fix(fixer) {
81
+ return fixer.replaceText(headerAccess, replacementText);
82
+ },
83
+ });
84
+ }
84
85
  });
85
86
  },
86
87
  };
@@ -0,0 +1,355 @@
1
+ // fixture/fetch-then.ts
2
+
3
+ /*
4
+ * Copyright (c) 2021-2024 Check Digit, LLC
5
+ *
6
+ * This code is licensed under the MIT license (see LICENSE.txt for details).
7
+ */
8
+
9
+ import type { CallExpression, Expression, MemberExpression, SimpleCallExpression } from 'estree';
10
+ import { type Rule, type Scope, SourceCode } from 'eslint';
11
+ import { getEnclosingFunction, getEnclosingStatement, getParent, isUsedInArrayOrAsArgument } from '../ast/tree';
12
+ import { hasAssertions, isInvalidResponseHeadersAccess } from './fetch';
13
+ import { strict as assert } from 'node:assert';
14
+ import getDocumentationUrl from '../get-documentation-url';
15
+ import { getIndentation } from '../ast/format';
16
+ import { isValidPropertyName } from './variable';
17
+ import { replaceEndpointUrlPrefixWithBasePath } from './url';
18
+
19
+ export const ruleId = 'fetch-then';
20
+
21
+ interface FixtureCallInformation {
22
+ fixtureNode: SimpleCallExpression;
23
+ requestBody?: Expression;
24
+ requestHeaders?: { name: Expression; value: Expression }[];
25
+ assertions?: Expression[][];
26
+ }
27
+
28
+ // recursively analyze the fixture/supertest call chain to collect information of request/response
29
+ function analyzeFixtureCall(call: SimpleCallExpression, results: FixtureCallInformation, sourceCode: SourceCode) {
30
+ const parent = getParent(call);
31
+ if (!parent) {
32
+ return;
33
+ }
34
+
35
+ let nextCall;
36
+ if (parent.type !== 'MemberExpression') {
37
+ results.fixtureNode = call;
38
+ return;
39
+ }
40
+
41
+ if (parent.property.type === 'Identifier') {
42
+ if (parent.property.name === 'expect') {
43
+ // supertest assertions
44
+ const assertionCall = getParent(parent);
45
+ assert.ok(assertionCall && assertionCall.type === 'CallExpression');
46
+ results.assertions = [...(results.assertions ?? []), assertionCall.arguments as Expression[]];
47
+ nextCall = assertionCall;
48
+ } else if (parent.property.name === 'send') {
49
+ // request body
50
+ const sendRequestBodyCall = getParent(parent);
51
+ assert.ok(sendRequestBodyCall && sendRequestBodyCall.type === 'CallExpression');
52
+ results.requestBody = sendRequestBodyCall.arguments[0] as Expression;
53
+ nextCall = sendRequestBodyCall;
54
+ } else if (parent.property.name === 'set') {
55
+ // request headers
56
+ const setRequestHeaderCall = getParent(parent);
57
+ assert.ok(setRequestHeaderCall && setRequestHeaderCall.type === 'CallExpression');
58
+ const [name, value] = setRequestHeaderCall.arguments as [Expression, Expression];
59
+ results.requestHeaders = [...(results.requestHeaders ?? []), { name, value }];
60
+ nextCall = setRequestHeaderCall;
61
+ }
62
+ } else {
63
+ throw new Error(`Unexpected expression in fixture/supertest call ${sourceCode.getText(parent)}.`);
64
+ }
65
+ if (nextCall) {
66
+ analyzeFixtureCall(nextCall, results, sourceCode);
67
+ }
68
+ }
69
+
70
+ // eslint-disable-next-line sonarjs/cognitive-complexity
71
+ function createResponseAssertions(
72
+ fixtureCallInformation: FixtureCallInformation,
73
+ sourceCode: SourceCode,
74
+ responseVariableName: string,
75
+ ) {
76
+ let statusAssertion: string | undefined;
77
+ const nonStatusAssertions: string[] = [];
78
+ for (const expectArguments of fixtureCallInformation.assertions ?? []) {
79
+ if (expectArguments.length === 1) {
80
+ const [assertionArgument] = expectArguments;
81
+ assert.ok(assertionArgument);
82
+ if (
83
+ (assertionArgument.type === 'MemberExpression' &&
84
+ assertionArgument.object.type === 'Identifier' &&
85
+ assertionArgument.object.name === 'StatusCodes') ||
86
+ assertionArgument.type === 'Literal' ||
87
+ sourceCode.getText(assertionArgument).includes('StatusCodes.')
88
+ ) {
89
+ // status code assertion
90
+ statusAssertion = `assert.equal(${responseVariableName}.status, ${sourceCode.getText(assertionArgument)})`;
91
+ } else if (assertionArgument.type === 'ArrowFunctionExpression') {
92
+ // callback assertion using arrow function
93
+ let functionBody = sourceCode.getText(assertionArgument.body);
94
+
95
+ const [originalResponseArgument] = assertionArgument.params;
96
+ assert.ok(originalResponseArgument?.type === 'Identifier');
97
+ const originalResponseArgumentName = originalResponseArgument.name;
98
+ if (originalResponseArgumentName !== responseVariableName) {
99
+ functionBody = functionBody.replace(
100
+ new RegExp(`\\b${originalResponseArgumentName}\\b`, 'ug'),
101
+ responseVariableName,
102
+ );
103
+ }
104
+ nonStatusAssertions.push(`assert.ok(${functionBody})`);
105
+ } else if (assertionArgument.type === 'Identifier') {
106
+ // callback assertion using function reference
107
+ nonStatusAssertions.push(`assert.ok(${sourceCode.getText(assertionArgument)}(${responseVariableName}))`);
108
+ } else if (assertionArgument.type === 'ObjectExpression' || assertionArgument.type === 'CallExpression') {
109
+ // body deep equal assertion
110
+ nonStatusAssertions.push(
111
+ `assert.deepEqual(await ${responseVariableName}.json(), ${sourceCode.getText(assertionArgument)})`,
112
+ );
113
+ } else {
114
+ throw new Error(`Unexpected Supertest assertion argument: ".expect(${sourceCode.getText(assertionArgument)})`);
115
+ }
116
+ } else if (expectArguments.length === 2) {
117
+ // header assertion
118
+ const [headerName, headerValue] = expectArguments;
119
+ assert.ok(headerName && headerValue);
120
+ const headersReference = `${responseVariableName}.headers`;
121
+ if (headerValue.type === 'Literal' && headerValue.value instanceof RegExp) {
122
+ nonStatusAssertions.push(
123
+ `assert.ok(${headersReference}.get(${sourceCode.getText(headerName)}).match(${sourceCode.getText(headerValue)}))`,
124
+ );
125
+ } else {
126
+ nonStatusAssertions.push(
127
+ `assert.equal(${headersReference}.get(${sourceCode.getText(headerName)}), ${sourceCode.getText(headerValue)})`,
128
+ );
129
+ }
130
+ }
131
+ }
132
+ return {
133
+ statusAssertion,
134
+ nonStatusAssertions,
135
+ };
136
+ }
137
+
138
+ function getResponseHeadersAccesses(
139
+ responseVariables: Scope.Variable[],
140
+ scopeManager: Scope.ScopeManager,
141
+ sourceCode: SourceCode,
142
+ ) {
143
+ const responseHeadersAccesses: MemberExpression[] = [];
144
+ for (const responseVariable of responseVariables) {
145
+ for (const responseReference of responseVariable.references) {
146
+ const responseAccess = getParent(responseReference.identifier);
147
+ if (!responseAccess || responseAccess.type !== 'MemberExpression') {
148
+ continue;
149
+ }
150
+
151
+ const responseAccessParent = getParent(responseAccess);
152
+ if (!responseAccessParent) {
153
+ continue;
154
+ }
155
+
156
+ if (
157
+ responseAccessParent.type === 'CallExpression' &&
158
+ responseAccessParent.arguments[0]?.type === 'ArrowFunctionExpression'
159
+ ) {
160
+ // map-like operation against responses, e.g. responses.map((response) => response.headers.etag)
161
+ responseHeadersAccesses.push(
162
+ ...getResponseHeadersAccesses(
163
+ scopeManager.getDeclaredVariables(responseAccessParent.arguments[0]),
164
+ scopeManager,
165
+ sourceCode,
166
+ ),
167
+ );
168
+ continue;
169
+ }
170
+
171
+ if (
172
+ responseAccess.computed &&
173
+ responseAccess.property.type === 'Literal' &&
174
+ responseAccessParent.type === 'MemberExpression'
175
+ ) {
176
+ // header access through indexed responses array, e.g. responses[0].headers, responses[1].get(...), etc.
177
+ responseHeadersAccesses.push(responseAccessParent);
178
+ } else {
179
+ responseHeadersAccesses.push(responseAccess);
180
+ }
181
+ }
182
+ }
183
+ return responseHeadersAccesses;
184
+ }
185
+
186
+ const rule: Rule.RuleModule = {
187
+ meta: {
188
+ type: 'suggestion',
189
+ docs: {
190
+ description: 'Prefer native fetch API over customized fixture API.',
191
+ url: getDocumentationUrl(ruleId),
192
+ },
193
+ messages: {
194
+ preferNativeFetch: 'Prefer native fetch API over customized fixture API.',
195
+ shouldUseHeaderGetter: 'Getter should be used to access response headers.',
196
+ unknownError:
197
+ 'Unknown error occurred in file "{{fileName}}": {{ error }}. Please manually convert the fixture API call to fetch API call.',
198
+ },
199
+ fixable: 'code',
200
+ schema: [],
201
+ },
202
+ // eslint-disable-next-line max-lines-per-function
203
+ create(context) {
204
+ const sourceCode = context.sourceCode;
205
+ const scopeManager = sourceCode.scopeManager;
206
+
207
+ return {
208
+ // eslint-disable-next-line max-lines-per-function
209
+ 'CallExpression[callee.object.object.name="fixture"][callee.object.property.name="api"]': (
210
+ fixtureCall: CallExpression,
211
+ // eslint-disable-next-line sonarjs/cognitive-complexity
212
+ ) => {
213
+ try {
214
+ if (!hasAssertions(fixtureCall)) {
215
+ // skip if there are no assertions, let "no-fixture" rule to handle the conversion
216
+ return;
217
+ }
218
+
219
+ if (!(isUsedInArrayOrAsArgument(fixtureCall) || getEnclosingFunction(fixtureCall)?.async === false)) {
220
+ return;
221
+ }
222
+
223
+ assert.ok(fixtureCall.type === 'CallExpression');
224
+ const fixtureFunction = fixtureCall.callee; // e.g. fixture.api.get
225
+ assert.ok(fixtureFunction.type === 'MemberExpression');
226
+ const indentation = getIndentation(fixtureCall, sourceCode);
227
+
228
+ const [urlArgumentNode] = fixtureCall.arguments; // e.g. `/sample-service/v1/ping`
229
+ assert.ok(urlArgumentNode !== undefined);
230
+
231
+ const fixtureCallInformation = {} as FixtureCallInformation;
232
+ analyzeFixtureCall(fixtureCall, fixtureCallInformation, sourceCode);
233
+
234
+ // convert url from `/sample-service/v1/ping` to `${BASE_PATH}/ping`
235
+ const originalUrlArgumentText = sourceCode.getText(urlArgumentNode);
236
+ const fetchUrlArgumentText = replaceEndpointUrlPrefixWithBasePath(originalUrlArgumentText);
237
+
238
+ // fetch request argument
239
+ const methodNode = fixtureFunction.property; // get/put/etc.
240
+ assert.ok(methodNode.type === 'Identifier');
241
+ const fetchRequestArgumentLines = [
242
+ '{',
243
+ ` method: '${methodNode.name.toUpperCase()}',`,
244
+ ...(fixtureCallInformation.requestBody
245
+ ? [` body: JSON.stringify(${sourceCode.getText(fixtureCallInformation.requestBody)}),`]
246
+ : []),
247
+ ...(fixtureCallInformation.requestHeaders
248
+ ? [
249
+ ` headers: {`,
250
+ ...fixtureCallInformation.requestHeaders.map(
251
+ ({ name, value }) =>
252
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions, no-nested-ternary, sonarjs/no-nested-template-literals
253
+ ` ${name.type === 'Literal' ? (isValidPropertyName(name.value) ? name.value : `'${name.value}'`) : `[${sourceCode.getText(name)}]`}: ${sourceCode.getText(value)},`,
254
+ ),
255
+ ` },`,
256
+ ]
257
+ : []),
258
+ '}',
259
+ ].join(`\n${indentation}`);
260
+
261
+ const responseVariableNameToUse = 'res';
262
+ const { statusAssertion, nonStatusAssertions } = createResponseAssertions(
263
+ fixtureCallInformation,
264
+ sourceCode,
265
+ responseVariableNameToUse,
266
+ );
267
+
268
+ // add variable declaration if needed
269
+ const disableLintComment = '// eslint-disable-next-line @checkdigit/no-promise-instance-method';
270
+ const fetchCallText = `fetch(${fetchUrlArgumentText}, ${fetchRequestArgumentLines})`;
271
+ const appendingAssignmentAndAssertionText = [
272
+ ...(statusAssertion !== undefined ? [statusAssertion] : []),
273
+ ...nonStatusAssertions,
274
+ ].join(`;\n${indentation}`);
275
+ const replacementText = fixtureCallInformation.assertions
276
+ ? [
277
+ disableLintComment,
278
+ `${fetchCallText}.then((${responseVariableNameToUse}) => {`,
279
+ appendingAssignmentAndAssertionText === '' ? '' : ` ${appendingAssignmentAndAssertionText};`,
280
+ ` return ${responseVariableNameToUse};`,
281
+ `})`,
282
+ ].join(`\n${indentation}`)
283
+ : fetchCallText;
284
+
285
+ context.report({
286
+ node: fixtureCall,
287
+ messageId: 'preferNativeFetch',
288
+ fix(fixer) {
289
+ return fixer.replaceText(fixtureCallInformation.fixtureNode, replacementText);
290
+ },
291
+ });
292
+
293
+ const responsesVariable = getEnclosingStatement(fixtureCallInformation.fixtureNode);
294
+ if (!responsesVariable) {
295
+ return;
296
+ }
297
+
298
+ const responseVariableReferences = scopeManager.getDeclaredVariables(responsesVariable);
299
+ const responseHeadersAccesses = getResponseHeadersAccesses(
300
+ responseVariableReferences,
301
+ scopeManager,
302
+ sourceCode,
303
+ );
304
+ for (const responseHeadersAccess of responseHeadersAccesses) {
305
+ if (isInvalidResponseHeadersAccess(responseHeadersAccess)) {
306
+ const headerAccess = getParent(responseHeadersAccess);
307
+ if (headerAccess?.type === 'MemberExpression') {
308
+ const headerNameNode = headerAccess.property;
309
+ const headerName = headerAccess.computed
310
+ ? sourceCode.getText(headerNameNode)
311
+ : `'${sourceCode.getText(headerNameNode)}'`;
312
+ const headerAccessReplacementText = `${sourceCode.getText(headerAccess.object)}.get(${headerName})`;
313
+
314
+ context.report({
315
+ node: headerAccess,
316
+ messageId: 'shouldUseHeaderGetter',
317
+ fix(fixer) {
318
+ return fixer.replaceText(headerAccess, headerAccessReplacementText);
319
+ },
320
+ });
321
+ } else if (
322
+ headerAccess?.type === 'CallExpression' &&
323
+ responseHeadersAccess.property.type === 'Identifier' &&
324
+ responseHeadersAccess.property.name === 'get'
325
+ ) {
326
+ const headerAccessReplacementText = `${sourceCode.getText(responseHeadersAccess.object)}.headers.get(${sourceCode.getText(headerAccess.arguments[0])})`;
327
+
328
+ context.report({
329
+ node: headerAccess,
330
+ messageId: 'shouldUseHeaderGetter',
331
+ fix(fixer) {
332
+ return fixer.replaceText(headerAccess, headerAccessReplacementText);
333
+ },
334
+ });
335
+ }
336
+ }
337
+ }
338
+ } catch (error) {
339
+ // eslint-disable-next-line no-console
340
+ console.error(`Failed to apply ${ruleId} rule for file "${context.filename}":`, error);
341
+ context.report({
342
+ node: fixtureCall,
343
+ messageId: 'unknownError',
344
+ data: {
345
+ fileName: context.filename,
346
+ error: error instanceof Error ? error.toString() : JSON.stringify(error),
347
+ },
348
+ });
349
+ }
350
+ },
351
+ };
352
+ },
353
+ };
354
+
355
+ export default rule;
@@ -1,5 +1,52 @@
1
1
  // fixture/fetch.ts
2
2
 
3
+ import { getParent, isBlockStatement } from '../ast/tree';
4
+ import type { Node } from 'estree';
5
+
3
6
  export function getResponseBodyRetrievalText(responseVariableName: string) {
4
7
  return `await ${responseVariableName}.json()`;
5
8
  }
9
+
10
+ export function isInvalidResponseHeadersAccess(responseHeadersAccess: Node) {
11
+ const responseHeaderAccessParent = getParent(responseHeadersAccess);
12
+ if (responseHeaderAccessParent?.type === 'VariableDeclarator') {
13
+ return false;
14
+ }
15
+
16
+ if (
17
+ responseHeaderAccessParent?.type === 'CallExpression' &&
18
+ responseHeaderAccessParent.callee.type === 'MemberExpression' &&
19
+ responseHeaderAccessParent.callee.property.type === 'Identifier' &&
20
+ responseHeaderAccessParent.callee.property.name === 'get'
21
+ ) {
22
+ return true;
23
+ }
24
+
25
+ return !(
26
+ responseHeaderAccessParent?.type === 'MemberExpression' &&
27
+ responseHeaderAccessParent.property.type === 'Identifier' &&
28
+ responseHeaderAccessParent.property.name === 'get'
29
+ );
30
+ }
31
+
32
+ export function hasAssertions(fixtureCall: Node) {
33
+ if (isBlockStatement(fixtureCall)) {
34
+ return false;
35
+ }
36
+
37
+ const parent = getParent(fixtureCall);
38
+ if (!parent) {
39
+ return false;
40
+ }
41
+
42
+ if (
43
+ parent.type === 'MemberExpression' &&
44
+ parent.property.type === 'Identifier' &&
45
+ parent.property.name === 'expect' &&
46
+ getParent(parent)?.type === 'CallExpression'
47
+ ) {
48
+ return true;
49
+ }
50
+
51
+ return hasAssertions(parent);
52
+ }
@@ -17,19 +17,25 @@ import type {
17
17
  VariableDeclaration,
18
18
  } from 'estree';
19
19
  import { type Rule, type Scope, SourceCode } from 'eslint';
20
- import { getEnclosingScopeNode, getEnclosingStatement, getParent } from '../ast/tree';
20
+ import {
21
+ getEnclosingFunction,
22
+ getEnclosingScopeNode,
23
+ getEnclosingStatement,
24
+ getParent,
25
+ isUsedInArrayOrAsArgument,
26
+ } from '../ast/tree';
27
+ import { getResponseBodyRetrievalText, hasAssertions } from './fetch';
21
28
  import { analyzeResponseReferences } from './response-reference';
22
29
  import { strict as assert } from 'node:assert';
23
30
  import getDocumentationUrl from '../get-documentation-url';
24
31
  import { getIndentation } from '../ast/format';
25
- import { getResponseBodyRetrievalText } from './fetch';
26
32
  import { isValidPropertyName } from './variable';
27
33
  import { replaceEndpointUrlPrefixWithBasePath } from './url';
28
34
 
29
35
  export const ruleId = 'no-fixture';
30
36
 
31
37
  interface FixtureCallInformation {
32
- rootNode: AwaitExpression | ReturnStatement | VariableDeclaration;
38
+ rootNode: AwaitExpression | ReturnStatement | VariableDeclaration | SimpleCallExpression;
33
39
  fixtureNode: AwaitExpression | SimpleCallExpression;
34
40
  variableDeclaration?: VariableDeclaration;
35
41
  requestBody?: Expression;
@@ -40,6 +46,7 @@ interface FixtureCallInformation {
40
46
  }
41
47
 
42
48
  // recursively analyze the fixture/supertest call chain to collect information of request/response
49
+ // eslint-disable-next-line sonarjs/cognitive-complexity
43
50
  function analyzeFixtureCall(call: SimpleCallExpression, results: FixtureCallInformation, sourceCode: SourceCode) {
44
51
  const parent = getParent(call);
45
52
  assert.ok(parent, 'parent should exist for fixture/supertest call node');
@@ -49,6 +56,10 @@ function analyzeFixtureCall(call: SimpleCallExpression, results: FixtureCallInfo
49
56
  // direct return, no variable declaration or await
50
57
  results.fixtureNode = call;
51
58
  results.rootNode = parent;
59
+ } else if (parent.type === 'ArrayExpression' || parent.type === 'CallExpression') {
60
+ // direct return, no variable declaration or await
61
+ results.fixtureNode = call;
62
+ results.rootNode = call;
52
63
  } else if (parent.type === 'AwaitExpression') {
53
64
  results.fixtureNode = call;
54
65
  const enclosingStatement = getEnclosingStatement(parent);
@@ -235,6 +246,14 @@ const rule: Rule.RuleModule = {
235
246
  fixtureCall: CallExpression,
236
247
  ) => {
237
248
  try {
249
+ if (
250
+ hasAssertions(fixtureCall) &&
251
+ (isUsedInArrayOrAsArgument(fixtureCall) || getEnclosingFunction(fixtureCall)?.async === false)
252
+ ) {
253
+ // skip and leave it to "fetch-then" rule to handle it because no "await" can be used here
254
+ return;
255
+ }
256
+
238
257
  assert.ok(fixtureCall.type === 'CallExpression');
239
258
  const fixtureFunction = fixtureCall.callee; // e.g. fixture.api.get
240
259
  assert.ok(fixtureFunction.type === 'MemberExpression');
@@ -52,27 +52,19 @@ export function analyzeResponseReferences(
52
52
  // e.g. response.body
53
53
  results.bodyReferences = responseReferences.filter(
54
54
  (node): node is MemberExpression =>
55
- node !== null &&
56
- node !== undefined &&
57
- node.type === 'MemberExpression' &&
58
- node.property.type === 'Identifier' &&
59
- node.property.name === 'body',
55
+ node?.type === 'MemberExpression' && node.property.type === 'Identifier' && node.property.name === 'body',
60
56
  );
61
57
  // e.g. response.headers / response.header / response.get()
62
58
  results.headersReferences = responseReferences.filter(
63
59
  (node): node is MemberExpression =>
64
- node !== null &&
65
- node !== undefined &&
66
- node.type === 'MemberExpression' &&
60
+ node?.type === 'MemberExpression' &&
67
61
  node.property.type === 'Identifier' &&
68
62
  (node.property.name === 'header' || node.property.name === 'headers' || node.property.name === 'get'),
69
63
  );
70
64
  // e.g. response.status / response.statusCode
71
65
  results.statusReferences = responseReferences.filter(
72
66
  (node): node is MemberExpression =>
73
- node !== null &&
74
- node !== undefined &&
75
- node.type === 'MemberExpression' &&
67
+ node?.type === 'MemberExpression' &&
76
68
  node.property.type === 'Identifier' &&
77
69
  (node.property.name === 'status' || node.property.name === 'statusCode'),
78
70
  );
package/src/index.ts CHANGED
@@ -6,8 +6,8 @@
6
6
  * This code is licensed under the MIT license (see LICENSE.txt for details).
7
7
  */
8
8
 
9
- import concurrentPromises, { ruleId as concurrentPromisesRuleId } from './fixture/concurrent-promises';
10
9
  import fetchHeaderGetter, { ruleId as fetchHeaderGetterRuleId } from './fixture/fetch-header-getter';
10
+ import fetchThen, { ruleId as fetchThenRuleId } from './fixture/fetch-then';
11
11
  import invalidJsonStringify, { ruleId as invalidJsonStringifyRuleId } from './invalid-json-stringify';
12
12
  import noFixture, { ruleId as noFixtureRuleId } from './fixture/no-fixture';
13
13
  import noPromiseInstanceMethod, { ruleId as noPromiseInstanceMethodRuleId } from './no-promise-instance-method';
@@ -36,7 +36,7 @@ export default {
36
36
  [noPromiseInstanceMethodRuleId]: noPromiseInstanceMethod,
37
37
  [noFixtureRuleId]: noFixture,
38
38
  [fetchHeaderGetterRuleId]: fetchHeaderGetter,
39
- [concurrentPromisesRuleId]: concurrentPromises,
39
+ [fetchThenRuleId]: fetchThen,
40
40
  },
41
41
  configs: {
42
42
  all: {
@@ -54,7 +54,7 @@ export default {
54
54
  [`@checkdigit/${noPromiseInstanceMethodRuleId}`]: 'error',
55
55
  [`@checkdigit/${noFixtureRuleId}`]: 'error',
56
56
  [`@checkdigit/${fetchHeaderGetterRuleId}`]: 'error',
57
- [`@checkdigit/${concurrentPromisesRuleId}`]: 'error',
57
+ [`@checkdigit/${fetchThenRuleId}`]: 'error',
58
58
  },
59
59
  },
60
60
  recommended: {