@checkdigit/eslint-plugin 6.6.0-PR.75-0fc6 → 6.6.0-PR.75-c9a3

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,102 @@
1
+ // agent/no-unused-imports.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 type { Scope } from '@typescript-eslint/utils/ts-eslint';
11
+ import getDocumentationUrl from '../get-documentation-url';
12
+
13
+ export const ruleId = 'no-unused-imports';
14
+
15
+ const createRule = ESLintUtils.RuleCreator((name) => getDocumentationUrl(name));
16
+
17
+ const rule = createRule({
18
+ name: ruleId,
19
+ meta: {
20
+ type: 'suggestion',
21
+ docs: {
22
+ description: 'Remove unused imports.',
23
+ },
24
+ messages: {
25
+ removeUnusedImports: 'Removing unused imports.',
26
+ unknownError: 'Unknown error occurred in file "{{fileName}}": {{ error }}.',
27
+ },
28
+ fixable: 'code',
29
+ schema: [],
30
+ },
31
+ defaultOptions: [],
32
+ create(context) {
33
+ const sourceCode = context.sourceCode;
34
+
35
+ function isImportUsed(specifier: TSESTree.ImportClause, scope: Scope.Scope): boolean {
36
+ return (
37
+ specifier.type !== TSESTree.AST_NODE_TYPES.ImportSpecifier ||
38
+ scope.references.some((ref) => ref.identifier.name === specifier.local.name) ||
39
+ scope.childScopes.some((childScope) => isImportUsed(specifier, childScope))
40
+ );
41
+ }
42
+
43
+ return {
44
+ ImportDeclaration(importDeclaration) {
45
+ try {
46
+ const moduleName = importDeclaration.source.value;
47
+ if (
48
+ !importDeclaration.specifiers.every(
49
+ (specifier) => specifier.type === TSESTree.AST_NODE_TYPES.ImportSpecifier,
50
+ ) ||
51
+ // [TODO:] move to meta schema
52
+ !['@checkdigit/serve-runtime', '@checkdigit/fixture'].includes(moduleName)
53
+ ) {
54
+ return;
55
+ }
56
+
57
+ const originalSpecifiers = importDeclaration.specifiers;
58
+ const scope = sourceCode.getScope(importDeclaration);
59
+ const usedSpecifiers = originalSpecifiers.filter((specifier) => isImportUsed(specifier, scope));
60
+ if (usedSpecifiers.length === originalSpecifiers.length) {
61
+ return;
62
+ }
63
+
64
+ if (usedSpecifiers.length === 0) {
65
+ context.report({
66
+ messageId: 'removeUnusedImports',
67
+ node: importDeclaration,
68
+ *fix(fixer) {
69
+ yield fixer.remove(importDeclaration);
70
+ },
71
+ });
72
+ return;
73
+ }
74
+
75
+ const usedSpecifierTexts = usedSpecifiers.map((specifier) => sourceCode.getText(specifier));
76
+ const updatedImportDeclaration = `import ${importDeclaration.importKind === 'type' ? 'type ' : ''}{ ${usedSpecifierTexts.join(', ')} } from '${moduleName}';`;
77
+
78
+ context.report({
79
+ messageId: 'removeUnusedImports',
80
+ node: importDeclaration,
81
+ *fix(fixer) {
82
+ yield fixer.replaceText(importDeclaration, updatedImportDeclaration);
83
+ },
84
+ });
85
+ } catch (error) {
86
+ // eslint-disable-next-line no-console
87
+ console.error(`Failed to apply ${ruleId} rule for file "${context.filename}":`, error);
88
+ context.report({
89
+ node: importDeclaration,
90
+ messageId: 'unknownError',
91
+ data: {
92
+ fileName: context.filename,
93
+ error: error instanceof Error ? error.toString() : JSON.stringify(error),
94
+ },
95
+ });
96
+ }
97
+ },
98
+ };
99
+ },
100
+ });
101
+
102
+ export default rule;
package/src/index.ts CHANGED
@@ -12,6 +12,9 @@ import fetchResponseHeaderGetter, {
12
12
  ruleId as fetchResponseHeaderGetterRuleId,
13
13
  } from './agent/fetch-response-header-getter';
14
14
  import fetchThen, { ruleId as fetchThenRuleId } from './agent/fetch-then';
15
+ import fixFunctionCallArguments, {
16
+ ruleId as fixFunctionCallArgumentsRuleId,
17
+ } from './agent/fix-function-call-arguments';
15
18
  import invalidJsonStringify, { ruleId as invalidJsonStringifyRuleId } from './invalid-json-stringify';
16
19
  import noDuplicatedImports, { ruleId as noDuplicatedImportsRuleId } from './no-duplicated-imports';
17
20
  import noFixture, { ruleId as noFixtureRuleId } from './agent/no-fixture';
@@ -23,6 +26,7 @@ import noStatusCode, { ruleId as noStatusCodeRuleId } from './agent/no-status-co
23
26
  import noUnusedFunctionArguments, {
24
27
  ruleId as noUnusedFunctionArgumentsRuleId,
25
28
  } from './agent/no-unused-function-argument';
29
+ import noUnusedImports, { ruleId as noUnusedImportsRuleId } from './agent/no-unused-imports';
26
30
  import noUnusedServiceVariables, { ruleId as noUnusedServiceVariablesRuleId } from './agent/no-unused-service-variable';
27
31
  import requireFixedServicesImport, {
28
32
  ruleId as requireFixedServicesImportRuleId,
@@ -71,6 +75,8 @@ export default {
71
75
  [requireTypeOutOfTypeOnlyImportsRuleId]: requireTypeOutOfTypeOnlyImports,
72
76
  [noUnusedFunctionArgumentsRuleId]: noUnusedFunctionArguments,
73
77
  [noUnusedServiceVariablesRuleId]: noUnusedServiceVariables,
78
+ [noUnusedImportsRuleId]: noUnusedImports,
79
+ [fixFunctionCallArgumentsRuleId]: fixFunctionCallArguments,
74
80
  },
75
81
  configs: {
76
82
  all: {
@@ -125,6 +131,8 @@ export default {
125
131
  [`@checkdigit/${fetchThenRuleId}`]: 'error',
126
132
  [`@checkdigit/${noUnusedFunctionArgumentsRuleId}`]: 'error',
127
133
  [`@checkdigit/${noUnusedServiceVariablesRuleId}`]: 'error',
134
+ [`@checkdigit/${noUnusedImportsRuleId}`]: 'error',
135
+ [`@checkdigit/${fixFunctionCallArgumentsRuleId}`]: 'error',
128
136
  },
129
137
  },
130
138
  'agent-phase-2-production': {
@@ -140,6 +148,8 @@ export default {
140
148
  [`@checkdigit/${fetchThenRuleId}`]: 'error',
141
149
  [`@checkdigit/${noUnusedFunctionArgumentsRuleId}`]: 'error',
142
150
  [`@checkdigit/${noUnusedServiceVariablesRuleId}`]: 'error',
151
+ [`@checkdigit/${noUnusedImportsRuleId}`]: 'error',
152
+ [`@checkdigit/${fixFunctionCallArgumentsRuleId}`]: 'error',
143
153
  },
144
154
  },
145
155
  },