@checkdigit/eslint-plugin 6.6.0-PR.75-20dc → 6.6.0-PR.75-9891

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/package.json CHANGED
@@ -1 +1 @@
1
- {"name":"@checkdigit/eslint-plugin","version":"6.6.0-PR.75-20dc","description":"Check Digit eslint plugins","keywords":["eslint","eslintplugin"],"homepage":"https://github.com/checkdigit/eslint-plugin#readme","bugs":{"url":"https://github.com/checkdigit/eslint-plugin/issues"},"repository":{"type":"git","url":"https://github.com/checkdigit/eslint-plugin"},"license":"MIT","author":"Check Digit, LLC","sideEffects":false,"type":"module","exports":{".":{"types":"./dist-types/index.d.ts","require":"./dist-cjs/index.cjs","import":"./dist-mjs/index.mjs","default":"./dist-mjs/index.mjs"}},"files":["src","dist-types","dist-cjs","dist-mjs","!src/**/*.test.ts","!src/**/*.spec.ts","!dist-types/**/*.test.d.ts","!dist-types/**/*.spec.d.ts","!dist-cjs/**/*.test.cjs","!dist-cjs/**/*.spec.cjs","!dist-mjs/**/*.test.mjs","!dist-mjs/**/*.spec.mjs","SECURITY.md"],"scripts":{"build:dist-cjs":"rimraf dist-cjs && npx builder --type=commonjs --sourceMap --entryPoint=index.ts --outDir=dist-cjs --outFile=index.cjs --external=espree && echo \"module.exports = module.exports.default;\" >> dist-cjs/index.cjs","build:dist-mjs":"rimraf dist-mjs && npx builder --type=module --sourceMap --outDir=dist-mjs && node dist-mjs/index.mjs","build:dist-types":"rimraf dist-types && npx builder --type=types --outDir=dist-types","ci:compile":"tsc --noEmit","ci:coverage":"NODE_OPTIONS=\"--disable-warning ExperimentalWarning --experimental-vm-modules\" jest --coverage=true","ci:lint":"npm run lint","ci:style":"npm run prettier","ci:test":"NODE_OPTIONS=\"--disable-warning ExperimentalWarning --experimental-vm-modules\" jest --coverage=false","lint":"eslint --max-warnings 0 --ignore-path .gitignore .","lint:fix":"eslint --ignore-path .gitignore . --fix","prepublishOnly":"npm run build:dist-types && npm run build:dist-cjs && npm run build:dist-mjs","prettier":"prettier --ignore-path .gitignore --list-different .","prettier:fix":"prettier --ignore-path .gitignore --write .","test":"npm run ci:compile && npm run ci:test && npm run ci:lint && npm run ci:style"},"prettier":"@checkdigit/prettier-config","jest":{"preset":"@checkdigit/jest-config"},"devDependencies":{"@checkdigit/jest-config":"^6.0.2","@checkdigit/prettier-config":"^5.5.0","@checkdigit/typescript-config":"6.0.0","@types/eslint":"^8.56.10","@typescript-eslint/eslint-plugin":"^7.16.1","@typescript-eslint/parser":"^7.16.1","eslint-config-prettier":"^9.1.0","eslint-plugin-eslint-plugin":"^6.2.0","eslint-plugin-import":"^2.29.1","eslint-plugin-no-only-tests":"^3.1.0","eslint-plugin-no-secrets":"^1.0.2","eslint-plugin-node":"^11.1.0","eslint-plugin-sonarjs":"0.24.0"},"peerDependencies":{"eslint":">=8 <9"},"engines":{"node":">=20.14"}}
1
+ {"name":"@checkdigit/eslint-plugin","version":"6.6.0-PR.75-9891","description":"Check Digit eslint plugins","keywords":["eslint","eslintplugin"],"homepage":"https://github.com/checkdigit/eslint-plugin#readme","bugs":{"url":"https://github.com/checkdigit/eslint-plugin/issues"},"repository":{"type":"git","url":"https://github.com/checkdigit/eslint-plugin"},"license":"MIT","author":"Check Digit, LLC","sideEffects":false,"type":"module","exports":{".":{"types":"./dist-types/index.d.ts","require":"./dist-cjs/index.cjs","import":"./dist-mjs/index.mjs","default":"./dist-mjs/index.mjs"}},"files":["src","dist-types","dist-cjs","dist-mjs","!src/**/*.test.ts","!src/**/*.spec.ts","!dist-types/**/*.test.d.ts","!dist-types/**/*.spec.d.ts","!dist-cjs/**/*.test.cjs","!dist-cjs/**/*.spec.cjs","!dist-mjs/**/*.test.mjs","!dist-mjs/**/*.spec.mjs","SECURITY.md"],"scripts":{"build:dist-cjs":"rimraf dist-cjs && npx builder --type=commonjs --sourceMap --entryPoint=index.ts --outDir=dist-cjs --outFile=index.cjs --external=espree && echo \"module.exports = module.exports.default;\" >> dist-cjs/index.cjs","build:dist-mjs":"rimraf dist-mjs && npx builder --type=module --sourceMap --outDir=dist-mjs && node dist-mjs/index.mjs","build:dist-types":"rimraf dist-types && npx builder --type=types --outDir=dist-types","ci:compile":"tsc --noEmit","ci:coverage":"NODE_OPTIONS=\"--disable-warning ExperimentalWarning --experimental-vm-modules\" jest --coverage=true","ci:lint":"npm run lint","ci:style":"npm run prettier","ci:test":"NODE_OPTIONS=\"--disable-warning ExperimentalWarning --experimental-vm-modules\" jest --coverage=false","lint":"eslint --max-warnings 0 --ignore-path .gitignore .","lint:fix":"eslint --ignore-path .gitignore . --fix","prepublishOnly":"npm run build:dist-types && npm run build:dist-cjs && npm run build:dist-mjs","prettier":"prettier --ignore-path .gitignore --list-different .","prettier:fix":"prettier --ignore-path .gitignore --write .","test":"npm run ci:compile && npm run ci:test && npm run ci:lint && npm run ci:style"},"prettier":"@checkdigit/prettier-config","jest":{"preset":"@checkdigit/jest-config"},"devDependencies":{"@checkdigit/jest-config":"^6.0.2","@checkdigit/prettier-config":"^5.5.0","@checkdigit/typescript-config":"6.0.0","@types/eslint":"^8.56.10","@typescript-eslint/eslint-plugin":"^7.16.1","@typescript-eslint/parser":"^7.16.1","eslint-config-prettier":"^9.1.0","eslint-plugin-eslint-plugin":"^6.2.0","eslint-plugin-import":"^2.29.1","eslint-plugin-no-only-tests":"^3.1.0","eslint-plugin-no-secrets":"^1.0.2","eslint-plugin-node":"^11.1.0","eslint-plugin-sonarjs":"0.24.0"},"peerDependencies":{"eslint":">=8 <9"},"engines":{"node":">=20.14"}}
@@ -0,0 +1,242 @@
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;
@@ -1,4 +1,4 @@
1
- // no-fixture.ts
1
+ // fixture/no-fixture.ts
2
2
 
