@checkdigit/eslint-plugin 6.6.0-PR.75-7695 → 6.6.0-PR.75-8ba0

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,75 @@
1
+ // fixture/no-full-response.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 { ESLintUtils, TSESTree } from '@typescript-eslint/utils';
10
+ import { strict as assert } from 'node:assert';
11
+ import getDocumentationUrl from '../get-documentation-url';
12
+ import { getTypeParentNode } from './ts-tree';
13
+
14
+ export const ruleId = 'no-full-response';
15
+
16
+ const createRule = ESLintUtils.RuleCreator((name) => getDocumentationUrl(name));
17
+
18
+ const rule = createRule({
19
+ name: ruleId,
20
+ meta: {
21
+ type: 'suggestion',
22
+ docs: {
23
+ description: 'Remove the usage of FullResponse type.',
24
+ },
25
+ messages: {
26
+ removeFullResponse: 'Remove the usage of FullResponse type.',
27
+ unknownError: 'Unknown error occurred in file "{{fileName}}": {{ error }}.',
28
+ },
29
+ fixable: 'code',
30
+ schema: [],
31
+ },
32
+ defaultOptions: [],
33
+ create(context) {
34
+ const sourceCode = context.sourceCode;
35
+
36
+ return {
37
+ 'TSTypeReference[typeName.name="FullResponse"]': (typeReference: TSESTree.TSTypeReference) => {
38
+ try {
39
+ const typeParentNode = getTypeParentNode(typeReference);
40
+ assert.ok(typeParentNode);
41
+ if (typeParentNode.type === TSESTree.AST_NODE_TYPES.TSAsExpression) {
42
+ context.report({
43
+ messageId: 'removeFullResponse',
44
+ node: typeReference,
45
+ fix(fixer) {
46
+ return fixer.replaceText(typeParentNode, sourceCode.getText(typeParentNode.expression));
47
+ },
48
+ });
49
+ } else {
50
+ context.report({
51
+ messageId: 'removeFullResponse',
52
+ node: typeReference,
53
+ fix(fixer) {
54
+ return fixer.remove(typeParentNode);
55
+ },
56
+ });
57
+ }
58
+ } catch (error) {
59
+ // eslint-disable-next-line no-console
60
+ console.error(`Failed to apply ${ruleId} rule for file "${context.filename}":`, error);
61
+ context.report({
62
+ node: typeReference,
63
+ messageId: 'unknownError',
64
+ data: {
65
+ fileName: context.filename,
66
+ error: error instanceof Error ? error.toString() : JSON.stringify(error),
67
+ },
68
+ });
69
+ }
70
+ },
71
+ };
72
+ },
73
+ });
74
+
75
+ export default rule;
@@ -135,21 +135,19 @@ const rule = createRule({
135
135
  serviceCall: TSESTree.CallExpression,
136
136
  ) => {
137
137
  try {
138
+ if (!isCalleeServiceWrapper(serviceCall)) {
139
+ return;
140
+ }
141
+
138
142
  const enclosingScopeNode = getEnclosingScopeNode(serviceCall);
139
143
  assert.ok(enclosingScopeNode, 'enclosingScopeNode is undefined');
140
144
  const scope = scopeManager?.acquire(enclosingScopeNode);
141
145
  assert.ok(scope, 'scope is undefined');
142
-
143
146
  const urlArgument = serviceCall.arguments[0];
144
-
145
147
  if (!isUrlArgumentValid(urlArgument, scope)) {
146
148
  return;
147
149
  }
148
150
 
149
- if (!isCalleeServiceWrapper(serviceCall)) {
150
- return;
151
- }
152
-
153
151
  assert.ok(serviceCall.callee.type === AST_NODE_TYPES.MemberExpression);
154
152
  assert.ok(serviceCall.callee.property.type === AST_NODE_TYPES.Identifier);
155
153
 
@@ -0,0 +1,14 @@
1
+ // fixture/ts-tree.ts
2
+
3
+ import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils';
4
+
5
+ export function getTypeParentNode(
6
+ node: TSESTree.Node | undefined,
7
+ ): TSESTree.TSTypeAnnotation | TSESTree.TSAsExpression | undefined {
8
+ if (!node) {
9
+ return undefined;
10
+ }
11
+ return node.type === AST_NODE_TYPES.TSTypeAnnotation || node.type === AST_NODE_TYPES.TSAsExpression
12
+ ? node
13
+ : getTypeParentNode(node.parent);
14
+ }
package/src/index.ts CHANGED
@@ -15,6 +15,7 @@ import fetchResponseHeaderGetterTs, {
15
15
  import fetchThen, { ruleId as fetchThenRuleId } from './fixture/fetch-then';
16
16
  import invalidJsonStringify, { ruleId as invalidJsonStringifyRuleId } from './invalid-json-stringify';
17
17
  import noFixture, { ruleId as noFixtureRuleId } from './fixture/no-fixture';
18
+ import noFullResponse, { ruleId as noFullResponseRuleId } from './fixture/no-full-response';
18
19
  import noPromiseInstanceMethod, { ruleId as noPromiseInstanceMethodRuleId } from './no-promise-instance-method';
19
20
  import noServiceWrapper, { ruleId as noServiceWrapperRuleId } from './fixture/no-service-wrapper';
20
21
  import noStatusCode, { ruleId as noStatusCodeRuleId } from './fixture/no-status-code';
@@ -49,6 +50,7 @@ export default {
49
50
  [fetchResponseBodyJsonRuleId]: fetchResponseBodyJson,
50
51
  [fetchResponseHeaderGetterTsRuleId]: fetchResponseHeaderGetterTs,
51
52
  [addUrlDomainRuleId]: addUrlDomain,
53
+ [noFullResponseRuleId]: noFullResponse,
52
54
  },
53
55
  configs: {
54
56
  all: {
@@ -72,6 +74,7 @@ export default {
72
74
  [`@checkdigit/${fetchResponseBodyJsonRuleId}`]: 'error',
73
75
  [`@checkdigit/${fetchResponseHeaderGetterTsRuleId}`]: 'error',
74
76
  [`@checkdigit/${addUrlDomainRuleId}`]: 'error',
77
+ [`@checkdigit/${noFullResponseRuleId}`]: 'error',
75
78
  },
76
79
  },
77
80
  recommended: {