@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.
@@ -1,191 +0,0 @@
1
- // src/fixture/concurrent-promises.ts
2
- import "eslint";
3
- import { strict as assert } from "node:assert";
4
- import getDocumentationUrl from "../get-documentation-url.mjs";
5
- import { getIndentation } from "../ast/format.mjs";
6
- import { getParent } from "../ast/tree.mjs";
7
- import { isValidPropertyName } from "./variable.mjs";
8
- import { replaceEndpointUrlPrefixWithBasePath } from "./url.mjs";
9
- var ruleId = "concurrent-promises";
10
- function analyzeFixtureCall(call, results, sourceCode) {
11
- const parent = getParent(call);
12
- if (!parent) {
13
- return;
14
- }
15
- let nextCall;
16
- if (parent.type === "ArrayExpression") {
17
- results.fixtureNode = call;
18
- } else if (parent.type === "MemberExpression" && parent.property.type === "Identifier") {
19
- if (parent.property.name === "expect") {
20
- const assertionCall = getParent(parent);
21
- assert.ok(assertionCall && assertionCall.type === "CallExpression");
22
- results.assertions = [...results.assertions ?? [], assertionCall.arguments];
23
- nextCall = assertionCall;
24
- } else if (parent.property.name === "send") {
25
- const sendRequestBodyCall = getParent(parent);
26
- assert.ok(sendRequestBodyCall && sendRequestBodyCall.type === "CallExpression");
27
- results.requestBody = sendRequestBodyCall.arguments[0];
28
- nextCall = sendRequestBodyCall;
29
- } else if (parent.property.name === "set") {
30
- const setRequestHeaderCall = getParent(parent);
31
- assert.ok(setRequestHeaderCall && setRequestHeaderCall.type === "CallExpression");
32
- const [name, value] = setRequestHeaderCall.arguments;
33
- results.requestHeaders = [...results.requestHeaders ?? [], { name, value }];
34
- nextCall = setRequestHeaderCall;
35
- }
36
- } else {
37
- throw new Error(`Unexpected expression in fixture/supertest call ${sourceCode.getText(parent)}.`);
38
- }
39
- if (nextCall) {
40
- analyzeFixtureCall(nextCall, results, sourceCode);
41
- }
42
- }
43
- function createResponseAssertions(fixtureCallInformation, sourceCode, responseVariableName) {
44
- let statusAssertion;
45
- const nonStatusAssertions = [];
46
- for (const expectArguments of fixtureCallInformation.assertions ?? []) {
47
- if (expectArguments.length === 1) {
48
- const [assertionArgument] = expectArguments;
49
- assert.ok(assertionArgument);
50
- if (assertionArgument.type === "MemberExpression" && assertionArgument.object.type === "Identifier" && assertionArgument.object.name === "StatusCodes" || assertionArgument.type === "Literal" || sourceCode.getText(assertionArgument).includes("StatusCodes.")) {
51
- statusAssertion = `assert.equal(${responseVariableName}.status, ${sourceCode.getText(assertionArgument)})`;
52
- } else if (assertionArgument.type === "ArrowFunctionExpression") {
53
- let functionBody = sourceCode.getText(assertionArgument.body);
54
- const [originalResponseArgument] = assertionArgument.params;
55
- assert.ok(originalResponseArgument?.type === "Identifier");
56
- const originalResponseArgumentName = originalResponseArgument.name;
57
- if (originalResponseArgumentName !== responseVariableName) {
58
- functionBody = functionBody.replace(
59
- new RegExp(`\\b${originalResponseArgumentName}\\b`, "ug"),
60
- responseVariableName
61
- );
62
- }
63
- nonStatusAssertions.push(`assert.ok(${functionBody})`);
64
- } else if (assertionArgument.type === "Identifier") {
65
- nonStatusAssertions.push(`assert.ok(${sourceCode.getText(assertionArgument)}(${responseVariableName}))`);
66
- } else if (assertionArgument.type === "ObjectExpression" || assertionArgument.type === "CallExpression") {
67
- nonStatusAssertions.push(
68
- `assert.deepEqual(await ${responseVariableName}.json(), ${sourceCode.getText(assertionArgument)})`
69
- );
70
- } else {
71
- throw new Error(`Unexpected Supertest assertion argument: ".expect(${sourceCode.getText(assertionArgument)})`);
72
- }
73
- } else if (expectArguments.length === 2) {
74
- const [headerName, headerValue] = expectArguments;
75
- assert.ok(headerName && headerValue);
76
- const headersReference = `${responseVariableName}.headers`;
77
- if (headerValue.type === "Literal" && headerValue.value instanceof RegExp) {
78
- nonStatusAssertions.push(
79
- `assert.ok(${headersReference}.get(${sourceCode.getText(headerName)}).match(${sourceCode.getText(headerValue)}))`
80
- );
81
- } else {
82
- nonStatusAssertions.push(
83
- `assert.equal(${headersReference}.get(${sourceCode.getText(headerName)}), ${sourceCode.getText(headerValue)})`
84
- );
85
- }
86
- }
87
- }
88
- return {
89
- statusAssertion,
90
- nonStatusAssertions
91
- };
92
- }
93
- var rule = {
94
- meta: {
95
- type: "suggestion",
96
- docs: {
97
- description: "Prefer native fetch API over customized fixture API.",
98
- url: getDocumentationUrl(ruleId)
99
- },
100
- messages: {
101
- preferNativeFetch: "Prefer native fetch API over customized fixture API.",
102
- unknownError: 'Unknown error occurred in file "{{fileName}}": {{ error }}. Please manually convert the fixture API call to fetch API call.'
103
- },
104
- fixable: "code",
105
- schema: []
106
- },
107
- // eslint-disable-next-line max-lines-per-function
108
- create(context) {
109
- const sourceCode = context.sourceCode;
110
- return {
111
- // eslint-disable-next-line max-lines-per-function
112
- 'CallExpression[callee.object.name="Promise"] > ArrayExpression CallExpression[callee.object.object.name="fixture"][callee.object.property.name="api"]': (fixtureCall) => {
113
- try {
114
- assert.ok(fixtureCall.type === "CallExpression");
115
- const fixtureFunction = fixtureCall.callee;
116
- assert.ok(fixtureFunction.type === "MemberExpression");
117
- const indentation = getIndentation(fixtureCall, sourceCode);
118
- const [urlArgumentNode] = fixtureCall.arguments;
119
- assert.ok(urlArgumentNode !== void 0);
120
- const fixtureCallInformation = {};
121
- analyzeFixtureCall(fixtureCall, fixtureCallInformation, sourceCode);
122
- const originalUrlArgumentText = sourceCode.getText(urlArgumentNode);
123
- const fetchUrlArgumentText = replaceEndpointUrlPrefixWithBasePath(originalUrlArgumentText);
124
- const methodNode = fixtureFunction.property;
125
- assert.ok(methodNode.type === "Identifier");
126
- const fetchRequestArgumentLines = [
127
- "{",
128
- ` method: '${methodNode.name.toUpperCase()}',`,
129
- ...fixtureCallInformation.requestBody ? [` body: JSON.stringify(${sourceCode.getText(fixtureCallInformation.requestBody)}),`] : [],
130
- ...fixtureCallInformation.requestHeaders ? [
131
- ` headers: {`,
132
- ...fixtureCallInformation.requestHeaders.map(
133
- ({ name, value }) => (
134
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions, no-nested-ternary, sonarjs/no-nested-template-literals
135
- ` ${name.type === "Literal" ? isValidPropertyName(name.value) ? name.value : `'${name.value}'` : `[${sourceCode.getText(name)}]`}: ${sourceCode.getText(value)},`
136
- )
137
- ),
138
- ` },`
139
- ] : [],
140
- "}"
141
- ].join(`
142
- ${indentation}`);
143
- const responseVariableNameToUse = "res";
144
- const { statusAssertion, nonStatusAssertions } = createResponseAssertions(
145
- fixtureCallInformation,
146
- sourceCode,
147
- responseVariableNameToUse
148
- );
149
- const disableLintComment = "// eslint-disable-next-line @checkdigit/no-promise-instance-method";
150
- const fetchCallText = `fetch(${fetchUrlArgumentText}, ${fetchRequestArgumentLines})`;
151
- const appendingAssignmentAndAssertionText = [
152
- ...statusAssertion !== void 0 ? [statusAssertion] : [],
153
- ...nonStatusAssertions
154
- ].join(`;
155
- ${indentation}`);
156
- const replacementText = [
157
- disableLintComment,
158
- `${fetchCallText}.then((${responseVariableNameToUse}) => {`,
159
- appendingAssignmentAndAssertionText === "" ? "" : ` ${appendingAssignmentAndAssertionText};`,
160
- ` return ${responseVariableNameToUse};`,
161
- `})`
162
- ].join(`
163
- ${indentation}`);
164
- context.report({
165
- node: fixtureCall,
166
- messageId: "preferNativeFetch",
167
- fix(fixer) {
168
- return fixer.replaceText(fixtureCallInformation.fixtureNode, replacementText);
169
- }
170
- });
171
- } catch (error) {
172
- console.error(`Failed to apply ${ruleId} rule for file "${context.filename}":`, error);
173
- context.report({
174
- node: fixtureCall,
175
- messageId: "unknownError",
176
- data: {
177
- fileName: context.filename,
178
- error: error instanceof Error ? error.toString() : JSON.stringify(error)
179
- }
180
- });
181
- }
182
- }
183
- };
184
- }
185
- };
186
- var concurrent_promises_default = rule;
187
- export {
188
- concurrent_promises_default as default,
189
- ruleId
190
- };
191
- //# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsiLi4vLi4vc3JjL2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BSSxDQUFDO0FBQUEsWUFDekQsR0FBRztBQUFBLFVBQ0wsRUFBRSxLQUFLO0FBQUEsRUFBTSxXQUFXLEVBQUU7QUFDMUIsZ0JBQU0sa0JBQWtCO0FBQUEsWUFDdEI7QUFBQSxZQUNBLEdBQUcsYUFBYSxVQUFVLHlCQUF5QjtBQUFBLFlBQ25ELHdDQUF3QyxLQUFLLEtBQUssS0FBSyxtQ0FBbUM7QUFBQSxZQUMxRixZQUFZLHlCQUF5QjtBQUFBLFlBQ3JDO0FBQUEsVUFDRixFQUFFLEtBQUs7QUFBQSxFQUFLLFdBQVcsRUFBRTtBQUV6QixrQkFBUSxPQUFPO0FBQUEsWUFDYixNQUFNO0FBQUEsWUFDTixXQUFXO0FBQUEsWUFDWCxJQUFJLE9BQU87QUFDVCxxQkFBTyxNQUFNLFlBQVksdUJBQXVCLGFBQWEsZUFBZTtBQUFBLFlBQzlFO0FBQUEsVUFDRixDQUFDO0FBQUEsUUFDSCxTQUFTLE9BQU87QUFFZCxrQkFBUSxNQUFNLG1CQUFtQixNQUFNLG1CQUFtQixRQUFRLFFBQVEsTUFBTSxLQUFLO0FBQ3JGLGtCQUFRLE9BQU87QUFBQSxZQUNiLE1BQU07QUFBQSxZQUNOLFdBQVc7QUFBQSxZQUNYLE1BQU07QUFBQSxjQUNKLFVBQVUsUUFBUTtBQUFBLGNBQ2xCLE9BQU8saUJBQWlCLFFBQVEsTUFBTSxTQUFTLElBQUksS0FBSyxVQUFVLEtBQUs7QUFBQSxZQUN6RTtBQUFBLFVBQ0YsQ0FBQztBQUFBLFFBQ0g7QUFBQSxNQUNGO0FBQUEsSUFDSjtBQUFBLEVBQ0Y7QUFDRjtBQUVBLElBQU8sOEJBQVE7IiwKICAibmFtZXMiOiBbXQp9Cg==
@@ -1,242 +0,0 @@
1
- // fixture/concurrent-promises.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, SimpleCallExpression } from 'estree';
10
- import { type Rule, SourceCode } from 'eslint';
11
- import { strict as assert } from 'node:assert';
12
- import getDocumentationUrl from '../get-documentation-url';
13
- import { getIndentation } from '../ast/format';
14
- import { getParent } from '../ast/tree';
15
- import { isValidPropertyName } from './variable';
16
- import { replaceEndpointUrlPrefixWithBasePath } from './url';
17
-
18
- export const ruleId = 'concurrent-promises';
19
-
20
- interface FixtureCallInformation {
21
- fixtureNode: SimpleCallExpression;
22
- requestBody?: Expression;
23
- requestHeaders?: { name: Expression; value: Expression }[];
24
- assertions?: Expression[][];
25
- }
26
-
27
- // recursively analyze the fixture/supertest call chain to collect information of request/response
28
- function analyzeFixtureCall(call: SimpleCallExpression, results: FixtureCallInformation, sourceCode: SourceCode) {
29
- const parent = getParent(call);
30
- if (!parent) {
31
- return;
32
- }
33
-
34
- let nextCall;
35
- if (parent.type === 'ArrayExpression') {
36
- results.fixtureNode = call;
37
- } else if (parent.type === 'MemberExpression' && parent.property.type === 'Identifier') {
38
- if (parent.property.name === 'expect') {
39
- // supertest assertions
40
- const assertionCall = getParent(parent);
41
- assert.ok(assertionCall && assertionCall.type === 'CallExpression');
42
- results.assertions = [...(results.assertions ?? []), assertionCall.arguments as Expression[]];
43
- nextCall = assertionCall;
44
- } else if (parent.property.name === 'send') {
45
- // request body
46
- const sendRequestBodyCall = getParent(parent);
47
- assert.ok(sendRequestBodyCall && sendRequestBodyCall.type === 'CallExpression');
48
- results.requestBody = sendRequestBodyCall.arguments[0] as Expression;
49
- nextCall = sendRequestBodyCall;
50
- } else if (parent.property.name === 'set') {
51
- // request headers
52
- const setRequestHeaderCall = getParent(parent);
53
- assert.ok(setRequestHeaderCall && setRequestHeaderCall.type === 'CallExpression');
54
- const [name, value] = setRequestHeaderCall.arguments as [Expression, Expression];
55
- results.requestHeaders = [...(results.requestHeaders ?? []), { name, value }];
56
- nextCall = setRequestHeaderCall;
57
- }
58
- } else {
59
- throw new Error(`Unexpected expression in fixture/supertest call ${sourceCode.getText(parent)}.`);
60
- }
61
- if (nextCall) {
62
- analyzeFixtureCall(nextCall, results, sourceCode);
63
- }
64
- }
65
-
66
- // eslint-disable-next-line sonarjs/cognitive-complexity
67
- function createResponseAssertions(
68
- fixtureCallInformation: FixtureCallInformation,
69
- sourceCode: SourceCode,
70
- responseVariableName: string,
71
- ) {
72
- let statusAssertion: string | undefined;
73
- const nonStatusAssertions: string[] = [];
74
- for (const expectArguments of fixtureCallInformation.assertions ?? []) {
75
- if (expectArguments.length === 1) {
76
- const [assertionArgument] = expectArguments;
77
- assert.ok(assertionArgument);
78
- if (
79
- (assertionArgument.type === 'MemberExpression' &&
80
- assertionArgument.object.type === 'Identifier' &&
81
- assertionArgument.object.name === 'StatusCodes') ||
82
- assertionArgument.type === 'Literal' ||
83
- sourceCode.getText(assertionArgument).includes('StatusCodes.')
84
- ) {
85
- // status code assertion
86
- statusAssertion = `assert.equal(${responseVariableName}.status, ${sourceCode.getText(assertionArgument)})`;
87
- } else if (assertionArgument.type === 'ArrowFunctionExpression') {
88
- // callback assertion using arrow function
89
- let functionBody = sourceCode.getText(assertionArgument.body);
90
-
91
- const [originalResponseArgument] = assertionArgument.params;
92
- assert.ok(originalResponseArgument?.type === 'Identifier');
93
- const originalResponseArgumentName = originalResponseArgument.name;
94
- if (originalResponseArgumentName !== responseVariableName) {
95
- functionBody = functionBody.replace(
96
- new RegExp(`\\b${originalResponseArgumentName}\\b`, 'ug'),
97
- responseVariableName,
98
- );
99
- }
100
- nonStatusAssertions.push(`assert.ok(${functionBody})`);
101
- } else if (assertionArgument.type === 'Identifier') {
102
- // callback assertion using function reference
103
- nonStatusAssertions.push(`assert.ok(${sourceCode.getText(assertionArgument)}(${responseVariableName}))`);
104
- } else if (assertionArgument.type === 'ObjectExpression' || assertionArgument.type === 'CallExpression') {
105
- // body deep equal assertion
106
- nonStatusAssertions.push(
107
- `assert.deepEqual(await ${responseVariableName}.json(), ${sourceCode.getText(assertionArgument)})`,
108
- );
109
- } else {
110
- throw new Error(`Unexpected Supertest assertion argument: ".expect(${sourceCode.getText(assertionArgument)})`);
111
- }
112
- } else if (expectArguments.length === 2) {
113
- // header assertion
114
- const [headerName, headerValue] = expectArguments;
115
- assert.ok(headerName && headerValue);
116
- const headersReference = `${responseVariableName}.headers`;
117
- if (headerValue.type === 'Literal' && headerValue.value instanceof RegExp) {
118
- nonStatusAssertions.push(
119
- `assert.ok(${headersReference}.get(${sourceCode.getText(headerName)}).match(${sourceCode.getText(headerValue)}))`,
120
- );
121
- } else {
122
- nonStatusAssertions.push(
123
- `assert.equal(${headersReference}.get(${sourceCode.getText(headerName)}), ${sourceCode.getText(headerValue)})`,
124
- );
125
- }
126
- }
127
- }
128
- return {
129
- statusAssertion,
130
- nonStatusAssertions,
131
- };
132
- }
133
-
134
- const rule: Rule.RuleModule = {
135
- meta: {
136
- type: 'suggestion',
137
- docs: {
138
- description: 'Prefer native fetch API over customized fixture API.',
139
- url: getDocumentationUrl(ruleId),
140
- },
141
- messages: {
142
- preferNativeFetch: 'Prefer native fetch API over customized fixture API.',
143
- unknownError:
144
- 'Unknown error occurred in file "{{fileName}}": {{ error }}. Please manually convert the fixture API call to fetch API call.',
145
- },
146
- fixable: 'code',
147
- schema: [],
148
- },
149
- // eslint-disable-next-line max-lines-per-function
150
- create(context) {
151
- const sourceCode = context.sourceCode;
152
-
153
- return {
154
- // eslint-disable-next-line max-lines-per-function
155
- 'CallExpression[callee.object.name="Promise"] > ArrayExpression CallExpression[callee.object.object.name="fixture"][callee.object.property.name="api"]':
156
- (fixtureCall: CallExpression) => {
157
- try {
158
- assert.ok(fixtureCall.type === 'CallExpression');
159
- const fixtureFunction = fixtureCall.callee; // e.g. fixture.api.get
160
- assert.ok(fixtureFunction.type === 'MemberExpression');
161
- const indentation = getIndentation(fixtureCall, sourceCode);
162
-
163
- const [urlArgumentNode] = fixtureCall.arguments; // e.g. `/sample-service/v1/ping`
164
- assert.ok(urlArgumentNode !== undefined);
165
-
166
- const fixtureCallInformation = {} as FixtureCallInformation;
167
- analyzeFixtureCall(fixtureCall, fixtureCallInformation, sourceCode);
168
-
169
- // convert url from `/sample-service/v1/ping` to `${BASE_PATH}/ping`
170
- const originalUrlArgumentText = sourceCode.getText(urlArgumentNode);
171
- const fetchUrlArgumentText = replaceEndpointUrlPrefixWithBasePath(originalUrlArgumentText);
172
-
173
- // fetch request argument
174
- const methodNode = fixtureFunction.property; // get/put/etc.
175
- assert.ok(methodNode.type === 'Identifier');
176
- const fetchRequestArgumentLines = [
177
- '{',
178
- ` method: '${methodNode.name.toUpperCase()}',`,
179
- ...(fixtureCallInformation.requestBody
180
- ? [` body: JSON.stringify(${sourceCode.getText(fixtureCallInformation.requestBody)}),`]
181
- : []),
182
- ...(fixtureCallInformation.requestHeaders
183
- ? [
184
- ` headers: {`,
185
- ...fixtureCallInformation.requestHeaders.map(
186
- ({ name, value }) =>
187
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions, no-nested-ternary, sonarjs/no-nested-template-literals
188
- ` ${name.type === 'Literal' ? (isValidPropertyName(name.value) ? name.value : `'${name.value}'`) : `[${sourceCode.getText(name)}]`}: ${sourceCode.getText(value)},`,
189
- ),
190
- ` },`,
191
- ]
192
- : []),
193
- '}',
194
- ].join(`\n${indentation}`);
195
-
196
- const responseVariableNameToUse = 'res';
197
- const { statusAssertion, nonStatusAssertions } = createResponseAssertions(
198
- fixtureCallInformation,
199
- sourceCode,
200
- responseVariableNameToUse,
201
- );
202
-
203
- // add variable declaration if needed
204
- const disableLintComment = '// eslint-disable-next-line @checkdigit/no-promise-instance-method';
205
- const fetchCallText = `fetch(${fetchUrlArgumentText}, ${fetchRequestArgumentLines})`;
206
- const appendingAssignmentAndAssertionText = [
207
- ...(statusAssertion !== undefined ? [statusAssertion] : []),
208
- ...nonStatusAssertions,
209
- ].join(`;\n${indentation}`);
210
- const replacementText = [
211
- disableLintComment,
212
- `${fetchCallText}.then((${responseVariableNameToUse}) => {`,
213
- appendingAssignmentAndAssertionText === '' ? '' : ` ${appendingAssignmentAndAssertionText};`,
214
- ` return ${responseVariableNameToUse};`,
215
- `})`,
216
- ].join(`\n${indentation}`);
217
-
218
- context.report({
219
- node: fixtureCall,
220
- messageId: 'preferNativeFetch',
221
- fix(fixer) {
222
- return fixer.replaceText(fixtureCallInformation.fixtureNode, replacementText);
223
- },
224
- });
225
- } catch (error) {
226
- // eslint-disable-next-line no-console
227
- console.error(`Failed to apply ${ruleId} rule for file "${context.filename}":`, error);
228
- context.report({
229
- node: fixtureCall,
230
- messageId: 'unknownError',
231
- data: {
232
- fileName: context.filename,
233
- error: error instanceof Error ? error.toString() : JSON.stringify(error),
234
- },
235
- });
236
- }
237
- },
238
- };
239
- },
240
- };
241
-
242
- export default rule;