3
3
  /*
4
4
  * Copyright (c) 2021-2024 Check Digit, LLC
@@ -0,0 +1,5 @@
1
+ // fixture/fetch.ts
2
+
3
+ export function getResponseBodyRetrievalText(responseVariableName: string) {
4
+ return `await ${responseVariableName}.json()`;
5
+ }
@@ -1,4 +1,4 @@
1
- // no-fixture.ts
1
+ // fixture/no-fixture.ts
2
2
 
3
3
  /*
4
4
  * Copyright (c) 2021-2024 Check Digit, LLC
@@ -22,6 +22,9 @@ import { analyzeResponseReferences } from './response-reference';
22
22
  import { strict as assert } from 'node:assert';
23
23
  import getDocumentationUrl from '../get-documentation-url';
24
24
  import { getIndentation } from '../ast/format';
25
+ import { getResponseBodyRetrievalText } from './fetch';
26
+ import { isValidPropertyName } from './variable';
27
+ import { replaceEndpointUrlPrefixWithBasePath } from './url';
25
28
 
26
29
  export const ruleId = 'no-fixture';
27
30
 
@@ -92,16 +95,6 @@ function analyzeFixtureCall(call: SimpleCallExpression, results: FixtureCallInfo
92
95
  }
93
96
  }
94
97
 
95
- // `/sample-service/v1/ping` -> `${BASE_PATH}/ping`
96
- function replaceEndpointUrlPrefixWithBasePath(url: string) {
97
- // eslint-disable-next-line no-template-curly-in-string
98
- return url.replace(/`\/\w+(?<parts>-\w+)*\/v\d+\//u, '`${BASE_PATH}/');
99
- }
100
-
101
- function isValidPropertyName(name: unknown) {
102
- return typeof name === 'string' && /^[a-zA-Z_$][a-zA-Z_$0-9]*$/u.test(name);
103
- }
104
-
105
98
  // eslint-disable-next-line sonarjs/cognitive-complexity
106
99
  function createResponseAssertions(
107
100
  fixtureCallInformation: FixtureCallInformation,
@@ -215,10 +208,6 @@ function isResponseBodyRedefinition(responseBodyReference: MemberExpression): bo
215
208
  return parent?.type === 'VariableDeclarator' && parent.id.type === 'Identifier';
216
209
  }
217
210
 
218
- function getResponseBodyRetrievalText(responseVariableName: string) {
219
- return `await ${responseVariableName}.json()`;
220
- }
221
-
222
211
  const rule: Rule.RuleModule = {
223
212
  meta: {
224
213
  type: 'suggestion',
@@ -1,4 +1,4 @@
1
- // no-fixture.ts
1
+ // fixture/response-reference.ts
2
2
 
3
3
  /*
4
4
  * Copyright (c) 2021-2024 Check Digit, LLC
@@ -0,0 +1,6 @@
1
+ // fixture/url.ts
2
+
3
+ export function replaceEndpointUrlPrefixWithBasePath(url: string) {
4
+ // eslint-disable-next-line no-template-curly-in-string
5
+ return url.replace(/`\/\w+(?<parts>-\w+)*\/v\d+\//u, '`${BASE_PATH}/');
6
+ }
@@ -0,0 +1,5 @@
1
+ // fixture/variable.ts
2
+
3
+ export function isValidPropertyName(name: unknown) {
4
+ return typeof name === 'string' && /^[a-zA-Z_$][a-zA-Z_$0-9]*$/u.test(name);
5
+ }
package/src/index.ts CHANGED
@@ -6,6 +6,7 @@
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';
9
10
  import fetchHeaderGetter, { ruleId as fetchHeaderGetterRuleId } from './fixture/fetch-header-getter';
10
11
  import invalidJsonStringify, { ruleId as invalidJsonStringifyRuleId } from './invalid-json-stringify';
11
12
  import noFixture, { ruleId as noFixtureRuleId } from './fixture/no-fixture';
@@ -35,6 +36,7 @@ export default {
35
36
  [noPromiseInstanceMethodRuleId]: noPromiseInstanceMethod,
36
37
  [noFixtureRuleId]: noFixture,
37
38
  [fetchHeaderGetterRuleId]: fetchHeaderGetter,
39
+ [concurrentPromisesRuleId]: concurrentPromises,
38
40
  },
39
41
  configs: {
40
42
  all: {
@@ -52,6 +54,7 @@ export default {
52
54
  [`@checkdigit/${noPromiseInstanceMethodRuleId}`]: 'error',
53
55
  [`@checkdigit/${noFixtureRuleId}`]: 'error',
54
56
  [`@checkdigit/${fetchHeaderGetterRuleId}`]: 'error',
57
+ [`@checkdigit/${concurrentPromisesRuleId}`]: 'error',
55
58
  },
56
59
  },
57
60
  recommended: {